1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/network/network.go

72 lines
1.6 KiB
Go
Raw Normal View History

2019-06-18 12:56:11 +02:00
// Package network is a package for defining a network overlay
2019-06-17 17:57:53 +02:00
package network
import (
"github.com/micro/go-micro/config/options"
)
2019-07-01 23:41:27 +02: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 17:57:53 +02:00
type Network interface {
options.Options
2019-07-08 17:32:12 +02:00
// Create starts the network and creates a new node
2019-07-08 17:24:57 +02:00
Create() (*Node, error)
2019-07-07 11:10:38 +02:00
// Name of the network
Name() string
2019-07-08 17:32:12 +02:00
// Connect to a node on the network
2019-07-08 17:24:57 +02:00
Connect(*Node) (Conn, error)
2019-07-08 17:32:12 +02:00
// Listen for connections for this node
2019-07-08 17:24:57 +02:00
Listen(*Node) (Listener, error)
2019-06-17 17:57:53 +02:00
}
2019-07-08 17:32:12 +02:00
// Node is a network node represented with id/address and
// metadata which includes the network name, transport, etc
2019-07-08 17:24:57 +02:00
type Node struct {
Id string
Address string
Metadata map[string]string
}
2019-07-08 17:32:12 +02:00
// A network node listener which can be used to receive messages
2019-07-08 17:24:57 +02:00
type Listener interface {
2019-06-22 20:02:57 +02:00
Address() string
Close() error
2019-07-08 17:24:57 +02:00
Accept() (Conn, error)
2019-06-17 17:57:53 +02:00
}
2019-07-08 17:32:12 +02:00
// A connection from another node on the network
2019-07-08 17:24:57 +02: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 17:57:53 +02:00
}
2019-06-17 19:25:42 +02:00
2019-07-08 17:32:12 +02:00
// The message type sent over the network
2019-06-22 17:51:20 +02:00
type Message struct {
Header map[string]string
2019-07-08 17:24:57 +02:00
Body []byte
2019-06-22 17:51:20 +02:00
}
2019-06-18 12:56:11 +02:00
2019-06-17 19:25:42 +02:00
var (
2019-07-07 11:10:38 +02:00
// The default network name is local
2019-07-08 17:27:02 +02:00
DefaultName = "go.micro"
// just the standard network element
DefaultNetwork = NewNetwork()
2019-06-17 19:25:42 +02:00
)
2019-07-01 12:55:15 +02:00
// NewNetwork returns a new network interface
func NewNetwork(opts ...options.Option) Network {
2019-07-01 23:59:11 +02:00
return newNetwork(opts...)
}