server

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: BSD-3-Clause Imports: 5 Imported by: 10

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

func Loop(ctx context.Context, lst Accepter, newService func() Service, opts *LoopOptions) error

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.

func Static added in v0.16.0

func Static(m jrpc2.Assigner) func() Service

Static wraps a jrpc2.Assigner to trivially implement the Service interface.

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.

func NetAccepter added in v0.27.0

func NetAccepter(lst net.Listener, f channel.Framing) Accepter

NetAccepter adapts a net.Listener to the Accepter interface, using f as the channel framing.

type Local

type Local struct {
	Server *jrpc2.Server
	Client *jrpc2.Client
}

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!

func (Local) Close

func (l Local) Close() error

Close shuts down the client and waits for the server to exit, returning the result from the server's Wait method.

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.

Jump to

Keyboard shortcuts

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