@@ -39,8 +39,10 @@ const (
3939
4040type IReloadAgent interface {
4141 Reload () string
42+ ReloadWithCallback (func ()) string
4243 Restart () error
4344 ForceReload () error
45+ ForceReloadWithCallback (func ()) error
4446 GetReloads () models.Reloads
4547 GetReload (id string ) * models.Reload
4648}
@@ -53,6 +55,7 @@ type reloadCache struct {
5355 index int64
5456 retention int
5557 mu sync.RWMutex
58+ callbacks map [string ]func ()
5659}
5760
5861type ReloadAgentParams struct {
@@ -134,8 +137,13 @@ func (ra *ReloadAgent) handleReload(id string) {
134137 log .WithFieldsf (logFields , log .WarnLevel , "Reload failed: %s" , err )
135138 } else {
136139 ra .cache .succeedReload (response )
140+ callback , ok := ra .cache .callbacks [id ]
141+ if ok {
142+ callback ()
143+ }
137144 log .WithFields (logFields , log .DebugLevel , "Handling reload completed, waiting for new requests" )
138145 }
146+ delete (ra .cache .callbacks , id )
139147}
140148
141149func (ra * ReloadAgent ) handleReloads () {
@@ -238,6 +246,27 @@ func (ra *ReloadAgent) ForceReload() error {
238246 return nil
239247}
240248
249+ // Reload schedules a reload, callback is called only if reload is successfull
250+ func (ra * ReloadAgent ) ReloadWithCallback (callback func ()) string {
251+ next := ra .cache .getNext ()
252+ if next == "" {
253+ next = ra .cache .newReloadWithCallback (callback )
254+ log .WithFields (map [string ]interface {}{logFieldReloadID : next }, log .DebugLevel , "Scheduling a new reload..." )
255+ }
256+
257+ return next
258+ }
259+
260+ // ForceReload calls reload directly, callback is called only if reload is successfull
261+ func (ra * ReloadAgent ) ForceReloadWithCallback (callback func ()) error {
262+ r , err := ra .reloadHAProxy ("force" )
263+ if err != nil {
264+ return NewReloadError (fmt .Sprintf ("Reload failed: %v, %v" , err , r ))
265+ }
266+ callback ()
267+ return nil
268+ }
269+
241270func (rc * reloadCache ) Init (retention int ) {
242271 rc .mu .Lock ()
243272 defer rc .mu .Unlock ()
@@ -247,6 +276,7 @@ func (rc *reloadCache) Init(retention int) {
247276 rc .lastSuccess = nil
248277 rc .index = 0
249278 rc .retention = retention
279+ rc .callbacks = make (map [string ]func ())
250280}
251281
252282func (rc * reloadCache ) newReload () string {
@@ -258,6 +288,12 @@ func (rc *reloadCache) newReload() string {
258288 return rc .next
259289}
260290
291+ func (rc * reloadCache ) newReloadWithCallback (callback func ()) string {
292+ next := rc .newReload ()
293+ rc .callbacks [next ] = callback
294+ return next
295+ }
296+
261297func (rc * reloadCache ) getNext () string {
262298 rc .mu .RLock ()
263299 defer rc .mu .RUnlock ()
@@ -427,5 +463,5 @@ func copyFile(src, dest string) error {
427463 if err != nil {
428464 return err
429465 }
430- return renameio .WriteFile (dest , data , 0644 )
466+ return renameio .WriteFile (dest , data , 0o644 )
431467}
0 commit comments