2016-12-14 15:41:48 +00:00
|
|
|
// Package is an interface for synchronous communication
|
2015-05-20 22:57:19 +01:00
|
|
|
package transport
|
|
|
|
|
2016-01-03 21:25:03 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-05-20 22:57:19 +01:00
|
|
|
type Message struct {
|
|
|
|
Header map[string]string
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type Socket interface {
|
2015-05-21 21:08:19 +01:00
|
|
|
Recv(*Message) error
|
|
|
|
Send(*Message) error
|
|
|
|
Close() error
|
2018-11-14 19:49:04 +00:00
|
|
|
Local() string
|
|
|
|
Remote() string
|
2015-05-20 22:57:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client interface {
|
2016-11-05 11:44:02 +00:00
|
|
|
Socket
|
2015-05-20 22:57:19 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:08:19 +01:00
|
|
|
type Listener interface {
|
2015-05-20 22:57:19 +01:00
|
|
|
Addr() string
|
|
|
|
Close() error
|
2015-05-21 21:08:19 +01:00
|
|
|
Accept(func(Socket)) error
|
2015-05-20 22:57:19 +01:00
|
|
|
}
|
|
|
|
|
2016-01-30 21:19:55 +00: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-20 22:57:19 +01:00
|
|
|
type Transport interface {
|
2018-08-08 18:57:29 +01:00
|
|
|
Init(...Option) error
|
|
|
|
Options() Options
|
2015-06-01 18:55:27 +01:00
|
|
|
Dial(addr string, opts ...DialOption) (Client, error)
|
2016-01-18 00:10:04 +00:00
|
|
|
Listen(addr string, opts ...ListenOption) (Listener, error)
|
2015-12-19 21:56:14 +00:00
|
|
|
String() string
|
2015-05-20 22:57:19 +01:00
|
|
|
}
|
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
type Option func(*Options)
|
2015-05-23 20:04:16 +01:00
|
|
|
|
2015-12-31 18:11:46 +00:00
|
|
|
type DialOption func(*DialOptions)
|
2015-06-01 18:55:27 +01:00
|
|
|
|
2016-01-18 00:10:04 +00:00
|
|
|
type ListenOption func(*ListenOptions)
|
|
|
|
|
2015-05-20 22:57:19 +01:00
|
|
|
var (
|
2016-04-06 18:03:27 +01:00
|
|
|
DefaultTransport Transport = newHTTPTransport()
|
2016-01-03 21:25:03 +00:00
|
|
|
|
|
|
|
DefaultDialTimeout = time.Second * 5
|
2015-05-20 22:57:19 +01:00
|
|
|
)
|
|
|
|
|
2016-03-15 22:25:32 +00:00
|
|
|
func NewTransport(opts ...Option) Transport {
|
2016-04-06 18:03:27 +01:00
|
|
|
return newHTTPTransport(opts...)
|
2015-05-23 20:04:16 +01:00
|
|
|
}
|