Documentation ¶
Overview ¶
Package server provides an interface for defining KRPC services.
Example: Defining a handler.
type EchoService struct{} func (EchoService) Echo(ctx krpc.Context, in string, out chan<- string) error { out <- in return nil }
Example: Registering a handler with a service.
var s server.Service s.Register(EchoService{}) // s.Name is now "EchoService" http.Handle("/", server.Endpoint{&s})
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoSuchMethod = errors.New("no such method")
ErrNoSuchMethod indicates that a requested method was not found.
var ErrNoSuchService = errors.New("no such service")
ErrNoSuchService indicates that a requested service was not found.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context interface { // Get fetches the value associated with key, or "" if none is defined. Get(key string) string // Set associates key with the given value in the context, replacing any // previously-defined value for that key. Set(key, value string) // Del removes any value associated with key in the context. Del(key string) }
A Context provides access to a virtual collection of request-specific key/value metadata for an RPC call.
type Endpoint ¶
type Endpoint []*Service
An Endpoint is a collection of services that implements http.Handler to dispatch KRPC requests to the appropriate service. Each Endpoint handles the ServerInfo/List request as a special case.
func (Endpoint) Resolve ¶
Resolve returns the *Method corresponding to the given service and method name. Returns an error if either the service or the method was not found.
Note that Resolve doesn't know about the server info meta-service; that is handled separately by the endpoint.
type Map ¶
A Map implements the Context interface on a string-to-string map.
type Method ¶
type Method struct { Name string `json:"name"` // The name of the method Params []string `json:"params"` Stream bool `json:"stream,omitempty"` // contains filtered or unexported fields }
A Method represents a single method on a service.
type Service ¶
type Service struct { // Name is the name presented by the service to callers. Name string `json:"name"` // The set of methods registered with the service. Methods []*Method `json:"methods"` }
A Service represents a named collection of remotely-callable methods.
func (*Service) Method ¶
Method returns the *Method corresponding to the given name, or nil if there is no such method defined on the service. Names are case-sensitive.
func (*Service) Register ¶
Register registers the methods on the given handler with the Service. A handler is a value of a type T that exposes methods having the following general signatures:
// streaming method in which results are returned as they are available func (T) Name(Context, Input, chan<- Output) error // single-result, blocking method func (T) Name(Context, Input) (Output, error)
Methods not matching these signatures are ignored, as are any unexported methods even if they do match this signature. The service wrapper will ensure that the output channel is closed once the method returns.
It is an error if the handler has no methods defined; it is also an error if more than one method with the same name is registered.