context.go 1023 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package proxy
  2. import (
  3. "context"
  4. )
  5. type key int
  6. const (
  7. inboundMetaKey key = iota
  8. outboundMetaKey
  9. dialerKey
  10. )
  11. func ContextWithInboundMeta(ctx context.Context, meta *InboundHandlerMeta) context.Context {
  12. return context.WithValue(ctx, inboundMetaKey, meta)
  13. }
  14. func InboundMetaFromContext(ctx context.Context) *InboundHandlerMeta {
  15. v := ctx.Value(inboundMetaKey)
  16. if v == nil {
  17. return nil
  18. }
  19. return v.(*InboundHandlerMeta)
  20. }
  21. func ContextWithOutboundMeta(ctx context.Context, meta *OutboundHandlerMeta) context.Context {
  22. return context.WithValue(ctx, outboundMetaKey, meta)
  23. }
  24. func OutboundMetaFromContext(ctx context.Context) *OutboundHandlerMeta {
  25. v := ctx.Value(outboundMetaKey)
  26. if v == nil {
  27. return nil
  28. }
  29. return v.(*OutboundHandlerMeta)
  30. }
  31. func ContextWithDialer(ctx context.Context, dialer Dialer) context.Context {
  32. return context.WithValue(ctx, dialerKey, dialer)
  33. }
  34. func DialerFromContext(ctx context.Context) Dialer {
  35. v := ctx.Value(dialerKey)
  36. if v == nil {
  37. return nil
  38. }
  39. return v.(Dialer)
  40. }