micro/network/network.go

67 lines
1.3 KiB
Go
Raw Normal View History

2019-06-18 13:56:11 +03:00
// Package network is a package for defining a network overlay
2019-06-17 18:57:53 +03:00
package network
import (
"github.com/micro/go-micro/config/options"
)
2019-07-02 00:41:27 +03:00
// Network defines a network interface. The network is a single
// shared network between all nodes connected to it. The network
// is responsible for routing messages to the correct services.
2019-06-17 18:57:53 +03:00
type Network interface {
options.Options
2019-07-08 18:24:57 +03:00
// Create starts the network
Create() (*Node, error)
2019-07-07 12:10:38 +03:00
// Name of the network
Name() string
2019-07-08 18:24:57 +03:00
// Connect to a node
Connect(*Node) (Conn, error)
// Listen for connections
Listen(*Node) (Listener, error)
2019-06-17 18:57:53 +03:00
}
2019-07-08 18:24:57 +03:00
type Node struct {
Id string
Address string
Metadata map[string]string
}
type Listener interface {
2019-06-22 21:02:57 +03:00
Address() string
Close() error
2019-07-08 18:24:57 +03:00
Accept() (Conn, error)
2019-06-17 18:57:53 +03:00
}
2019-07-08 18:24:57 +03:00
type Conn interface {
// Unique id of the connection
Id() string
// Close the connection
Close() error
// Send a message
Send(*Message) error
// Receive a message
Recv(*Message) error
// The remote node
Remote() string
// The local node
Local() string
2019-06-17 18:57:53 +03:00
}
2019-06-17 20:25:42 +03:00
2019-06-22 18:51:20 +03:00
type Message struct {
Header map[string]string
2019-07-08 18:24:57 +03:00
Body []byte
2019-06-22 18:51:20 +03:00
}
2019-06-18 13:56:11 +03:00
2019-06-17 20:25:42 +03:00
var (
2019-07-07 12:10:38 +03:00
// The default network name is local
2019-07-08 18:27:02 +03:00
DefaultName = "go.micro"
// just the standard network element
DefaultNetwork = NewNetwork()
2019-06-17 20:25:42 +03:00
)
2019-07-01 13:55:15 +03:00
// NewNetwork returns a new network interface
func NewNetwork(opts ...options.Option) Network {
2019-07-02 00:59:11 +03:00
return newNetwork(opts...)
}