micro/transport/transport.go

57 lines
1.1 KiB
Go
Raw Normal View History

2018-11-18 21:38:46 +03:00
// Package transport is an interface for synchronous communication
2015-05-21 00:57:19 +03:00
package transport
2016-01-04 00:25:03 +03:00
import (
"time"
)
2015-05-21 00:57:19 +03:00
type Message struct {
Header map[string]string
Body []byte
}
type Socket interface {
2015-05-21 23:08:19 +03:00
Recv(*Message) error
Send(*Message) error
Close() error
2018-11-14 22:49:04 +03:00
Local() string
Remote() string
2015-05-21 00:57:19 +03:00
}
type Client interface {
2016-11-05 14:44:02 +03:00
Socket
2015-05-21 00:57:19 +03:00
}
2015-05-21 23:08:19 +03:00
type Listener interface {
2015-05-21 00:57:19 +03:00
Addr() string
Close() error
2015-05-21 23:08:19 +03:00
Accept(func(Socket)) error
2015-05-21 00:57:19 +03:00
}
2016-01-31 00:19:55 +03:00
// Transport is an interface which is used for communication between
// services. It uses socket send/recv semantics and had various
// implementations {HTTP, RabbitMQ, NATS, ...}
2015-05-21 00:57:19 +03:00
type Transport interface {
Init(...Option) error
Options() Options
Dial(addr string, opts ...DialOption) (Client, error)
2016-01-18 03:10:04 +03:00
Listen(addr string, opts ...ListenOption) (Listener, error)
2015-12-20 00:56:14 +03:00
String() string
2015-05-21 00:57:19 +03:00
}
type Option func(*Options)
type DialOption func(*DialOptions)
2016-01-18 03:10:04 +03:00
type ListenOption func(*ListenOptions)
2015-05-21 00:57:19 +03:00
var (
2016-04-06 20:03:27 +03:00
DefaultTransport Transport = newHTTPTransport()
2016-01-04 00:25:03 +03:00
DefaultDialTimeout = time.Second * 5
2015-05-21 00:57:19 +03:00
)
2016-03-16 01:25:32 +03:00
func NewTransport(opts ...Option) Transport {
2016-04-06 20:03:27 +03:00
return newHTTPTransport(opts...)
}