Skip to content

Add request metadata to context (try #2) #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type requestIDKey struct{}
type metadataIDKey struct{}
type methodNameKey struct{}

// RequestID takes request id from context.
Expand All @@ -19,6 +20,16 @@ func WithRequestID(c context.Context, id *fastjson.RawMessage) context.Context {
return context.WithValue(c, requestIDKey{}, id)
}

// GetMetadata takes jsonrpc metadata from context.
func GetMetadata(c context.Context) Metadata {
return c.Value(metadataIDKey{}).(Metadata)
}

// WithMetadata adds jsonrpc metadata to context.
func WithMetadata(c context.Context, md Metadata) context.Context {
return context.WithValue(c, metadataIDKey{}, md)
}

// MethodName takes method name from context.
func MethodName(c context.Context) string {
return c.Value(methodNameKey{}).(string)
Expand Down
12 changes: 12 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ func TestRequestID(t *testing.T) {
require.Equal(t, &id, pick)
}

func TestMetadata(t *testing.T) {

c := context.Background()
md := Metadata{Params: Metadata{}}
c = WithMetadata(c, md)
var pick Metadata
require.NotPanics(t, func() {
pick = GetMetadata(c)
})
require.Equal(t, md, pick)
}

func TestMethodName(t *testing.T) {

c := context.Background()
Expand Down
7 changes: 4 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ func (mr *MethodRepository) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// InvokeMethod invokes JSON-RPC method.
func (mr *MethodRepository) InvokeMethod(c context.Context, r *Request) *Response {
var h Handler
var md Metadata
res := NewResponse(r)
h, res.Error = mr.TakeMethod(r)
md, res.Error = mr.TakeMethodMetadata(r)
if res.Error != nil {
return res
}

wrappedContext := WithRequestID(c, r.ID)
wrappedContext = WithMethodName(wrappedContext, r.Method)
res.Result, res.Error = h.ServeJSONRPC(wrappedContext, r.Params)
wrappedContext = WithMetadata(wrappedContext, md)
res.Result, res.Error = md.Handler.ServeJSONRPC(wrappedContext, r.Params)
if res.Error != nil {
res.Result = nil
}
Expand Down
18 changes: 14 additions & 4 deletions method.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@ func NewMethodRepository() *MethodRepository {
}
}

// TakeMethod takes jsonrpc.Func in MethodRepository.
func (mr *MethodRepository) TakeMethod(r *Request) (Handler, *Error) {
// TakeMethodMetadata takes metadata in MethodRepository for request.
func (mr *MethodRepository) TakeMethodMetadata(r *Request) (Metadata, *Error) {

if r.Method == "" || r.Version != Version {
return nil, ErrInvalidParams()
return Metadata{}, ErrInvalidParams()
}

mr.m.RLock()
md, ok := mr.r[r.Method]
mr.m.RUnlock()
if !ok {
return nil, ErrMethodNotFound()
return Metadata{}, ErrMethodNotFound()
}

return md, nil
}

// TakeMethod takes jsonrpc.Func in MethodRepository.
func (mr *MethodRepository) TakeMethod(r *Request) (Handler, *Error) {
md, err := mr.TakeMethodMetadata(r)
if err != nil {
return nil, err
}
return md.Handler, nil
}

Expand Down