1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-06-30 22:33:49 +02:00
Files
go-micro/network/network.go

72 lines
1.6 KiB
Go
Raw Normal View History

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