mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-05-13 09:36:05 +00:00
perf(xray): bound Xray-version request and extend cache
Replace the unbounded http.Get used by GetXrayVersions with a 10s- timeout client so a slow or unreachable GitHub can't hang the Xray Updates modal. Bump the controller cache from 60s to 15 minutes, and on a request error fall back to the last successful list when one is available.
This commit is contained in:
parent
113a29733e
commit
9735d26b3d
2 changed files with 11 additions and 3 deletions
|
|
@ -143,16 +143,22 @@ func (a *ServerController) getMetricHistoryBucket(c *gin.Context) {
|
||||||
jsonObj(c, a.serverService.AggregateSystemMetric(metric, bucket, 60), nil)
|
jsonObj(c, a.serverService.AggregateSystemMetric(metric, bucket, 60), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getXrayVersion retrieves available Xray versions, with caching for 1 minute.
|
|
||||||
func (a *ServerController) getXrayVersion(c *gin.Context) {
|
func (a *ServerController) getXrayVersion(c *gin.Context) {
|
||||||
|
const cacheTTLSeconds = 15 * 60
|
||||||
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
if now-a.lastGetVersionsTime <= 60 { // 1 minute cache
|
if a.lastVersions != nil && now-a.lastGetVersionsTime <= cacheTTLSeconds {
|
||||||
jsonObj(c, a.lastVersions, nil)
|
jsonObj(c, a.lastVersions, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
versions, err := a.serverService.GetXrayVersions()
|
versions, err := a.serverService.GetXrayVersions()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if a.lastVersions != nil {
|
||||||
|
logger.Warning("getXrayVersion failed; serving cached list:", err)
|
||||||
|
jsonObj(c, a.lastVersions, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
jsonMsg(c, I18nWeb(c, "getVersion"), err)
|
jsonMsg(c, I18nWeb(c, "getVersion"), err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -492,13 +492,15 @@ func (s *ServerService) sampleCPUUtilization() (float64, error) {
|
||||||
return s.emaCPU, nil
|
return s.emaCPU, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var xrayVersionsClient = &http.Client{Timeout: 10 * time.Second}
|
||||||
|
|
||||||
func (s *ServerService) GetXrayVersions() ([]string, error) {
|
func (s *ServerService) GetXrayVersions() ([]string, error) {
|
||||||
const (
|
const (
|
||||||
XrayURL = "https://api.github.com/repos/XTLS/Xray-core/releases"
|
XrayURL = "https://api.github.com/repos/XTLS/Xray-core/releases"
|
||||||
bufferSize = 8192
|
bufferSize = 8192
|
||||||
)
|
)
|
||||||
|
|
||||||
resp, err := http.Get(XrayURL)
|
resp, err := xrayVersionsClient.Get(XrayURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue