Files
v2rayA/service/server/controller/latency.go
T

73 lines
1.4 KiB
Go

package controller
import (
"github.com/gin-gonic/gin"
"github.com/json-iterator/go"
"github.com/v2rayA/v2rayA/common"
"github.com/v2rayA/v2rayA/db/configure"
"github.com/v2rayA/v2rayA/server/service"
"time"
)
func GetPingLatency(ctx *gin.Context) {
updatingMu.Lock()
if updating {
common.ResponseError(ctx, processingErr)
updatingMu.Unlock()
return
}
updating = true
updatingMu.Unlock()
defer func() {
updatingMu.Lock()
updating = false
updatingMu.Unlock()
}()
var wt []*configure.Which
err := jsoniter.Unmarshal([]byte(ctx.Query("whiches")), &wt)
if err != nil {
common.ResponseError(ctx, logError("bad request"))
return
}
wt, err = service.Ping(wt, 1*time.Second)
if err != nil {
common.ResponseError(ctx, logError(err))
return
}
common.ResponseSuccess(ctx, gin.H{
"whiches": wt,
})
}
func GetHttpLatency(ctx *gin.Context) {
updatingMu.Lock()
if updating {
common.ResponseError(ctx, processingErr)
updatingMu.Unlock()
return
}
updating = true
updatingMu.Unlock()
defer func() {
updatingMu.Lock()
updating = false
updatingMu.Unlock()
}()
var wt []*configure.Which
err := jsoniter.Unmarshal([]byte(ctx.Query("whiches")), &wt)
if err != nil {
common.ResponseError(ctx, logError("bad request"))
return
}
wt, err = service.TestHttpLatency(wt, 8*time.Second, 4, false)
if err != nil {
common.ResponseError(ctx, logError(err))
return
}
common.ResponseSuccess(ctx, gin.H{
"whiches": wt,
})
}