Documentation
¶
Index ¶
- Constants
- Variables
- type Connection
- func (c *Connection) Close(status velaros.Status, reason string) error
- func (c *Connection) GatewayID() string
- func (c *Connection) HandleClose(status velaros.Status, reason string)
- func (c *Connection) HandleMessage(msg *velaros.SocketMessage)
- func (c *Connection) Read(readCtx context.Context) (*velaros.SocketMessage, error)
- func (c *Connection) Write(ctx context.Context, msg *velaros.SocketMessage) error
- type ConnectionMessage
- type Gateway
- func (g *Gateway) CanHandle(ctx *velaros.Context) bool
- func (g *Gateway) CanServePath(path string) bool
- func (g *Gateway) Close() error
- func (g *Gateway) Connect(ctx context.Context) error
- func (g *Gateway) DescriptorMiddleware() velaros.HandlerFunc
- func (g *Gateway) Handle(ctx *velaros.Context)
- func (g *Gateway) HandleClose(ctx *velaros.Context)
- func (g *Gateway) Ready() <-chan struct{}
- type GatewayDescriptor
- type GatewayServiceIndexer
- func (r *GatewayServiceIndexer) Close()
- func (r *GatewayServiceIndexer) FreshServiceDescriptors(threshold time.Duration) []*ServiceDescriptor
- func (r *GatewayServiceIndexer) IsClosed() bool
- func (r *GatewayServiceIndexer) MapSocket(serviceName, socketID string) (string, bool, error)
- func (r *GatewayServiceIndexer) PruneStaleServices(threshold time.Duration) ([]string, []string)
- func (r *GatewayServiceIndexer) Resolve(path string) (*ServiceDescriptor, *RouteDescriptor, bool)
- func (r *GatewayServiceIndexer) ResolveService(path string) (string, bool)
- func (r *GatewayServiceIndexer) ServiceDescriptors() []*ServiceDescriptor
- func (r *GatewayServiceIndexer) SetServiceDescriptor(descriptor *ServiceDescriptor) error
- func (r *GatewayServiceIndexer) SocketIDsByServiceInstance() map[string][]string
- func (r *GatewayServiceIndexer) UnmapSocket(socketID string)
- func (r *GatewayServiceIndexer) UnsetService(id string) error
- type RouteDescriptor
- type Service
- type ServiceDescriptor
- type Subscription
- type SubscriptionFunc
- type Transport
Constants ¶
const ContextKeyRouteDescriptor = "eurus:route-descriptor"
ContextKeyRouteDescriptor is the key used to store the matched route descriptor on the velaros context.
const ContextKeyServiceDescriptor = "eurus:service-descriptor"
ContextKeyServiceDescriptor is the key used to store the matched service descriptor on the velaros context.
const ContextKeyServiceDescriptors = "eurus:service-descriptors"
ContextKeyServiceDescriptors is the key used to store all service descriptors on the velaros context.
Variables ¶
var ( // ErrGatewayAlreadyConnected is returned by Gateway.Connect when the // gateway is already connected. ErrGatewayAlreadyConnected = errors.New("eurus: gateway already connected") // ErrServiceAlreadyListening is returned by Service.Listen when the // service is already listening. ErrServiceAlreadyListening = errors.New("eurus: service already listening") // ErrNoTransport is returned by Gateway.Connect and Service.Listen when no // transport was provided. Local services attached directly to a gateway // are driven by the gateway and must not be started on their own. ErrNoTransport = errors.New("eurus: no transport provided") // ErrSlowConsumer is returned by transport message delivery when the // receiving side rejected the message because the socket's dispatch queue // overflowed. It indicates a problem with the socket, not the receiving // instance. ErrSlowConsumer = errors.New("eurus: slow consumer") // ErrNoRouteMetadata is returned by RouteDescriptor.UnmarshalMetadata // when the route has no metadata attached. ErrNoRouteMetadata = errors.New("eurus: route has no metadata") )
var ( ServicePruneInterval = 10 * time.Second ClosedSocketTTL = 30 * time.Second HeartbeatInterval = 5 * time.Second // ConnectionMailboxSize is the default number of inbound messages buffered // per connection before HandleMessage applies backpressure to the // transport's per-socket dispatch queue. ConnectionMailboxSize = 64 )
GatewayAnnounceInterval is the default interval between gateway announcements. It is jittered per process so a fleet of gateways does not announce in lockstep.
var GatewaySendTimeout = 30 * time.Second
GatewaySendTimeout is the default time allowed for delivering a single message to a client socket before the socket is considered failed.
Functions ¶
This section is empty.
Types ¶
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
func NewConnection ¶
func NewConnection(transport Transport, gatewayID, socketID string, info *velaros.ConnectionInfo, mailboxSize int, onClosed func()) *Connection
NewConnection creates a connection with a mailbox buffering up to mailboxSize inbound messages. When the mailbox is full HandleMessage blocks, applying backpressure to the transport's per-socket dispatch queue.
func (*Connection) GatewayID ¶
func (c *Connection) GatewayID() string
func (*Connection) HandleClose ¶
func (c *Connection) HandleClose(status velaros.Status, reason string)
func (*Connection) HandleMessage ¶
func (c *Connection) HandleMessage(msg *velaros.SocketMessage)
func (*Connection) Read ¶
func (c *Connection) Read(readCtx context.Context) (*velaros.SocketMessage, error)
func (*Connection) Write ¶
func (c *Connection) Write(ctx context.Context, msg *velaros.SocketMessage) error
type ConnectionMessage ¶
type ConnectionMessage struct {
MessageType velaros.MessageType
Data []byte
}
type Gateway ¶
type Gateway struct {
Name string
ID string
Transport Transport
AnnounceInterval time.Duration
SendTimeout time.Duration
// contains filtered or unexported fields
}
func NewGateway ¶
func (*Gateway) CanServePath ¶
func (*Gateway) Close ¶
Close interrupts a running Connect call and waits for it to return. It is safe to call multiple times, and is a no-op when the gateway is not connected.
func (*Gateway) Connect ¶
Connect joins the gateway to the transport and blocks until the context is canceled, Close is called, or a transport stream fails. A gateway may be connected again after a clean shutdown.
func (*Gateway) DescriptorMiddleware ¶ added in v2.1.0
func (g *Gateway) DescriptorMiddleware() velaros.HandlerFunc
DescriptorMiddleware returns a velaros middleware that resolves the matching route descriptor for the incoming message and sets it on the context. Downstream middleware can retrieve it with RouteDescriptorFromContext — for example to enforce auth or rate-limit policy declared with velaros.WithMetadata before the message is dispatched.
func (*Gateway) HandleClose ¶
type GatewayDescriptor ¶
type GatewayDescriptor struct {
Name string `msgpack:"name"`
ServiceDescriptors []*ServiceDescriptor `msgpack:"serviceDescriptors"`
}
type GatewayServiceIndexer ¶
type GatewayServiceIndexer struct {
// contains filtered or unexported fields
}
func NewGatewayServiceIndexer ¶
func NewGatewayServiceIndexer() *GatewayServiceIndexer
func (*GatewayServiceIndexer) Close ¶
func (r *GatewayServiceIndexer) Close()
func (*GatewayServiceIndexer) FreshServiceDescriptors ¶
func (r *GatewayServiceIndexer) FreshServiceDescriptors(threshold time.Duration) []*ServiceDescriptor
func (*GatewayServiceIndexer) IsClosed ¶
func (r *GatewayServiceIndexer) IsClosed() bool
func (*GatewayServiceIndexer) MapSocket ¶
func (r *GatewayServiceIndexer) MapSocket(serviceName, socketID string) (string, bool, error)
func (*GatewayServiceIndexer) PruneStaleServices ¶
func (r *GatewayServiceIndexer) PruneStaleServices(threshold time.Duration) ([]string, []string)
func (*GatewayServiceIndexer) Resolve ¶ added in v2.1.0
func (r *GatewayServiceIndexer) Resolve(path string) (*ServiceDescriptor, *RouteDescriptor, bool)
Resolve finds the service and route that match the given path. It returns the matching service descriptor and route descriptor, or false when no service can handle the path.
func (*GatewayServiceIndexer) ResolveService ¶
func (r *GatewayServiceIndexer) ResolveService(path string) (string, bool)
ResolveService finds the name of the service that matches the given path.
func (*GatewayServiceIndexer) ServiceDescriptors ¶
func (r *GatewayServiceIndexer) ServiceDescriptors() []*ServiceDescriptor
ServiceDescriptors returns a snapshot of all current service descriptors.
func (*GatewayServiceIndexer) SetServiceDescriptor ¶
func (r *GatewayServiceIndexer) SetServiceDescriptor(descriptor *ServiceDescriptor) error
func (*GatewayServiceIndexer) SocketIDsByServiceInstance ¶
func (r *GatewayServiceIndexer) SocketIDsByServiceInstance() map[string][]string
func (*GatewayServiceIndexer) UnmapSocket ¶
func (r *GatewayServiceIndexer) UnmapSocket(socketID string)
func (*GatewayServiceIndexer) UnsetService ¶
func (r *GatewayServiceIndexer) UnsetService(id string) error
type RouteDescriptor ¶
type RouteDescriptor struct {
Pattern *velaros.Pattern
// Metadata carries arbitrary route metadata declared at the bind site
// (for example with velaros.WithMetadata). After a msgpack round trip it
// holds a msgpack.RawMessage; use UnmarshalMetadata to decode it into a
// typed value regardless of how the descriptor was obtained.
Metadata any
}
RouteDescriptor defines a route this service can handle. A route is a HTTP method, and a path matching pattern. It is used by the eurus gateway to determine which service to dispatch a request to.
func NewRouteDescriptor ¶
func NewRouteDescriptor(patternStr string) (*RouteDescriptor, error)
NewRouteDescriptor creates a new RouteDescriptor from a path pattern. The pattern determines which URL path this route will match.
func RouteDescriptorFromContext ¶ added in v2.1.0
func RouteDescriptorFromContext(ctx *velaros.Context) *RouteDescriptor
RouteDescriptorFromContext retrieves the route descriptor that was set on the context by DescriptorMiddleware or Handle. Returns nil if no descriptor was set.
func (*RouteDescriptor) MarshalMsgpack ¶
func (r *RouteDescriptor) MarshalMsgpack() ([]byte, error)
MarshalMsgpack returns the msgpack representation of the route descriptor.
func (*RouteDescriptor) UnmarshalMetadata ¶ added in v2.1.0
func (r *RouteDescriptor) UnmarshalMetadata(into any) error
UnmarshalMetadata decodes the route's metadata into the given value. It works both for descriptors received over a transport (where the metadata is a raw msgpack value) and for locally constructed descriptors (where it is the original value), so consumers behave identically across transports. Returns ErrNoRouteMetadata when the route has no metadata.
func (*RouteDescriptor) UnmarshalMsgpack ¶
func (r *RouteDescriptor) UnmarshalMsgpack(data []byte) error
UnmarshalMsgpack parses the msgpack representation of the route descriptor. Metadata is preserved as a raw msgpack value so consumers can decode it into their own types with UnmarshalMetadata.
type Service ¶
type Service struct {
// GatewayNames is a list of gateway names that the service should announce
// itself to. If the list is empty, the service will announce itself to all
// gateways on the connection.
GatewayNames []string
// Name is the name of the service. This is used to identify the service
// when announcing it to the gateway.
Name string
// ID is the unique identifier for the instance of the service. This is
// automatically generated when the service is created.
ID string
// Transport is a struct that implements the Transport interface and
// facilitates communication between services, gateways, and clients.
Transport Transport
// RouteDescriptors is a list of route descriptors that describe the routes
// that this service can handle. If this is left empty, the service will
// not be routable. This is automatically populated if the handler is a
// Navaros router.
RouteDescriptors []*RouteDescriptor
// Router is called when a request is made to the service. This can be
// either a Navaros router or a standard http.Router or http.HandlerFunc.
Router *velaros.Router
PruneInterval time.Duration
ClosedSocketTTL time.Duration
HeartbeatInterval time.Duration
MailboxSize int
// contains filtered or unexported fields
}
Service is a struct that facilitates communication between a go microservice and a eurus gateway. It will manage the announcement of the service to the gateway as well as calls any HTTP Handler or Navaros handler.
If the service is given a Navaros router, it will automatically announce any public routes declared on the router. That said any handler compatible with go's http.HandlerFunc or http.Handler interface can be used.
Note that if you opt to use something other than Navaros, you will need to assign your route descriptors manually. This can be done by using the eurus.NewRouteDescriptor function to create your route descriptors, then assigning them to the RouteDescriptors field on the service.
func NewService ¶
NewService creates a new service with the given name, connection, and handler. The service will automatically announce itself to the gateway when it starts. If the handler is a Velaros router, the service will automatically announce any public routes declared on the router.
func (*Service) Close ¶
Close interrupts a running Listen call and waits for it to return. It is safe to call multiple times, and is a no-op when the service is not listening.
type ServiceDescriptor ¶
type ServiceDescriptor struct {
Name string `msgpack:"name"`
ID string `msgpack:"id"`
GatewayNames []string `msgpack:"gatewayNames"`
RouteDescriptors []*RouteDescriptor `msgpack:"httpRouteDescriptors"`
LastSeenAt *time.Time `msgpack:"-"`
// contains filtered or unexported fields
}
func ServiceDescriptorFromContext ¶ added in v2.1.0
func ServiceDescriptorFromContext(ctx *velaros.Context) *ServiceDescriptor
ServiceDescriptorFromContext retrieves the service descriptor that was set on the context by DescriptorMiddleware. Returns nil if no descriptor was set.
func ServiceDescriptorsFromContext ¶ added in v2.1.0
func ServiceDescriptorsFromContext(ctx *velaros.Context) []*ServiceDescriptor
ServiceDescriptorsFromContext retrieves the service descriptors that were set on the context by DescriptorMiddleware. Returns nil if no descriptors were set.
type Subscription ¶
Subscription is a live transport stream created by one of the Subscribe methods on Transport. By the time a Subscription is returned the stream is registered with the transport backend — messages published from this moment on will be observed.
Serve pumps the stream, invoking the handler passed to Subscribe. It must be called exactly once, blocks until the given context is canceled (returning nil) or the stream fails (returning the error), and releases the stream's resources before returning.
type SubscriptionFunc ¶
SubscriptionFunc adapts a function to the Subscription interface.
type Transport ¶
type Transport interface {
// AnnounceGateway broadcasts a gateway descriptor to all services.
AnnounceGateway(gatewayDescriptor *GatewayDescriptor) error
// SubscribeGatewayAnnouncements subscribes to gateway announcements.
SubscribeGatewayAnnouncements(ctx context.Context, handler func(gatewayDescriptor *GatewayDescriptor)) (Subscription, error)
// AnnounceService broadcasts a service descriptor to all gateways.
AnnounceService(serviceDescriptor *ServiceDescriptor) error
// SubscribeServiceAnnouncements subscribes to service announcements.
SubscribeServiceAnnouncements(ctx context.Context, handler func(serviceDescriptor *ServiceDescriptor)) (Subscription, error)
// MessageService delivers a client message to a service instance. An error
// indicates the message was not delivered and the caller may re-route.
MessageService(serviceID, gatewayID, socketID string, connInfo *velaros.ConnectionInfo, msg *velaros.SocketMessage) error
// SubscribeServiceMessages subscribes to messages addressed to a service
// instance.
SubscribeServiceMessages(ctx context.Context, serviceID string, handler func(gatewayID, socketID string, connInfo *velaros.ConnectionInfo, msg *velaros.SocketMessage)) (Subscription, error)
// MessageGateway delivers a service message to the gateway that owns a
// socket. An error indicates the message was not delivered.
MessageGateway(gatewayID string, socketID string, msg *velaros.SocketMessage) error
// SubscribeGatewayMessages subscribes to messages addressed to a gateway.
SubscribeGatewayMessages(ctx context.Context, gatewayID string, handler func(socketID string, msg *velaros.SocketMessage)) (Subscription, error)
// ClosedSocket broadcasts that a socket has been closed.
ClosedSocket(socketID string, status velaros.Status, reason string) error
// SubscribeSocketClosures subscribes to socket closure broadcasts.
SubscribeSocketClosures(ctx context.Context, handler func(socketID string, status velaros.Status, reason string)) (Subscription, error)
// HeartbeatSockets asks a gateway which of the given sockets are no longer
// alive, and returns their IDs. An error indicates the gateway could not
// be reached at all.
HeartbeatSockets(gatewayID string, serviceID string, socketIDs []string) (dead []string, err error)
// SubscribeSocketHeartbeats subscribes a gateway to heartbeat requests.
// The handler returns the subset of socketIDs that are no longer alive.
SubscribeSocketHeartbeats(ctx context.Context, gatewayID string, handler func(serviceID string, socketIDs []string) (dead []string)) (Subscription, error)
}
Transport moves announcements, socket messages, closures, and heartbeats between gateways and services.
Handler concurrency contract: announcement, closure, and heartbeat handlers may be invoked concurrently. Message handlers may be invoked concurrently for different sockets, but calls for the same socket are serialized in arrival order.