Skip to content

Fast JSON-RPC 2.0 implementation for fasthttp server.

License

Notifications You must be signed in to change notification settings

lifebackend/fastjsonrpc

 
 

Repository files navigation

fastjsonrpc

Build Status Coverage Status Go Report Card GoDev

Fast JSON-RPC 2.0 implementation for fasthttp server.

$ GOMAXPROCS=1 go test -bench=. -benchmem -benchtime 10s
goos: darwin
goarch: arm64
pkg: github.com/lifebackend/fastjsonrpc
BenchmarkEchoHandler     	39853107	       277.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkSumHandler      	33881436	       354.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkBatchSumHandler 	15191965	       793.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkErrorHandler    	37461435	       320.8 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/lifebackend/fastjsonrpc	49.339s

Install

go get -u github.com/lifebackend/fastjsonrpc

Example

package main

import (
	"github.com/lifebackend/fastjsonrpc"
	"github.com/valyala/fasthttp"
)

func main() {
	repo := fastjsonrpc.NewRepository()

	repo.Register("sum", func(ctx *fastjsonrpc.RequestCtx) {
		params := ctx.Params()

		a := params.GetInt("a")
		b := params.GetInt("b")

		ctx.SetResult(ctx.Arena().NewNumberInt(a + b))
	})
	repo.Register("sum_struct", func(ctx *fastjsonrpc.RequestCtx) {
		type (
			sumRequest struct {
				A int `json:"a"`
				B int `json:"b"`
			}
			sumResponse int
		)

		var req sumRequest
		if err := ctx.ParamsUnmarshal(&req); err != nil {
			ctx.SetError(err)
			return
		}

		ctx.SetResult(sumResponse(req.A + req.B))
	})

	_ = fasthttp.ListenAndServe(":8080", repo.RequestHandler())
}

TODO

  • Parallel batch processing
  • End-to-end benchmarks

About

Fast JSON-RPC 2.0 implementation for fasthttp server.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%