diff --git a/service/common/ntp/ntp.go b/service/common/ntp/ntp.go index 05c3aa24..a8b8bdb7 100644 --- a/service/common/ntp/ntp.go +++ b/service/common/ntp/ntp.go @@ -3,6 +3,7 @@ package ntp import ( "fmt" "github.com/beevik/ntp" + "sync" "time" ) @@ -10,8 +11,27 @@ const ( DisplayFormat = "2006/01/02 15:04 MST" ) -func IsDatetimeSynced() (bool, time.Time, error) { - t, err := ntp.Time("ntp.aliyun.com") +var ( + ntpSyncCache struct { + value bool + lastReq time.Time + realTime time.Time + mu sync.Mutex + } +) + +func IsDatetimeSynced() (ok bool, t time.Time, err error) { + ntpSyncCache.mu.Lock() + defer ntpSyncCache.mu.Unlock() + if time.Since(ntpSyncCache.lastReq) < 5*time.Second { + return ntpSyncCache.value, ntpSyncCache.realTime, nil + } + defer func() { + ntpSyncCache.value = ok + ntpSyncCache.lastReq = time.Now() + ntpSyncCache.realTime = t + }() + t, err = ntp.Time("ntp.aliyun.com") if err != nil { return false, time.Time{}, fmt.Errorf("IsDatetimeSynced: %w", err) } diff --git a/service/core/v2ray/process.go b/service/core/v2ray/process.go index 607fa997..ed855827 100644 --- a/service/core/v2ray/process.go +++ b/service/core/v2ray/process.go @@ -133,6 +133,7 @@ func NewProcess(tmpl *Template, prestart func() error, poststart func() error) ( } portList = append(portList, port) } + log.Trace("portList for connectivity test: %+v", portList) startTime := time.Now() for i := 0; i < len(portList); { conn, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", portList[i])) diff --git a/service/server/controller/latency.go b/service/server/controller/latency.go index eb075010..4a3edfcc 100644 --- a/service/server/controller/latency.go +++ b/service/server/controller/latency.go @@ -61,7 +61,7 @@ func GetHttpLatency(ctx *gin.Context) { common.ResponseError(ctx, logError("bad request")) return } - wt, err = service.TestHttpLatency(wt, 8*time.Second, 4, false) + wt, err = service.TestHttpLatency(wt, 8*time.Second, 128, false) if err != nil { common.ResponseError(ctx, logError(err)) return diff --git a/service/server/service/latency.go b/service/server/service/latency.go index de4097e8..62c4eeb3 100644 --- a/service/server/service/latency.go +++ b/service/server/service/latency.go @@ -149,7 +149,6 @@ func TestHttpLatency(which []*configure.Which, timeout time.Duration, maxParalle port = l.Addr().(*net.TCPAddr).Port break } - time.Sleep(30 * time.Millisecond) } v2rayInboundPort := strconv.Itoa(port) pluginPort := 0 @@ -162,7 +161,6 @@ func TestHttpLatency(which []*configure.Which, timeout time.Duration, maxParalle port = l.Addr().(*net.TCPAddr).Port break } - time.Sleep(30 * time.Millisecond) } pluginPort = port pluginPortMap[i] = port