1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-01-05 10:20:53 +02:00
go-micro/transport/transport.go

66 lines
1.3 KiB
Go
Raw Normal View History

2015-05-20 23:57:19 +02:00
package transport
2016-01-03 23:25:03 +02:00
import (
"time"
)
2015-05-20 23:57:19 +02:00
type Message struct {
Header map[string]string
Body []byte
}
type Socket interface {
2015-05-21 22:08:19 +02:00
Recv(*Message) error
Send(*Message) error
Close() error
2015-05-20 23:57:19 +02:00
}
type Client interface {
Recv(*Message) error
Send(*Message) error
2015-05-20 23:57:19 +02:00
Close() error
}
2015-05-21 22:08:19 +02:00
type Listener interface {
2015-05-20 23:57:19 +02:00
Addr() string
Close() error
2015-05-21 22:08:19 +02:00
Accept(func(Socket)) error
2015-05-20 23:57:19 +02:00
}
2016-01-30 23:19:55 +02: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 23:57:19 +02:00
type Transport interface {
Dial(addr string, opts ...DialOption) (Client, error)
2016-01-18 02:10:04 +02:00
Listen(addr string, opts ...ListenOption) (Listener, error)
2015-12-19 23:56:14 +02:00
String() string
2015-05-20 23:57:19 +02:00
}
type Option func(*Options)
type DialOption func(*DialOptions)
2016-01-18 02:10:04 +02:00
type ListenOption func(*ListenOptions)
2015-05-20 23:57:19 +02:00
var (
DefaultTransport Transport = newHttpTransport([]string{})
2016-01-03 23:25:03 +02:00
DefaultDialTimeout = time.Second * 5
2015-05-20 23:57:19 +02:00
)
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-20 23:57:19 +02:00
}
2016-01-18 02:10:04 +02:00
func Listen(addr string, opts ...ListenOption) (Listener, error) {
return DefaultTransport.Listen(addr, opts...)
2015-05-20 23:57:19 +02:00
}
2015-12-19 23:56:14 +02:00
func String() string {
return DefaultTransport.String()
}