@@ -3,10 +3,8 @@ package limits
33import (
44 "context"
55 "sync"
6- "sync/atomic"
76 "time"
87
9- "github.com/smartcontractkit/chainlink-common/pkg/contexts"
108 "github.com/smartcontractkit/chainlink-common/pkg/logger"
119 "github.com/smartcontractkit/chainlink-common/pkg/services"
1210 "github.com/smartcontractkit/chainlink-common/pkg/settings"
@@ -21,8 +19,7 @@ type updater[N any] struct {
2119 recordLimit func (context.Context , N )
2220 onLimitUpdate func (context.Context )
2321
24- creCh chan struct {}
25- cre atomic.Value
22+ ctxCh chan context.Context // to receive updates
2623
2724 stopOnce sync.Once
2825 stopCh services.StopChan
@@ -40,7 +37,7 @@ func newUpdater[N any](lggr logger.Logger, getLimitFn func(context.Context) (N,
4037 getLimitFn : getLimitFn ,
4138 subFn : subFn ,
4239 recordLimit : func (ctx context.Context , n N ) {}, // no-op
43- creCh : make (chan struct {} , 1 ),
40+ ctxCh : make (chan context. Context , 1 ),
4441 stopCh : make (chan struct {}),
4542 done : make (chan struct {}),
4643 }
@@ -59,30 +56,26 @@ func (u *updater[N]) Close() error {
5956
6057}
6158
62- func (u * updater [N ]) updateCRE (cre contexts.CRE ) {
63- if v := u .cre .Load (); v != nil && v .(contexts.CRE ) == cre {
64- return
65- }
66- u .cre .Store (cre )
59+ func (u * updater [N ]) updateCtx (ctx context.Context ) {
6760 select {
68- case u .creCh <- struct {}{} :
61+ case u .ctxCh <- ctx :
6962 default :
7063 }
7164}
7265
7366// updateLoop updates the limit either by subscribing via subFn or polling if subFn is not set. It also processes
7467// contexts.CRE updates. Stopped by Close.
7568// opt: reap after period of non-use
76- func (u * updater [N ]) updateLoop (cre contexts. CRE ) {
69+ func (u * updater [N ]) updateLoop (ctx context. Context ) {
7770 defer close (u .done )
78- ctx , cancel := u .stopCh .NewCtx ( )
71+ ctx , cancel := u .stopCh .Ctx ( context . WithoutCancel ( ctx ) )
7972 defer cancel ()
8073
8174 var updates <- chan settings.Update [N ]
8275 var cancelSub func ()
8376 var c <- chan time.Time
8477 if u .subFn != nil {
85- updates , cancelSub = u .subFn (contexts . WithCRE ( ctx , cre ) )
78+ updates , cancelSub = u .subFn (ctx )
8679 defer func () { cancelSub () }() // extra func wrapper is required to ensure we get the final cancelSub value
8780 // opt: poll now to initialize
8881 } else {
@@ -96,31 +89,30 @@ func (u *updater[N]) updateLoop(cre contexts.CRE) {
9689 return
9790
9891 case <- c :
99- limit , err := u .getLimitFn (contexts . WithCRE ( ctx , cre ) )
92+ limit , err := u .getLimitFn (ctx )
10093 if err != nil {
10194 u .lggr .Errorw ("Failed to get limit. Using default value" , "default" , limit , "err" , err )
10295 }
103- rcCtx := contexts .WithCRE (ctx , cre )
104- u .recordLimit (rcCtx , limit )
96+ u .recordLimit (ctx , limit )
10597 if u .onLimitUpdate != nil {
106- u .onLimitUpdate (rcCtx )
98+ u .onLimitUpdate (ctx )
10799 }
108100
109101 case update := <- updates :
110102 if update .Err != nil {
111103 u .lggr .Errorw ("Failed to update limit. Using default value" , "default" , update .Value , "err" , update .Err )
112104 }
113- rcCtx := contexts .WithCRE (ctx , cre )
114- u .recordLimit (rcCtx , update .Value )
105+ u .recordLimit (ctx , update .Value )
115106 if u .onLimitUpdate != nil {
116- u .onLimitUpdate (rcCtx )
107+ u .onLimitUpdate (ctx )
117108 }
118109
119- case <- u .creCh :
120- cre = u .cre .Load ().(contexts.CRE )
110+ case newCtx := <- u .ctxCh :
111+ cancel ()
112+ ctx , cancel = u .stopCh .Ctx (newCtx )
121113 if u .subFn != nil {
122114 cancelSub ()
123- updates , cancelSub = u .subFn (contexts . WithCRE ( ctx , cre ) )
115+ updates , cancelSub = u .subFn (ctx )
124116 }
125117 // opt: update now
126118 }
0 commit comments