context.go 757 B

123456789101112131415161718192021222324252627282930313233343536
  1. package proxy
  2. import (
  3. "context"
  4. )
  5. type key int
  6. const (
  7. inboundMetaKey = key(0)
  8. outboundMetaKey = key(1)
  9. )
  10. func ContextWithInboundMeta(ctx context.Context, meta *InboundHandlerMeta) context.Context {
  11. return context.WithValue(ctx, inboundMetaKey, meta)
  12. }
  13. func InboundMetaFromContext(ctx context.Context) *InboundHandlerMeta {
  14. v := ctx.Value(inboundMetaKey)
  15. if v == nil {
  16. return nil
  17. }
  18. return v.(*InboundHandlerMeta)
  19. }
  20. func ContextWithOutboundMeta(ctx context.Context, meta *OutboundHandlerMeta) context.Context {
  21. return context.WithValue(ctx, outboundMetaKey, meta)
  22. }
  23. func OutboundMetaFromContext(ctx context.Context) *OutboundHandlerMeta {
  24. v := ctx.Value(outboundMetaKey)
  25. if v == nil {
  26. return nil
  27. }
  28. return v.(*OutboundHandlerMeta)
  29. }