micro/transport/transport.go

73 lines
1.3 KiB
Go
Raw Normal View History

2015-05-21 00:57:19 +03:00
package transport
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
2015-05-21 00:57:19 +03:00
}
type Client interface {
Recv(*Message) error
Send(*Message) error
2015-05-21 00:57:19 +03:00
Close() error
}
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
}
type Transport interface {
Dial(addr string, opts ...DialOption) (Client, error)
2015-05-21 23:08:19 +03:00
Listen(addr string) (Listener, error)
2015-12-20 00:56:14 +03:00
String() string
2015-05-21 00:57:19 +03:00
}
type Options struct {
// Other options to be used by broker implementations
Options map[string]string
}
type DialOptions struct {
Stream bool
// Other options to be used by broker implementations
Options map[string]string
}
type Option func(*Options)
type DialOption func(*DialOptions)
2015-05-21 00:57:19 +03:00
var (
DefaultTransport Transport = newHttpTransport([]string{})
2015-05-21 00:57:19 +03:00
)
func WithStream() DialOption {
return func(o *DialOptions) {
o.Stream = true
}
}
func NewTransport(addrs []string, opt ...Option) Transport {
return newHttpTransport(addrs, opt...)
}
func Dial(addr string, opts ...DialOption) (Client, error) {
return DefaultTransport.Dial(addr, opts...)
2015-05-21 00:57:19 +03:00
}
2015-05-21 23:08:19 +03:00
func Listen(addr string) (Listener, error) {
return DefaultTransport.Listen(addr)
2015-05-21 00:57:19 +03:00
}
2015-12-20 00:56:14 +03:00
func String() string {
return DefaultTransport.String()
}