chore: reduce internal dependencies of the ntp package
Some checks are pending
Test / test (1.20, macos-15-intel) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-15-intel) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, ubuntu-latest) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-15-intel) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, ubuntu-latest) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-15-intel) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, ubuntu-latest) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-15-intel) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, ubuntu-latest) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Test / test (1.25, macos-15-intel) (push) Waiting to run
Test / test (1.25, macos-latest) (push) Waiting to run
Test / test (1.25, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.25, ubuntu-latest) (push) Waiting to run
Test / test (1.25, windows-latest) (push) Waiting to run
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run

This commit is contained in:
wwqgtxx 2025-10-27 17:29:57 +08:00
parent 85c56e7446
commit ff62386f6b
6 changed files with 37 additions and 25 deletions

View File

@ -39,7 +39,7 @@ import (
"github.com/metacubex/mihomo/listener/inner"
"github.com/metacubex/mihomo/listener/tproxy"
"github.com/metacubex/mihomo/log"
"github.com/metacubex/mihomo/ntp"
"github.com/metacubex/mihomo/ntp/ntp"
"github.com/metacubex/mihomo/tunnel"
)

View File

@ -3,18 +3,18 @@ package ntp
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/metacubex/mihomo/component/dialer"
"github.com/metacubex/mihomo/component/proxydialer"
"github.com/metacubex/mihomo/log"
mihomoNtp "github.com/metacubex/mihomo/ntp"
M "github.com/metacubex/sing/common/metadata"
"github.com/metacubex/sing/common/ntp"
)
var globalSrv atomic.Pointer[Service]
var globalSrv *Service
var globalMu sync.Mutex
type Service struct {
@ -23,21 +23,20 @@ type Service struct {
ticker *time.Ticker
ctx context.Context
cancel context.CancelFunc
offset atomic.Int64 // [time.Duration]
syncSystemTime bool
}
func ReCreateNTPService(server string, interval time.Duration, dialerProxy string, syncSystemTime bool) {
globalMu.Lock()
defer globalMu.Unlock()
if service := globalSrv.Swap(nil); service != nil {
service.Stop()
if globalSrv != nil {
globalSrv.Stop()
}
if server == "" || interval <= 0 {
return
}
ctx, cancel := context.WithCancel(context.Background())
service := &Service{
globalSrv = &Service{
server: M.ParseSocksaddr(server),
dialer: proxydialer.NewByNameSingDialer(dialerProxy, dialer.NewDialer()),
ticker: time.NewTicker(interval * time.Minute),
@ -45,8 +44,7 @@ func ReCreateNTPService(server string, interval time.Duration, dialerProxy strin
cancel: cancel,
syncSystemTime: syncSystemTime,
}
service.Start()
globalSrv.Store(service)
globalSrv.Start()
}
func (srv *Service) Start() {
@ -59,10 +57,6 @@ func (srv *Service) Stop() {
srv.cancel()
}
func (srv *Service) Offset() time.Duration {
return time.Duration(srv.offset.Load())
}
func (srv *Service) update() error {
var response *ntp.Response
var err error
@ -80,7 +74,7 @@ func (srv *Service) update() error {
} else if offset < time.Duration(0) {
log.Infoln("System clock is behind NTP time by %s", -offset)
}
srv.offset.Store(int64(offset))
mihomoNtp.SetOffset(offset)
if srv.syncSystemTime {
timeNow := response.Time
syncErr := setSystemTime(timeNow)
@ -97,7 +91,7 @@ func (srv *Service) update() error {
}
func (srv *Service) loopUpdate() {
defer srv.offset.Store(0)
defer mihomoNtp.SetOffset(0)
defer srv.ticker.Stop()
for {
err := srv.update()
@ -111,13 +105,3 @@ func (srv *Service) loopUpdate() {
}
}
}
func Now() time.Time {
now := time.Now()
if service := globalSrv.Load(); service != nil {
if offset := service.Offset(); offset.Abs() > 0 {
now = now.Add(offset)
}
}
return now
}

28
ntp/time.go Normal file
View File

@ -0,0 +1,28 @@
// Package ntp provide time.Now
//
// DON'T import other package in mihomo to keep minimal internal dependencies
package ntp
import (
"time"
"sync/atomic"
)
var _offset atomic.Int64 // [time.Duration]
func SetOffset(offset time.Duration) {
_offset.Store(int64(offset))
}
func GetOffset() time.Duration {
return time.Duration(_offset.Load())
}
func Now() time.Time {
now := time.Now()
if offset := GetOffset(); offset != 0 {
now = now.Add(offset)
}
return now
}