github.com/v2fly/v2ray-core/v4@v4.45.2/infra/conf/cfgcommon/session.go (about)

     1  package cfgcommon
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/v2fly/v2ray-core/v4/common"
     7  	"github.com/v2fly/v2ray-core/v4/infra/conf/geodata"
     8  )
     9  
    10  type configureLoadingContext int
    11  
    12  const confContextKey = configureLoadingContext(1)
    13  
    14  type configureLoadingEnvironment struct {
    15  	geoLoader geodata.Loader
    16  }
    17  
    18  func (c *configureLoadingEnvironment) GetGeoLoader() geodata.Loader {
    19  	if c.geoLoader == nil {
    20  		var err error
    21  		c.geoLoader, err = geodata.GetGeoDataLoader("standard")
    22  		common.Must(err)
    23  	}
    24  	return c.geoLoader
    25  }
    26  
    27  type ConfigureLoadingEnvironment interface {
    28  	GetGeoLoader() geodata.Loader
    29  }
    30  
    31  func NewConfigureLoadingContext(ctx context.Context) context.Context {
    32  	environment := &configureLoadingEnvironment{}
    33  	return context.WithValue(ctx, confContextKey, environment)
    34  }
    35  
    36  func GetConfigureLoadingEnvironment(ctx context.Context) ConfigureLoadingEnvironment {
    37  	return ctx.Value(confContextKey).(ConfigureLoadingEnvironment)
    38  }
    39  
    40  func SetGeoDataLoader(ctx context.Context, loader geodata.Loader) {
    41  	GetConfigureLoadingEnvironment(ctx).(*configureLoadingEnvironment).geoLoader = loader
    42  }