fastjsonrpc

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: GPL-3.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

fast jsonrpc GoDoc Go Report

Fast JSON-RPC 2.0 Server based on fasthttp

Benchmarks

$ GOMAXPROCS=1 go test -bench=. -benchmem -benchtime=10s
goos: linux
goarch: amd64
pkg: github.com/zc310/fastjsonrpc
cpu: Intel(R) Core(TM) i7-4800MQ CPU @ 2.70GHz
BenchmarkEchoHandler            	21422912	       555.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkSumHandler             	16634361	       718.4 ns/op	       0 B/op	       0 allocs/op
BenchmarkErrorHandler           	15317910	       744.7 ns/op	      48 B/op	       1 allocs/op
BenchmarkBatchSumHandler        	 5419057	      2213 ns/op	     712 B/op	      11 allocs/op
BenchmarkParamsUnmarshalHandler 	12894405	       923.6 ns/op	      32 B/op	       2 allocs/op
PASS
ok  	github.com/zc310/fastjsonrpc	64.434s

Install

go get -u github.com/zc310/fastjsonrpc

Example

package main

import (
	"errors"
	"github.com/fasthttp/router"
	"github.com/valyala/fasthttp"
	"github.com/zc310/fastjsonrpc"
	"runtime"
)

func main() {
	r := router.New()
	r.GET("/", func(ctx *fasthttp.RequestCtx) {
		_, _ = ctx.WriteString("Hello, world!")
	})

	var ss fastjsonrpc.ServerMap

	var tt Arith
	_ = ss.Register(&tt)
	ss.RegisterHandler("echo", func(c *fastjsonrpc.RequestCtx) {
		c.Result = c.Params
	})
	ss.RegisterHandler("sum", func(c *fastjsonrpc.RequestCtx) {
		c.Result = c.Params.GetInt("a") + c.Params.GetInt("b")
	})
	ss.RegisterHandler("error", func(c *fastjsonrpc.RequestCtx) {
		c.Error = nil
	})
	r.POST("/rpc", fasthttp.CompressHandler(ss.Handler))

	r.GET("/mem", fastjsonrpc.Get(func(ctx *fastjsonrpc.RequestCtx) {
		var ms runtime.MemStats
		runtime.ReadMemStats(&ms)
		ctx.Result = ms
	}))

	_ = fasthttp.ListenAndServe(":8080", r.Handler)
}

type Arith int
type Args struct {
	A int `json:"a,omitempty"`
	B int `json:"b,omitempty"`
}

func (t *Arith) Add(c *fastjsonrpc.RequestCtx) {
	var a Args
	if c.Error = c.ParamsUnmarshal(&a); c.Error == nil {
		c.Result = a.A + a.B
	}
}

func (t *Arith) Mul(c *fastjsonrpc.RequestCtx) {
	c.Result = c.Arena.NewNumberInt(c.Params.GetInt("a") * c.Params.GetInt("b"))
}

func (t *Arith) Div(c *fastjsonrpc.RequestCtx) {
	if c.Params.GetInt("b") == 0 {
		c.Error = errors.New("divide by zero")
		return
	}
	c.Result = c.Arena.NewNumberInt(c.Params.GetInt("a") / c.Params.GetInt("b"))
}
func (t *Arith) Panic(*fastjsonrpc.RequestCtx) { panic("ERROR") }
func (t *Arith) Error(c *fastjsonrpc.RequestCtx) {
	c.Error = fastjsonrpc.NewError(-32000, "Server error")
}
HTTP Request
### echo

POST http://localhost:8080/rpc
Content-Type: application/json

{"jsonrpc":"2.0","method":"echo","params":{"a":9,"b":9},"id":9}

### sum

POST http://localhost:8080/rpc
Content-Type: application/json

{"jsonrpc":"2.0","method":"sum","params":{"a":9,"b":9},"id":9}

### Arith.Add

POST http://localhost:8080/rpc
Content-Type: application/json

{"jsonrpc":"2.0","method":"Arith.Add","params":{"a":9,"b":9},"id":9}

### Arith.Mul

POST http://localhost:8080/rpc
Content-Type: application/json

{"jsonrpc":"2.0","method":"Arith.Mul","params":{"a":9,"b":9},"id":9}

### Arith.Div

POST http://localhost:8080/rpc
Content-Type: application/json

{"jsonrpc":"2.0","method":"Arith.Div","params":{"a":9,"b":9},"id":9}

### Arith.Error

POST http://localhost:8080/rpc
Content-Type: application/json

{"jsonrpc":"2.0","method":"Arith.Error","params":{"a":9,"b":9},"id":9}

### Arith.Panic

POST http://localhost:8080/rpc
Content-Type: application/json

{"jsonrpc":"2.0","method":"Arith.Panic","params":{"a":9,"b":9},"id":9}


### mem
GET http://localhost:8080/mem

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get added in v0.1.1

func Rpc

Types

type Error

type Error struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

func NewError

func NewError(code int, message string) *Error

func (*Error) Error

func (p *Error) Error() string

type Handler

type Handler func(ctx *RequestCtx)

type RequestCtx added in v0.1.1

type RequestCtx struct {
	Ctx   *fasthttp.RequestCtx
	Arena *fastjson.Arena

	Method []byte
	Params *fastjson.Value

	Error  any
	Result any
	// contains filtered or unexported fields
}

func (*RequestCtx) ParamsUnmarshal added in v0.1.1

func (p *RequestCtx) ParamsUnmarshal(v any) error

type ServerMap

type ServerMap struct {
	// contains filtered or unexported fields
}

func (*ServerMap) Handler

func (p *ServerMap) Handler(ctx *fasthttp.RequestCtx)

func (*ServerMap) Register

func (p *ServerMap) Register(rcvr any) error

func (*ServerMap) RegisterHandler

func (p *ServerMap) RegisterHandler(method string, handler Handler)

func (*ServerMap) RegisterName

func (p *ServerMap) RegisterName(name string, rcvr any) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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