Skip to content
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

feat(jsonrpc): add method to retrieve request ID from context #1281

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 20 additions & 5 deletions transport/http/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ type requestIDKeyType struct{}

var requestIDKey requestIDKeyType

// RequestIDFromContext returns the request id from context.
func RequestIDFromContext(ctx context.Context) *RequestID {
if ctx == nil {
return nil
}

v := ctx.Value(requestIDKey)
if v == nil {
return nil
}

vv, ok := v.(*RequestID)
if !ok {
return nil
}

return vv
}

// Server wraps an endpoint and implements http.Handler.
type Server struct {
ecm EndpointCodecMap
Expand Down Expand Up @@ -198,12 +217,8 @@ func DefaultErrorEncoder(ctx context.Context, err error, w http.ResponseWriter)

w.WriteHeader(http.StatusOK)

var requestID *RequestID
if v := ctx.Value(requestIDKey); v != nil {
requestID = v.(*RequestID)
}
_ = json.NewEncoder(w).Encode(Response{
ID: requestID,
ID: RequestIDFromContext(ctx),
JSONRPC: Version,
Error: &e,
})
Expand Down