Documentation ¶
Overview ¶
Package server provides support routines for running jrpc2 servers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Loop ¶
Loop obtains connections from lst and starts a server for each using a service instance returned by newService and the given options. Each server runs in a new goroutine.
If lst is closed or otherwise reports an error, the loop will terminate. The error will be reported to the caller of Loop once any active servers have returned. In addition, if ctx ends, any active servers will be stopped.
Types ¶
type Accepter ¶ added in v0.27.0
type Accepter interface { // Accept blocks until a connection is available, or until ctx ends. // If a connection is found, Accept returns a new channel for it. Accept(ctx context.Context) (channel.Channel, error) }
An Accepter obtains client connections from an external source and constructs channels from them.
type Local ¶
Local represents a client and server connected by an in-memory pipe.
func NewLocal ¶
func NewLocal(assigner jrpc2.Assigner, opts *LocalOptions) Local
NewLocal constructs a *jrpc2.Server and a *jrpc2.Client connected to it via an in-memory pipe, using the specified assigner and options. If opts == nil, it behaves as if the client and server options are also nil.
Example ¶
package main import ( "context" "fmt" "log" "github.com/creachadair/jrpc2/handler" "github.com/creachadair/jrpc2/server" ) func main() { loc := server.NewLocal(handler.Map{ "Hello": handler.New(func(context.Context) (string, error) { return "Hello, world!", nil }), }, nil) defer loc.Close() var result string if err := loc.Client.CallResult(context.Background(), "Hello", nil, &result); err != nil { log.Fatalf("Call failed: %v", err) } fmt.Println(result) }
Output: Hello, world!
type LocalOptions ¶
type LocalOptions struct { Client *jrpc2.ClientOptions Server *jrpc2.ServerOptions }
LocalOptions control the behaviour of the server and client constructed by the NewLocal function.
type LoopOptions ¶
type LoopOptions struct { // If non-nil, these options are used when constructing the server to // handle requests on an inbound connection. ServerOptions *jrpc2.ServerOptions }
LoopOptions control the behaviour of the Loop function. A nil *LoopOptions provides default values as described.
type Service ¶ added in v0.7.0
type Service interface { // This method is called to create an assigner and initialize the service // for use. If it reports an error, the server is not started. Assigner() (jrpc2.Assigner, error) // This method is called when the server for this service has exited. // The arguments are the assigner returned by the Assigner method and the // server exit status. Finish(jrpc2.Assigner, jrpc2.ServerStatus) }
Service is the interface used by the Loop function to start up a server. The methods of this interface allow the instance to manage its state:
The Assigner method is called before the server is started, to to initialize the service.
The Finish method is called after the server exits to clean up.