github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/context.go (about)

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