@@ -12,20 +12,35 @@ import (
1212
1313type MyCircuit func (cfg CfgToken , refreshToken RefreshTokenFunc , validateToken ValidateTokenFunc , reconnect chan bool )
1414
15- func DebounceFirst ( circuit MyCircuit , d time.Duration ) MyCircuit {
15+ func DebounceFirstAndTimeout ( ctx context. Context , circuit MyCircuit , debounceDuration time.Duration ) MyCircuit {
1616 var threshold time.Time
1717 var m sync.Mutex
1818
1919 return func (cfg CfgToken , refreshToken RefreshTokenFunc , validateToken ValidateTokenFunc , reconnect chan bool ) {
2020 m .Lock ()
2121 defer func () {
22- threshold = time .Now ().Add (d )
22+ threshold = time .Now ().Add (debounceDuration )
2323 m .Unlock ()
2424 }()
2525 if time .Now ().Before (threshold ) {
2626 return
2727 }
28- circuit (cfg , refreshToken , validateToken , reconnect )
28+
29+ ctx , cancel := context .WithTimeout (ctx , time .Second * 20 )
30+ defer cancel ()
31+
32+ done := make (chan bool , 1 )
33+ go func () {
34+ circuit (cfg , refreshToken , validateToken , reconnect )
35+ done <- true
36+ }()
37+
38+ select {
39+ case <- done :
40+ log .Info ().Msg ("tryRefresh is finished" )
41+ case <- ctx .Done ():
42+ log .Warn ().Msg (("tryRefresh context timed out or terminated" ))
43+ }
2944 }
3045}
3146
@@ -115,7 +130,7 @@ func tryRefresh(cfg CfgToken, refreshToken RefreshTokenFunc, validateToken Valid
115130}
116131
117132func RunTokenValidator (ctx context.Context , cfg CfgToken , tokenInvalidated chan bool , reconnect chan bool , refreshToken RefreshTokenFunc , validateToken ValidateTokenFunc ) {
118- debouncedTryRefresh := DebounceFirst ( tryRefresh , time .Second * 5 )
133+ debouncedTryRefresh := DebounceFirstAndTimeout ( ctx , tryRefresh , time .Second * 5 )
119134
120135 debouncedTryRefresh (cfg , refreshToken , validateToken , reconnect )
121136
0 commit comments