metaverse/truss

Proposal: Update metadata and headers in context.Context.

adamryman opened this issue · 0 comments

We current put all HTTP/1.1 headers and all gRPC metadata values into the ctx context.Context with context.WithValue(. We just use string keys for the string values.

Putting them into the context as strings is bad form as it could conflict with others just using string values (though no one should be using it)

I propose we add a type to each transport for their respective request values (headers and metadata).

Example HTTP

# svc/transport_http.go
type HTTPHeaders struct{}

func headersToContext(ctx context.Context, r *http.Request) context.Context {
    return context.WithValue(ctx, HTTPHeaders, r.Header)
}
# handlers.go

func (s echoService) Echo(ctx context.Context, in *pb.EchoRequest) (*pb.EchoResponse, error) {
   headers = ctx.Value(svc.HTTPHeaders)
   logger.Log("Content-Type", headers.Get("Content-Type"))
   return nil, nil
}

Same basic idea with GRPC.

We could also use this to put other values, or the entire original request into the context.

type HTTPRequest struct{}

Another value would be the HTTP method that was used, could be useful for knowing if a custom verb was used: #230

type HTTPVerb struct{}