diff --git a/box.go b/box.go index fe1c1b8d..b4d5cb5d 100644 --- a/box.go +++ b/box.go @@ -235,7 +235,7 @@ func (s *Box) Start() error { } func (s *Box) preStart() error { - monitor := taskmonitor.New(s.logger, C.DefaultStartTimeout) + monitor := taskmonitor.New(s.logger, C.StartTimeout) monitor.Start("start logger") err := s.logFactory.Start() monitor.Finish() @@ -331,7 +331,7 @@ func (s *Box) Close() error { default: close(s.done) } - monitor := taskmonitor.New(s.logger, C.DefaultStopTimeout) + monitor := taskmonitor.New(s.logger, C.StopTimeout) var errors error for serviceName, service := range s.postServices { monitor.Start("close ", serviceName) diff --git a/box_outbound.go b/box_outbound.go index 95ba950a..6e3f0617 100644 --- a/box_outbound.go +++ b/box_outbound.go @@ -12,7 +12,7 @@ import ( ) func (s *Box) startOutbounds() error { - monitor := taskmonitor.New(s.logger, C.DefaultStartTimeout) + monitor := taskmonitor.New(s.logger, C.StartTimeout) outboundTags := make(map[adapter.Outbound]string) outbounds := make(map[string]adapter.Outbound) for i, outboundToStart := range s.outbounds { diff --git a/cmd/sing-box/cmd_run.go b/cmd/sing-box/cmd_run.go index bde811f7..3c4dd0d9 100644 --- a/cmd/sing-box/cmd_run.go +++ b/cmd/sing-box/cmd_run.go @@ -199,7 +199,7 @@ func run() error { } func closeMonitor(ctx context.Context) { - time.Sleep(C.DefaultStopFatalTimeout) + time.Sleep(C.FatalStopTimeout) select { case <-ctx.Done(): return diff --git a/constant/timeout.go b/constant/timeout.go index 8d9ffbba..0d7a0b7d 100644 --- a/constant/timeout.go +++ b/constant/timeout.go @@ -3,15 +3,16 @@ package constant import "time" const ( - TCPTimeout = 5 * time.Second - ReadPayloadTimeout = 300 * time.Millisecond - DNSTimeout = 10 * time.Second - QUICTimeout = 30 * time.Second - STUNTimeout = 15 * time.Second - UDPTimeout = 5 * time.Minute - DefaultURLTestInterval = 3 * time.Minute - DefaultURLTestIdleTimeout = 30 * time.Minute - DefaultStartTimeout = 10 * time.Second - DefaultStopTimeout = 5 * time.Second - DefaultStopFatalTimeout = 10 * time.Second + TCPTimeout = 5 * time.Second + ReadPayloadTimeout = 300 * time.Millisecond + DNSTimeout = 10 * time.Second + QUICTimeout = 30 * time.Second + STUNTimeout = 15 * time.Second + UDPTimeout = 5 * time.Minute + DefaultURLTestInterval = 3 * time.Minute + DefaultURLTestIdleTimeout = 30 * time.Minute + StartTimeout = 10 * time.Second + StopTimeout = 5 * time.Second + FatalStopTimeout = 10 * time.Second + FakeIPMetadataSaveInterval = 10 * time.Second ) diff --git a/experimental/cachefile/fakeip.go b/experimental/cachefile/fakeip.go index e998ebb8..690b0d91 100644 --- a/experimental/cachefile/fakeip.go +++ b/experimental/cachefile/fakeip.go @@ -7,6 +7,7 @@ import ( "github.com/sagernet/bbolt" "github.com/sagernet/sing-box/adapter" + C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common/logger" M "github.com/sagernet/sing/common/metadata" ) @@ -58,12 +59,13 @@ func (c *CacheFile) FakeIPSaveMetadata(metadata *adapter.FakeIPMetadata) error { } func (c *CacheFile) FakeIPSaveMetadataAsync(metadata *adapter.FakeIPMetadata) { - if timer := c.saveMetadataTimer; timer != nil { - timer.Stop() + if c.saveMetadataTimer == nil { + c.saveMetadataTimer = time.AfterFunc(C.FakeIPMetadataSaveInterval, func() { + _ = c.FakeIPSaveMetadata(metadata) + }) + } else { + c.saveMetadataTimer.Reset(C.FakeIPMetadataSaveInterval) } - c.saveMetadataTimer = time.AfterFunc(10*time.Second, func() { - _ = c.FakeIPSaveMetadata(metadata) - }) } func (c *CacheFile) FakeIPStore(address netip.Addr, domain string) error { diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index bfc48094..030aee8d 100644 --- a/experimental/libbox/service.go +++ b/experimental/libbox/service.go @@ -81,7 +81,7 @@ func (s *BoxService) Close() error { select { case <-done: return - case <-time.After(C.DefaultStopFatalTimeout): + case <-time.After(C.FatalStopTimeout): os.Exit(1) } }() diff --git a/inbound/tun.go b/inbound/tun.go index 7a08b1ec..c86273d8 100644 --- a/inbound/tun.go +++ b/inbound/tun.go @@ -153,7 +153,7 @@ func (t *Tun) Start() error { tunInterface tun.Tun err error ) - monitor := taskmonitor.New(t.logger, C.DefaultStartTimeout) + monitor := taskmonitor.New(t.logger, C.StartTimeout) monitor.Start("open tun interface") if t.platformInterface != nil { tunInterface, err = t.platformInterface.OpenTun(&t.tunOptions, t.platformOptions) diff --git a/route/router.go b/route/router.go index a62a318b..3f5d2f40 100644 --- a/route/router.go +++ b/route/router.go @@ -422,7 +422,7 @@ func (r *Router) Outbounds() []adapter.Outbound { } func (r *Router) PreStart() error { - monitor := taskmonitor.New(r.logger, C.DefaultStartTimeout) + monitor := taskmonitor.New(r.logger, C.StartTimeout) if r.interfaceMonitor != nil { monitor.Start("initialize interface monitor") err := r.interfaceMonitor.Start() @@ -451,7 +451,7 @@ func (r *Router) PreStart() error { } func (r *Router) Start() error { - monitor := taskmonitor.New(r.logger, C.DefaultStartTimeout) + monitor := taskmonitor.New(r.logger, C.StartTimeout) if r.needGeoIPDatabase { monitor.Start("initialize geoip database") err := r.prepareGeoIPDatabase() @@ -606,7 +606,7 @@ func (r *Router) Start() error { } func (r *Router) Close() error { - monitor := taskmonitor.New(r.logger, C.DefaultStopTimeout) + monitor := taskmonitor.New(r.logger, C.StopTimeout) var err error for i, rule := range r.rules { monitor.Start("close rule[", i, "]")