context.go 556 B

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