@@ -12,6 +12,7 @@ import (
1212 "google.golang.org/grpc"
1313
1414 "github.com/authzed/authzed-go/pkg/responsemeta"
15+ "github.com/authzed/ctxkey"
1516 "github.com/authzed/grpcutil"
1617
1718 log "github.com/authzed/spicedb/internal/logging"
@@ -87,40 +88,25 @@ func StreamServerInterceptor() grpc.StreamServerInterceptor {
8788}
8889
8990// Create a new type to prevent context collisions
90- type responseMetaKey string
91-
92- var metadataCtxKey responseMetaKey = "dispatched-response-meta"
93-
94- type metaHandle struct { metadata * dispatch.ResponseMeta }
91+ var ctxResponseMeta = ctxkey.NewBoxedWithDefault [* dispatch.ResponseMeta ](nil )
9592
9693// SetInContext should be called in a gRPC handler to correctly set the response metadata
9794// for the dispatched request.
9895func SetInContext (ctx context.Context , metadata * dispatch.ResponseMeta ) {
99- possibleHandle := ctx .Value (metadataCtxKey )
100- if possibleHandle == nil {
101- return
102- }
103-
104- handle := possibleHandle .(* metaHandle )
105- handle .metadata = metadata
96+ ctxResponseMeta .Set (ctx , metadata )
10697}
10798
10899// FromContext returns any metadata that was stored in the context.
109100//
110101// This is useful for testing that a handler is properly setting the context.
111102func FromContext (ctx context.Context ) * dispatch.ResponseMeta {
112- possibleHandle := ctx .Value (metadataCtxKey )
113- if possibleHandle == nil {
114- return nil
115- }
116- return possibleHandle .(* metaHandle ).metadata
103+ return ctxResponseMeta .Value (ctx )
117104}
118105
119106// contextWithHandle creates a new context with a location to store metadata
120107// returned from a dispatched request.
121108//
122109// This should only be called in middleware or testing functions.
123110func contextWithHandle (ctx context.Context ) context.Context {
124- var handle metaHandle
125- return context .WithValue (ctx , metadataCtxKey , & handle )
111+ return ctxResponseMeta .WithBox ()(ctx )
126112}
0 commit comments