Skip to content

Repository files navigation

fast jsonrpc GoDocGo 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"
)
funcmain() {
r:=router.New()
r.GET("/", func(ctx*fasthttp.RequestCtx) {
_, _=ctx.WriteString("Hello, world!")
})
varss fastjsonrpc.ServerMapvarttArith_=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) {
varms runtime.MemStatsruntime.ReadMemStats(&ms)
ctx.Result=ms
}))
_=fasthttp.ListenAndServe(":8080", r.Handler)
}
typeArithinttypeArgsstruct {
Aint`json:"a,omitempty"`Bint`json:"b,omitempty"`
}
func (t*Arith) Add(c*fastjsonrpc.RequestCtx) {
varaArgsifc.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) {
ifc.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

### echoPOST http://localhost:8080/rpcContent-Type: application/json
{"jsonrpc":"2.0","method":"echo","params":{"a":9,"b":9},"id":9}
### sumPOST http://localhost:8080/rpcContent-Type: application/json
{"jsonrpc":"2.0","method":"sum","params":{"a":9,"b":9},"id":9}
### Arith.AddPOST http://localhost:8080/rpcContent-Type: application/json
{"jsonrpc":"2.0","method":"Arith.Add","params":{"a":9,"b":9},"id":9}
### Arith.MulPOST http://localhost:8080/rpcContent-Type: application/json
{"jsonrpc":"2.0","method":"Arith.Mul","params":{"a":9,"b":9},"id":9}
### Arith.DivPOST http://localhost:8080/rpcContent-Type: application/json
{"jsonrpc":"2.0","method":"Arith.Div","params":{"a":9,"b":9},"id":9}
### Arith.ErrorPOST http://localhost:8080/rpcContent-Type: application/json
{"jsonrpc":"2.0","method":"Arith.Error","params":{"a":9,"b":9},"id":9}
### Arith.PanicPOST http://localhost:8080/rpcContent-Type: application/json
{"jsonrpc":"2.0","method":"Arith.Panic","params":{"a":9,"b":9},"id":9}
### memGET http://localhost:8080/mem

About

Simple Fast JSON-RPC 2.0 Server based on fasthttp

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages