Documentation ¶
Overview ¶
Package universal provides a way to express an atomic unit of work that can be used as a message handler and an HTTP handler. TODO: Also rpc! TODO: All in separate goroutines!
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HTTPError ¶
An HTTPError provides HTTP-specific response information for an error. It allows setting a specific HTTP status code, header values, and the response body.
type Message ¶
type Message interface { Context() context.Context // Reader provides the Message input as a readable byte stream. io.Reader // Writer provides the Message output as a writable byte stream. io.Writer }
Message is a task to be completed by a Processor.
type Processor ¶
type Processor interface { // Process accepts a Message, performing all actions necessary to complete // the processing of the Message except for persistence of the result to the // output data system (e.g. a message queue or a database). The Messages come // from either a Service.Run loop or a Service's HTTP handler. All Process calls // run in separate goroutines. // // Any error returned is be passed to the Finish function. If the Message // came from an HTTP request, the error is returned to the caller in the // HTTP body. Process(Message) error // Finish handles persistence of the results of processing a Message to the // output data system (e.g. a message queue or a database). The processed // Message and any error returned while processing the Message are passed // to Finish. // // Finish is only called from a Service.Run loop and is not called for Messages // from a Service's HTTP handler. All Finish calls run in separate goroutines // (in the same goroutine as the Process call). Finish(Message, error) }
A Processor implements all logic for handling incoming Messages, either from a Service.Run loop or from a Service's HTTP handler.
type RPCFinisher ¶
type RPCFinisher interface {
Finish(RPCMessage, RPCResult, error)
}
type RPCMessage ¶
type RPCResult ¶
type RPCResult = interface{}
An RPCResult is the result value from an RPC method call. The caller is responsible for type-asserting the value to the type of the result of the called RPC method (the 2nd argument of the RPC method).
type RPCService ¶
type RPCService struct {
// contains filtered or unexported fields
}
func NewRPCService ¶
func NewRPCService(finisher RPCFinisher) *RPCService
func (*RPCService) Run ¶
func (svc *RPCService) Run( next func() RPCMessage, server *rpc.Server, shutdownTimeout time.Duration, ) error
Run calls the specified RPC method then Finish on all messages returned by the provided RPCMessage source function, blocking until the provided RPCMessage source function returns nil.
When the provided RPCMessage source returns a nil RPCMessage, Run starts to shut down, blocking until all running goroutines started by Run have returned or until the shutdownTimeout is reached, whichever comes first. If the shutdownTimeout is reached, Run returns an error.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
A Service passes Messages to the Processor. Service provides a Run method for providing Messages directly to the Service and an HTTP handler that passes HTTP request bodies as Messages.
func NewService ¶
NewService creates a new Service that uses the provided Processor to process Messages.
func (*Service) Run ¶
Run calls Process then Finish on all Messages returned by the provided Message source function, blocking until the provided Message source function returns nil.
When the provided Message source returns a nil Message, Run starts to shut down, blocking until all running goroutines started by Run have returned or until the shutdownTimeout is reached, whichever comes first. If the shutdownTimeout is reached, Run returns an error.
func (*Service) ServeHTTP ¶
func (svc *Service) ServeHTTP(writer http.ResponseWriter, request *http.Request)
ServeHTTP is an HTTP handler that calls the Service's Process function with the HTTP request as a Message. All body unmarshalling and response marshalling is performed by the Process function, just like in the Service.Run loop.