66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package transport
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type Message struct {
|
|
Header map[string]string
|
|
Body []byte
|
|
}
|
|
|
|
type Socket interface {
|
|
Recv(*Message) error
|
|
Send(*Message) error
|
|
Close() error
|
|
}
|
|
|
|
type Client interface {
|
|
Recv(*Message) error
|
|
Send(*Message) error
|
|
Close() error
|
|
}
|
|
|
|
type Listener interface {
|
|
Addr() string
|
|
Close() error
|
|
Accept(func(Socket)) error
|
|
}
|
|
|
|
// Transport is an interface which is used for communication between
|
|
// services. It uses socket send/recv semantics and had various
|
|
// implementations {HTTP, RabbitMQ, NATS, ...}
|
|
type Transport interface {
|
|
Dial(addr string, opts ...DialOption) (Client, error)
|
|
Listen(addr string, opts ...ListenOption) (Listener, error)
|
|
String() string
|
|
}
|
|
|
|
type Option func(*Options)
|
|
|
|
type DialOption func(*DialOptions)
|
|
|
|
type ListenOption func(*ListenOptions)
|
|
|
|
var (
|
|
DefaultTransport Transport = newHttpTransport()
|
|
|
|
DefaultDialTimeout = time.Second * 5
|
|
)
|
|
|
|
func NewTransport(opts ...Option) Transport {
|
|
return newHttpTransport(opts...)
|
|
}
|
|
|
|
func Dial(addr string, opts ...DialOption) (Client, error) {
|
|
return DefaultTransport.Dial(addr, opts...)
|
|
}
|
|
|
|
func Listen(addr string, opts ...ListenOption) (Listener, error) {
|
|
return DefaultTransport.Listen(addr, opts...)
|
|
}
|
|
|
|
func String() string {
|
|
return DefaultTransport.String()
|
|
}
|