fastjsonrpc

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2020 License: MIT Imports: 10 Imported by: ? 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

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
BenchmarkEchoHandler            20473168               584 ns/op               0 B/op          0 allocs/op
BenchmarkSumHandler             16297743               729 ns/op               0 B/op          0 allocs/op
BenchmarkBatchSumHandler         7587087              1569 ns/op               0 B/op          0 allocs/op
BenchmarkErrorHandler           17734203               671 ns/op               0 B/op          0 allocs/op

Install

go get -u github.com/serjvanilla/fastjsonrpc

Example

package main

import (
	"github.com/serjvanilla/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

Documentation

Overview

Package fastjsonrpc provides fast JSON-RPC 2.0 server for use with fasthttp server.

Example
package main

import (
	"github.com/serjvanilla/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())
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error added in v0.2.0

type Error struct {
	Code    ErrorCode
	Message string
	Data    interface{}
}

Error is wrapper for JSON-RPC 2.0 Error Object.

func ErrInternalError added in v0.2.0

func ErrInternalError() *Error

ErrInternalError returns pre-built JSON-RPC error with code -32603 and message "Internal error".

func ErrInvalidParams added in v0.2.0

func ErrInvalidParams() *Error

ErrInvalidParams returns pre-built JSON-RPC error with code -32602 and message "Invalid params".

func ErrServerError added in v0.2.0

func ErrServerError(code ErrorCode) *Error

ErrServerError returns pre-built JSON-RPC error with provided code and message "Server error".

Example
package main

import (
	"github.com/serjvanilla/fastjsonrpc"
)

func main() {
	_ = fastjsonrpc.
		ErrServerError(fastjsonrpc.ErrorCode(-32042)).
		WithData("something went wrong")
}

func (*Error) Error added in v0.2.0

func (e *Error) Error() string

Error implements standard error interface.

func (*Error) WithData added in v0.2.0

func (e *Error) WithData(data interface{}) *Error

WithData sets error's data value.

Useful with ErrServerError.

type ErrorCode added in v0.2.0

type ErrorCode int

ErrorCode is JSON-RPC 2.0 spec defined error code.

For user defined errors it should be in range from -32099 to -32000.

type Repository

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

Repository is a JSON-RPC 2.0 methods repository.

func NewRepository

func NewRepository() *Repository

NewRepository returns empty repository.

It's safe to use Repository default value.

func (*Repository) Register

func (r *Repository) Register(method string, handler RequestHandler)

Register registers new method handler.

func (*Repository) RequestHandler

func (r *Repository) RequestHandler() fasthttp.RequestHandler

RequestHandler is suitable for using with fasthttp.

type RequestCtx added in v0.2.0

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

RequestCtx contains incoming request and manages outgoing response.

func (*RequestCtx) Arena added in v0.2.0

func (ctx *RequestCtx) Arena() *fastjson.Arena

Arena returns fastjson.Arena for current request.

RequestHandler should avoid holding references to Arena and/or constructed Values after the return.

func (*RequestCtx) Context added in v0.3.0

func (ctx *RequestCtx) Context() context.Context

func (*RequestCtx) ID added in v0.2.0

func (ctx *RequestCtx) ID() []byte

ID returns "id" field of JSON-RPC 2.0 request.

func (*RequestCtx) Method added in v0.2.0

func (ctx *RequestCtx) Method() []byte

Method returns matched method.

func (*RequestCtx) Params added in v0.2.0

func (ctx *RequestCtx) Params() *fastjson.Value

Params returns request parameters already unmarshalled with valyala/fastjson.

func (*RequestCtx) ParamsBytes added in v0.2.0

func (ctx *RequestCtx) ParamsBytes() []byte

ParamsBytes returns raw bytes of request's "params" field.

func (*RequestCtx) ParamsUnmarshal added in v0.2.0

func (ctx *RequestCtx) ParamsUnmarshal(v interface{}) *Error

ParamsUnmarshal parses request param and stores the result in the value pointed to by v.

func (*RequestCtx) SetError added in v0.2.0

func (ctx *RequestCtx) SetError(err *Error)

SetError writes JSON-RPC response with error.

It overwrites previous calls of SetResult and SetError.

func (*RequestCtx) SetResult added in v0.2.0

func (ctx *RequestCtx) SetResult(result interface{})

SetResult writes JSON-RPC response with result.

It overwrites previous calls of SetResult and SetError.

result may be *fastjson.Value, []byte, or interface{} (slower).

type RequestHandler

type RequestHandler func(ctx *RequestCtx)

RequestHandler must process incoming requests.

Jump to

Keyboard shortcuts

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