server

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2021 License: BSD-3-Clause Imports: 4 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(lst net.Listener, newService func() Service, opts *LoopOptions) error

Loop obtains connections from lst and starts a server for each with the given service constructor and options, running in a new goroutine. If accept reports an error, the loop will terminate and the error will be reported once all the servers currently active have returned.

TODO: Add options to support sensible rate-limitation.

func Run added in v0.16.0

func Run(ch channel.Channel, svc Service, opts *jrpc2.ServerOptions) error

Run starts a server for svc on the given channel, and blocks until it returns. The server exit status is reported to the service, and the error value is returned.

If the caller does not need the error value and does not want to wait for the server to complete, call Run in a goroutine.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/creachadair/jrpc2"
	"github.com/creachadair/jrpc2/channel"
	"github.com/creachadair/jrpc2/handler"
	"github.com/creachadair/jrpc2/server"
)

// Service is a trivial service for testing purposes.
type Service struct {
	done chan struct{}
}

func (Service) Assigner() (jrpc2.Assigner, error) {
	fmt.Println("SERVICE STARTED")
	return handler.Map{"Hello": handler.New(func(ctx context.Context) error {
		fmt.Println("Hello human")
		return nil
	})}, nil
}

func (s Service) Finish(_ jrpc2.Assigner, stat jrpc2.ServerStatus) {
	fmt.Printf("SERVICE FINISHED err=%v\n", stat.Err)
	close(s.done)
}

func main() {
	done := make(chan struct{})
	cch, sch := channel.Direct()
	go server.Run(sch, Service{done}, nil)

	cli := jrpc2.NewClient(cch, nil)
	if _, err := cli.Call(context.Background(), "Hello", nil); err != nil {
		log.Fatalf("Call failed: %v", err)
	}
	cli.Close()
	<-done
}
Output:

SERVICE STARTED
Hello human
SERVICE FINISHED err=<nil>

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 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, this function is used to convert a stream connection to an
	// RPC channel. If this field is nil, channel.RawJSON is used.
	Framing channel.Framing

	// 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.

Jump to

Keyboard shortcuts

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