@@ -51,6 +51,7 @@ type templateRouter struct {
51
51
templates map [string ]* template.Template
52
52
reloadScriptPath string
53
53
reloadInterval time.Duration
54
+ reloadCallbacks []func ()
54
55
state map [string ]ServiceAliasConfig
55
56
serviceUnits map [string ]ServiceUnit
56
57
certManager certificateManager
@@ -99,6 +100,7 @@ type templateRouterCfg struct {
99
100
templates map [string ]* template.Template
100
101
reloadScriptPath string
101
102
reloadInterval time.Duration
103
+ reloadCallbacks []func ()
102
104
defaultCertificate string
103
105
defaultCertificatePath string
104
106
defaultCertificateDir string
@@ -158,6 +160,7 @@ func newTemplateRouter(cfg templateRouterCfg) (*templateRouter, error) {
158
160
templates : cfg .templates ,
159
161
reloadScriptPath : cfg .reloadScriptPath ,
160
162
reloadInterval : cfg .reloadInterval ,
163
+ reloadCallbacks : cfg .reloadCallbacks ,
161
164
state : make (map [string ]ServiceAliasConfig ),
162
165
serviceUnits : make (map [string ]ServiceUnit ),
163
166
certManager : certManager ,
@@ -384,17 +387,25 @@ func (r *templateRouter) Commit() {
384
387
385
388
// commitAndReload refreshes the backend and persists the router state.
386
389
func (r * templateRouter ) commitAndReload () error {
387
- r .lock .Lock ()
388
- defer r .lock .Unlock ()
390
+ // only state changes must be done under the lock
391
+ if err := func () error {
392
+ r .lock .Lock ()
393
+ defer r .lock .Unlock ()
394
+
395
+ glog .V (4 ).Infof ("Writing the router state" )
396
+ if err := r .writeState (); err != nil {
397
+ return err
398
+ }
389
399
390
- glog .V (4 ).Infof ("Writing the router state" )
391
- if err := r .writeState (); err != nil {
400
+ glog .V (4 ).Infof ("Writing the router config" )
401
+ return r .writeConfig ()
402
+ }(); err != nil {
392
403
return err
393
404
}
394
405
395
- glog . V ( 4 ). Infof ( "Writing the router config" )
396
- if err := r . writeConfig (); err != nil {
397
- return err
406
+ for i , fn := range r . reloadCallbacks {
407
+ glog . V ( 4 ). Infof ( "Calling reload function %d" , i )
408
+ fn ()
398
409
}
399
410
400
411
glog .V (4 ).Infof ("Reloading the router" )
0 commit comments