context.go 535 B

1234567891011121314151617181920212223242526
  1. package core
  2. import (
  3. "context"
  4. )
  5. type key int
  6. const v2rayKey key = 1
  7. // FromContext returns an Instance from the given context, or nil if the context doesn't contain one.
  8. func FromContext(ctx context.Context) *Instance {
  9. if s, ok := ctx.Value(v2rayKey).(*Instance); ok {
  10. return s
  11. }
  12. return nil
  13. }
  14. // MustFromContext returns an Instance from the given context, or panics if not present.
  15. func MustFromContext(ctx context.Context) *Instance {
  16. v := FromContext(ctx)
  17. if v == nil {
  18. panic("V is not in context.")
  19. }
  20. return v
  21. }