jsonrpc

package module
v0.0.0-...-e028edb Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2022 License: MIT Imports: 11 Imported by: 0

README

TCP based JSON RPC 1.0 Client\Server

The package combines TCP connections management from valyala/fasthttp and Go's standard jsonrpc codec.

Here is a simple example. A server wishes to export an object of type Arith:

package server

import "errors"

type Args struct {
	A, B int
}

type Quotient struct {
	Quo, Rem int
}

type Arith int

func (t *Arith) Multiply(args *Args, reply *int) error {
	*reply = args.A * args.B
	return nil
}

func (t *Arith) Divide(args *Args, quo *Quotient) error {
	if args.B == 0 {
		return errors.New("divide by zero")
	}
	quo.Quo = args.A / args.B
	quo.Rem = args.A % args.B
	return nil
}

The server calls:

package main

import (
	"context"
	"log"
	"net"
	
	"github.com/makasim/jsonrpc"

	"server"
)

func main() {
	s := &jsonrpc.Server{
		Receivers: []interface{}{
			&server.Arith{},
		},
	}
	
	if err := s.ListenAndServe(":1234"); err != nil {
		log.Fatalf("serve: %s", err)
	}
}


At this point, clients can see a service "Arith" with methods "Arith.Multiply" and "Arith.Divide". To invoke one, a client does a remote call:

Client:

package main

import (
	"context"
	"log"
	
	"github.com/makasim/jsonrpc"
	
	"server"
)

func main() {
	ctx := context.Background()
	args := &server.Args{7,8}
	var reply int
	
	c := &jsonrpc.Client{}
	
	err := c.Call(ctx, "127.0.0.1:9999", "FooService.Func", args, &reply)
	if err != nil {
		log.Fatal("call error:", err)
	}
	
	log.Println(reply)
}

Documentation

Index

Constants

View Source
const DefaultDialTimeout = 3 * time.Second
View Source
const DefaultMaxConnsPerHost = 512
View Source
const DefaultMaxIdleConnDuration = 10 * time.Second

Variables

View Source
var ErrNoFreeConns = errors.New("no free connections available to host")
View Source
var ErrTimeout = &timeoutError{}

ErrTimeout is returned from timed out calls.

Functions

func AcquireTimer

func AcquireTimer(timeout time.Duration) *time.Timer

AcquireTimer returns a time.Timer from the pool and updates it to send the current time on its channel after at least timeout.

The returned Timer may be returned to the pool with ReleaseTimer when no longer needed. This allows reducing GC load.

func Dial

func Dial(addr string) (net.Conn, error)

func ReleaseTimer

func ReleaseTimer(t *time.Timer)

ReleaseTimer returns the time.Timer acquired via AcquireTimer to the pool and prevents the Timer from firing.

Do not access the released time.Timer or read from it's channel otherwise data races may occur.

Types

type Client

type Client struct {
	ReadTimeout         time.Duration
	WriteTimeout        time.Duration
	MaxConnsPerHost     int
	MaxConnWaitTimeout  time.Duration
	MaxIdleConnDuration time.Duration
	Dial                DialFunc
	// contains filtered or unexported fields
}

func (*Client) Call

func (c *Client) Call(ctx context.Context, addr, serviceMethod string, args, reply any) error

func (*Client) CloseIdleConnections

func (c *Client) CloseIdleConnections()

type DialFunc

type DialFunc func(addr string) (net.Conn, error)

type HostClient

type HostClient struct {
	Addr                string
	ReadTimeout         time.Duration
	WriteTimeout        time.Duration
	MaxConns            int
	MaxConnWaitTimeout  time.Duration
	MaxIdleConnDuration time.Duration
	Dial                DialFunc
	// contains filtered or unexported fields
}

func (*HostClient) Call

func (c *HostClient) Call(ctx context.Context, serviceMethod string, args, reply any) error

func (*HostClient) CloseIdleConnections

func (c *HostClient) CloseIdleConnections()

type Server

type Server struct {
	IdleTimeout    time.Duration
	WriteTimeout   time.Duration
	ReadTimeout    time.Duration
	Receivers      []interface{}
	NamedReceivers map[string]interface{}
	// contains filtered or unexported fields
}

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(addr string) error

func (*Server) Serve

func (s *Server) Serve(l net.Listener) error

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Jump to

Keyboard shortcuts

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