From b0cd7af416229cf9a3d1b9e6b33c14991b428417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 16 Dec 2023 15:40:14 +0800 Subject: [PATCH] Remove unnecessary context wrappers --- box.go | 9 ++++++--- experimental/libbox/service.go | 4 +--- outbound/urltest.go | 2 +- outbound/wireguard.go | 3 ++- route/router.go | 2 +- route/rule_set_remote.go | 2 +- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/box.go b/box.go index 89392d14..fe1c1b8d 100644 --- a/box.go +++ b/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) diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index 3375f750..a484c64b 100644 --- a/experimental/libbox/service.go +++ b/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 } diff --git a/outbound/urltest.go b/outbound/urltest.go index 5cae5236..3a6285d3 100644 --- a/outbound/urltest.go +++ b/outbound/urltest.go @@ -228,7 +228,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 diff --git a/outbound/wireguard.go b/outbound/wireguard.go index 5eb48450..045241ff 100644 --- a/outbound/wireguard.go +++ b/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 { diff --git a/route/router.go b/route/router.go index 97f5e787..b7aee514 100644 --- a/route/router.go +++ b/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 { diff --git a/route/rule_set_remote.go b/route/rule_set_remote.go index 4e687e21..eb6e4f9f 100644 --- a/route/rule_set_remote.go +++ b/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), } }