context.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package session
  2. import "context"
  3. type sessionKey int
  4. const (
  5. idSessionKey sessionKey = iota
  6. inboundSessionKey
  7. outboundSessionKey
  8. contentSessionKey
  9. muxPreferedSessionKey
  10. sockoptSessionKey
  11. trackedConnectionErrorKey
  12. )
  13. // ContextWithID returns a new context with the given ID.
  14. func ContextWithID(ctx context.Context, id ID) context.Context {
  15. return context.WithValue(ctx, idSessionKey, id)
  16. }
  17. // IDFromContext returns ID in this context, or 0 if not contained.
  18. func IDFromContext(ctx context.Context) ID {
  19. if id, ok := ctx.Value(idSessionKey).(ID); ok {
  20. return id
  21. }
  22. return 0
  23. }
  24. func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
  25. return context.WithValue(ctx, inboundSessionKey, inbound)
  26. }
  27. func InboundFromContext(ctx context.Context) *Inbound {
  28. if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
  29. return inbound
  30. }
  31. return nil
  32. }
  33. func ContextWithOutbound(ctx context.Context, outbound *Outbound) context.Context {
  34. return context.WithValue(ctx, outboundSessionKey, outbound)
  35. }
  36. func OutboundFromContext(ctx context.Context) *Outbound {
  37. if outbound, ok := ctx.Value(outboundSessionKey).(*Outbound); ok {
  38. return outbound
  39. }
  40. return nil
  41. }
  42. func ContextWithContent(ctx context.Context, content *Content) context.Context {
  43. return context.WithValue(ctx, contentSessionKey, content)
  44. }
  45. func ContentFromContext(ctx context.Context) *Content {
  46. if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
  47. return content
  48. }
  49. return nil
  50. }
  51. // ContextWithMuxPrefered returns a new context with the given bool
  52. func ContextWithMuxPrefered(ctx context.Context, forced bool) context.Context {
  53. return context.WithValue(ctx, muxPreferedSessionKey, forced)
  54. }
  55. // MuxPreferedFromContext returns value in this context, or false if not contained.
  56. func MuxPreferedFromContext(ctx context.Context) bool {
  57. if val, ok := ctx.Value(muxPreferedSessionKey).(bool); ok {
  58. return val
  59. }
  60. return false
  61. }
  62. // ContextWithSockopt returns a new context with Socket configs included
  63. func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
  64. return context.WithValue(ctx, sockoptSessionKey, s)
  65. }
  66. // SockoptFromContext returns Socket configs in this context, or nil if not contained.
  67. func SockoptFromContext(ctx context.Context) *Sockopt {
  68. if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
  69. return sockopt
  70. }
  71. return nil
  72. }
  73. func GetForcedOutboundTagFromContext(ctx context.Context) string {
  74. if ContentFromContext(ctx) == nil {
  75. return ""
  76. }
  77. return ContentFromContext(ctx).Attribute("forcedOutboundTag")
  78. }
  79. func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
  80. if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
  81. ctx = ContextWithContent(ctx, &Content{})
  82. }
  83. ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
  84. return ctx
  85. }
  86. type TrackedRequestErrorFeedback interface {
  87. SubmitError(err error)
  88. }
  89. func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
  90. if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
  91. errorTracker := errorTracker.(TrackedRequestErrorFeedback)
  92. errorTracker.SubmitError(err)
  93. }
  94. }
  95. func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
  96. return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
  97. }