@@ -27,7 +27,11 @@ func (x *Xray) checkXrayStatus() error {
2727 for {
2828 select {
2929 case lastLog := <- logChan :
30- if strings .Contains (lastLog , "Xray " + version ) {
30+ // Check for the actual "started" message - this is more reliable
31+ // Xray outputs: [Warning] core: Xray {version} started
32+ if strings .Contains (lastLog , "core:" ) &&
33+ strings .Contains (lastLog , "Xray " + version ) &&
34+ strings .Contains (lastLog , "started" ) {
3135 return nil
3236 }
3337
@@ -52,27 +56,36 @@ func (x *Xray) checkXrayStatus() error {
5256}
5357
5458func (x * Xray ) checkXrayHealth (baseCtx context.Context ) {
59+ consecutiveFailures := 0
60+ maxFailures := 3 // Allow a few failures before restarting
61+
5562 for {
5663 select {
5764 case <- baseCtx .Done ():
5865 return
5966 default :
6067 ctx , cancel := context .WithTimeout (baseCtx , time .Second * 3 )
6168 _ , err := x .GetSysStats (ctx )
62- cancel () // Always call cancel to avoid context leak
69+ cancel ()
6370
6471 if err != nil {
6572 if errors .Is (err , context .Canceled ) {
66- // Context was canceled due to x.ctx cancellation
67- return // Exit gracefully
73+ return
6874 }
6975
70- // Handle other errors by attempting restart
71- if err = x .Restart (); err != nil {
72- log .Println (err .Error ())
73- } else {
74- log .Println ("xray restarted" )
76+ consecutiveFailures ++
77+ // Only restart after multiple consecutive failures
78+ if consecutiveFailures >= maxFailures {
79+ log .Printf ("xray health check failed %d times, restarting..." , consecutiveFailures )
80+ if err = x .Restart (); err != nil {
81+ log .Println (err .Error ())
82+ } else {
83+ log .Println ("xray restarted" )
84+ consecutiveFailures = 0 // Reset counter after restart
85+ }
7586 }
87+ } else {
88+ consecutiveFailures = 0 // Reset on success
7689 }
7790 }
7891 time .Sleep (time .Second * 5 )
0 commit comments