server

package
v0.0.0-...-2435b8f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

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

View Source
var ErrNoSuchMethod = errors.New("no such method")

ErrNoSuchMethod indicates that a requested method was not found.

View Source
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

func (e Endpoint) Resolve(serviceName, methodName string) (*Method, error)

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.

func (Endpoint) ServeHTTP

func (e Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

func (Endpoint) ServePipes

func (e Endpoint) ServePipes(ctx Context, r io.Reader, w io.Writer) error

ServePipes implements the rpc protocol over an input and output stream

type Map

type Map map[string]string

A Map implements the Context interface on a string-to-string map.

func (Map) Del

func (m Map) Del(key string)

Del removes the value associated with a given key.

func (Map) Get

func (m Map) Get(key string) string

Get returns the value associated with a given key with the default value being the empty string.

func (Map) Set

func (m Map) Set(key, value string)

Set overrides the value associated with a given key.

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.

func (*Method) Invoke

func (m *Method) Invoke(ctx Context, in []byte, out func([]byte)) error

Invoke calls the method's handler with the given input. Each encoded output from the method is passed to out, and the final result is returned. Invoke will panic if it is unable to encode an output from the handler.

Returns ErrNoSuchMethod if m == nil.

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

func (s *Service) Method(name string) *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

func (s *Service) Register(handler interface{}) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL