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
go get -u github.com/lifebackend/fastjsonrpc
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())
}
- Parallel batch processing
- End-to-end benchmarks