sourcegraph / jsonrpc2

Package jsonrpc2 provides a client and server implementation of JSON-RPC 2.0 (http://www.jsonrpc.org/specification)
MIT License
195 stars 62 forks source link

[WIP] Adjust Handler interface and support middleware #56

Closed ggicci closed 1 year ago

ggicci commented 2 years ago
  1. This will be a breaking change since the original jsonrpc2.Handler interface will be changed from
type Handler interface {
    Handle(context.Context, *Conn, *Request)
}

to

type Handler interface {
    Handle(*Conn, *Request)
}
  1. In order to pass down context more easily from handlers to handlers, the context will be attached to Request. And the following new methods will be added to Request:
Context() context.Context
WithContext(newContext context.Context) *Request
  1. A new function Chain(middleware ...Middleware) will be added for writing middlewares.
func MyHandler(conn *Conn, r *Request) {
    // ...
}

func NoInternalMethods(next Handler) Handler {
    // hide "internal_*" methods from external users
}

func RequireSignature(next Handler) Handler {
    // extra field "signature" is required
}

handler := Chain(NoInternalMethods, RequireSignature).Then(MyHandler)

I hope this could help.

keegancsmith commented 2 years ago

Hi, thanks for the changes. Can you please give some more motivation in your description around why you made this change, the shortcomings of the current API and why this is better. It isn't really that clear to me, which also makes it hard to see if this justifies a backwards incompatible change.

ggicci commented 2 years ago

@keegancsmith Sure. I'm still working on this. The original implementation is okay but I hope my changes can make the API more clear and concise. I will give more explanations here and discuss the advantages when I finished this. Thanks :)

ggicci commented 1 year ago

Sorry I'm not able to finish this in the foreseeable future. I will close it for now.