@@ -49,7 +49,6 @@ type reloadCache struct {
4949 index int64
5050 retention int
5151 mu sync.RWMutex
52- channel chan string
5352}
5453
5554type ReloadAgentParams struct {
@@ -124,46 +123,41 @@ func (ra *ReloadAgent) handleReload(id string) {
124123 ra .cache .mu .Unlock ()
125124 }()
126125
127- response , err := ra .reloadHAProxy ()
126+ response , err := ra .reloadHAProxy (id )
128127 if err != nil {
129128 ra .cache .failReload (response )
130- log .Warning ("Reload failed " + err . Error () )
129+ log .Warningf ("Reload %s failed: %s" , id , err )
131130 } else {
132131 ra .cache .succeedReload (response )
133-
134- d := time .Duration (ra .delay ) * time .Millisecond
135- log .Debugf ("Delaying reload for %s" , d .String ())
136- time .Sleep (d )
137- log .Debugf ("Handling reload completed, waiting for new requests" )
132+ log .Debugf ("Handling reload %s completed, waiting for new requests" , id )
138133 }
139134}
140135
141136func (ra * ReloadAgent ) handleReloads () {
142- defer close ( ra .cache . channel )
137+ ticker := time . NewTicker ( time . Duration ( ra .delay ) * time . Millisecond )
143138 for {
144139 select {
145- case id , ok := <- ra . cache . channel :
146- if ! ok {
147- return
140+ case <- ticker . C :
141+ if next := ra . cache . getNext (); next != "" {
142+ ra . handleReload ( next )
148143 }
149- ra .handleReload (id )
150144 case <- ra .done :
145+ ticker .Stop ()
151146 return
152147 }
153148 }
154149}
155150
156- func (ra * ReloadAgent ) reloadHAProxy () (string , error ) {
151+ func (ra * ReloadAgent ) reloadHAProxy (id string ) (string , error ) {
157152 // try the reload
158- log .Debug ("Reload started..." )
153+ log .Debugf ("Reload %s started" , id )
159154 t := time .Now ()
160155 output , err := execCmd (ra .reloadCmd )
161- log .Debug ("Reload finished." )
162- log .Debug ("Time elapsed: " , time .Since (t ))
156+ log .Debugf ("Reload %s finished in %s" , id , time .Since (t ))
163157 if err != nil {
164158 reloadFailedError := err
165159 // if failed, return to last known good file and restart and return the original file
166- log .Info ("Reload failed, restarting with last known good config..." )
160+ log .Infof ("Reload %s failed, restarting with last known good config..." , id )
167161 if err := copyFile (ra .configFile , ra .configFile + ".bck" ); err != nil {
168162 return fmt .Sprintf ("Reload failed: %s, failed to backup original config file for restart." , output ), err
169163 }
@@ -182,7 +176,7 @@ func (ra *ReloadAgent) reloadHAProxy() (string, error) {
182176 log .Debug ("HAProxy restarted with last known good config." )
183177 return output , reloadFailedError
184178 }
185- log .Debug ("Reload successful" )
179+ log .Debugf ("Reload %s successful" , id )
186180 // if success, replace last known good file
187181 // nolint:errcheck
188182 copyFile (ra .configFile , ra .lkgConfigFile )
@@ -220,15 +214,18 @@ func execCmd(cmd string) (string, error) {
220214
221215// Reload schedules a reload
222216func (ra * ReloadAgent ) Reload () string {
223- if ra .cache .next == "" {
224- ra .cache .newReload ()
217+ next := ra .cache .getNext ()
218+ if next == "" {
219+ next = ra .cache .newReload ()
220+ log .Debugf ("Scheduling a new reload with id: %s" , next )
225221 }
226- return ra .cache .next
222+
223+ return next
227224}
228225
229226// ForceReload calls reload directly
230227func (ra * ReloadAgent ) ForceReload () error {
231- r , err := ra .reloadHAProxy ()
228+ r , err := ra .reloadHAProxy ("force" )
232229 if err != nil {
233230 return NewReloadError (fmt .Sprintf ("Reload failed: %v, %v" , err , r ))
234231 }
@@ -244,14 +241,20 @@ func (rc *reloadCache) Init(retention int) {
244241 rc .lastSuccess = nil
245242 rc .index = 0
246243 rc .retention = retention
247- rc .channel = make (chan string )
248244}
249245
250- func (rc * reloadCache ) newReload () {
246+ func (rc * reloadCache ) newReload () string {
247+ next := rc .generateID ()
251248 rc .mu .Lock ()
252- defer rc .mu .Unlock ()
253- rc .next = rc .generateID ()
254- rc .channel <- rc .next
249+ rc .next = next
250+ rc .mu .Unlock ()
251+ return next
252+ }
253+
254+ func (rc * reloadCache ) getNext () string {
255+ rc .mu .RLock ()
256+ defer rc .mu .RUnlock ()
257+ return rc .next
255258}
256259
257260func (rc * reloadCache ) failReload (response string ) {
@@ -378,10 +381,11 @@ func (ra *ReloadAgent) Restart() error {
378381}
379382
380383func (rc * reloadCache ) generateID () string {
381- defer func () {
382- rc .index ++
383- }()
384- return fmt .Sprintf ("%s-%v" , time .Now ().Format ("2006-01-02" ), rc .index )
384+ rc .mu .Lock ()
385+ defer rc .mu .Unlock ()
386+ id := fmt .Sprintf ("%s-%v" , time .Now ().Format ("2006-01-02" ), rc .index )
387+ rc .index ++
388+ return id
385389}
386390
387391func getTimeIndexFromID (id string ) (time.Time , int64 , error ) {
0 commit comments