2019-07-10 19:09:22 +01:00
|
|
|
// Package link provides a measured link on top of a transport.Socket
|
2019-07-10 17:12:51 +01:00
|
|
|
package link
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/config/options"
|
|
|
|
"github.com/micro/go-micro/transport"
|
|
|
|
)
|
|
|
|
|
2019-08-20 12:48:51 +01:00
|
|
|
var (
|
|
|
|
// ErrLinkClosed is returned when attempting i/o operation on the closed link
|
|
|
|
ErrLinkClosed = errors.New("link closed")
|
|
|
|
)
|
|
|
|
|
2019-07-10 19:09:22 +01:00
|
|
|
// Link is a layer on top of a transport socket with the
|
2019-08-20 12:48:51 +01:00
|
|
|
// buffering send and recv queues with the ability to
|
2019-07-10 19:09:22 +01:00
|
|
|
// measure the actual transport link and reconnect if
|
|
|
|
// an address is specified.
|
2019-07-10 17:12:51 +01:00
|
|
|
type Link interface {
|
|
|
|
// provides the transport.Socket interface
|
|
|
|
transport.Socket
|
2019-07-10 19:07:18 +01:00
|
|
|
// Connect connects the link. It must be called first
|
|
|
|
// if there's an expectation to create a new socket.
|
2019-07-10 17:12:51 +01:00
|
|
|
Connect() error
|
2019-07-10 17:43:36 +01:00
|
|
|
// Id of the link is "local" if not specified
|
2019-07-10 17:12:51 +01:00
|
|
|
Id() string
|
|
|
|
// Status of the link
|
|
|
|
Status() string
|
|
|
|
// Depth of the buffers
|
|
|
|
Weight() int
|
|
|
|
// Rate of the link
|
|
|
|
Length() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLink creates a new link on top of a socket
|
|
|
|
func NewLink(opts ...options.Option) Link {
|
|
|
|
return newLink(options.NewOptions(opts...))
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:09:22 +01:00
|
|
|
// Sets the link id which otherwise defaults to "local"
|
2019-07-10 17:12:51 +01:00
|
|
|
func Id(id string) options.Option {
|
|
|
|
return options.WithValue("link.id", id)
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:07:18 +01:00
|
|
|
// The address to use for the link. Connect must be
|
2019-07-10 19:09:22 +01:00
|
|
|
// called for this to be used, its otherwise unused.
|
2019-07-10 17:12:51 +01:00
|
|
|
func Address(a string) options.Option {
|
|
|
|
return options.WithValue("link.address", a)
|
|
|
|
}
|
|
|
|
|
2019-07-10 19:07:18 +01:00
|
|
|
// The transport to use for the link where we
|
|
|
|
// want to dial the connection first.
|
2019-07-10 17:12:51 +01:00
|
|
|
func Transport(t transport.Transport) options.Option {
|
|
|
|
return options.WithValue("link.transport", t)
|
|
|
|
}
|
2019-07-10 19:07:18 +01:00
|
|
|
|
2019-07-10 19:09:22 +01:00
|
|
|
// Socket sets the socket to use instead of dialing.
|
2019-07-10 19:07:18 +01:00
|
|
|
func Socket(s transport.Socket) options.Option {
|
|
|
|
return options.WithValue("link.socket", s)
|
|
|
|
}
|