Sfoglia il codice sorgente

Remove unnecessary context wrappers

世界 1 anno fa
parent
commit
2e607118c3
6 ha cambiato i file con 12 aggiunte e 10 eliminazioni
  1. 6 3
      box.go
  2. 1 3
      experimental/libbox/service.go
  3. 1 1
      outbound/urltest.go
  4. 2 1
      outbound/wireguard.go
  5. 1 1
      route/router.go
  6. 1 1
      route/rule_set_remote.go

+ 6 - 3
box.go

@@ -55,7 +55,7 @@ func New(options Options) (*Box, error) {
 		ctx = context.Background()
 	}
 	ctx = service.ContextWithDefaultRegistry(ctx)
-	ctx = pause.ContextWithDefaultManager(ctx)
+	ctx = pause.WithDefaultManager(ctx)
 	experimentalOptions := common.PtrValueOrDefault(options.Experimental)
 	applyDebugOptions(common.PtrValueOrDefault(experimentalOptions.Debug))
 	var needCacheFile bool
@@ -157,9 +157,12 @@ func New(options Options) (*Box, error) {
 	preServices2 := make(map[string]adapter.Service)
 	postServices := make(map[string]adapter.Service)
 	if needCacheFile {
-		cacheFile := cachefile.New(ctx, common.PtrValueOrDefault(experimentalOptions.CacheFile))
+		cacheFile := service.FromContext[adapter.CacheFile](ctx)
+		if cacheFile == nil {
+			cacheFile = cachefile.New(ctx, common.PtrValueOrDefault(experimentalOptions.CacheFile))
+			service.MustRegister[adapter.CacheFile](ctx, cacheFile)
+		}
 		preServices1["cache file"] = cacheFile
-		service.MustRegister[adapter.CacheFile](ctx, cacheFile)
 	}
 	if needClashAPI {
 		clashAPIOptions := common.PtrValueOrDefault(experimentalOptions.ClashAPI)

+ 1 - 3
experimental/libbox/service.go

@@ -44,8 +44,6 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
 	ctx = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID)
 	urlTestHistoryStorage := urltest.NewHistoryStorage()
 	ctx = service.ContextWithPtr(ctx, urlTestHistoryStorage)
-	pauseManager := pause.NewDefaultManager(ctx)
-	ctx = pause.ContextWithManager(ctx, pauseManager)
 	platformWrapper := &platformInterfaceWrapper{iif: platformInterface, useProcFS: platformInterface.UseProcFS()}
 	instance, err := box.New(box.Options{
 		Context:           ctx,
@@ -63,7 +61,7 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
 		cancel:                cancel,
 		instance:              instance,
 		urlTestHistoryStorage: urlTestHistoryStorage,
-		pauseManager:          pauseManager,
+		pauseManager:          service.FromContext[pause.Manager](ctx),
 	}, nil
 }
 

+ 1 - 1
outbound/urltest.go

@@ -233,7 +233,7 @@ func NewURLTestGroup(
 		idleTimeout:                  idleTimeout,
 		history:                      history,
 		close:                        make(chan struct{}),
-		pauseManager:                 pause.ManagerFromContext(ctx),
+		pauseManager:                 service.FromContext[pause.Manager](ctx),
 		interruptGroup:               interrupt.NewGroup(),
 		interruptExternalConnections: interruptExternalConnections,
 	}, nil

+ 2 - 1
outbound/wireguard.go

@@ -23,6 +23,7 @@ import (
 	M "github.com/sagernet/sing/common/metadata"
 	N "github.com/sagernet/sing/common/network"
 	"github.com/sagernet/sing/common/x/list"
+	"github.com/sagernet/sing/service"
 	"github.com/sagernet/sing/service/pause"
 	"github.com/sagernet/wireguard-go/conn"
 	"github.com/sagernet/wireguard-go/device"
@@ -61,7 +62,7 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context
 		},
 		ctx:          ctx,
 		workers:      options.Workers,
-		pauseManager: pause.ManagerFromContext(ctx),
+		pauseManager: service.FromContext[pause.Manager](ctx),
 	}
 	peers, err := wireguard.ParsePeers(options)
 	if err != nil {

+ 1 - 1
route/router.go

@@ -125,7 +125,7 @@ func NewRouter(
 		autoDetectInterface:   options.AutoDetectInterface,
 		defaultInterface:      options.DefaultInterface,
 		defaultMark:           options.DefaultMark,
-		pauseManager:          pause.ManagerFromContext(ctx),
+		pauseManager:          service.FromContext[pause.Manager](ctx),
 		platformInterface:     platformInterface,
 		needWIFIState:         hasRule(options.Rules, isWIFIRule) || hasDNSRule(dnsOptions.Rules, isWIFIDNSRule),
 		needPackageManager: C.IsAndroid && platformInterface == nil && common.Any(inbounds, func(inbound option.Inbound) bool {

+ 1 - 1
route/rule_set_remote.go

@@ -55,7 +55,7 @@ func NewRemoteRuleSet(ctx context.Context, router adapter.Router, logger logger.
 		logger:         logger,
 		options:        options,
 		updateInterval: updateInterval,
-		pauseManager:   pause.ManagerFromContext(ctx),
+		pauseManager:   service.FromContext[pause.Manager](ctx),
 	}
 }