@@ -97,19 +97,28 @@ type FileWatcherInterceptor struct {
9797 policyContents []byte
9898 refreshDuration time.Duration
9999 cancel context.CancelFunc
100+ onPolicyUpdate func ()
100101}
101102
102103// NewFileWatcher returns a new FileWatcherInterceptor from a policy file
103104// that contains JSON string of authorization policy and a refresh duration to
104105// specify the amount of time between policy refreshes.
105106func NewFileWatcher (file string , duration time.Duration ) (* FileWatcherInterceptor , error ) {
107+ return NewFileWatcherWithCallback (file , duration , func () {})
108+ }
109+
110+ // NewFileWatcherWithCallback returns a new FileWatcherInterceptor from a policy file
111+ // that contains JSON string of authorization policy and a refresh duration to
112+ // specify the amount of time between policy refreshes. The callback is invoked when
113+ // a policy is loaded.
114+ func NewFileWatcherWithCallback (file string , duration time.Duration , onPolicyUpdate func ()) (* FileWatcherInterceptor , error ) {
106115 if file == "" {
107116 return nil , fmt .Errorf ("authorization policy file path is empty" )
108117 }
109118 if duration <= time .Duration (0 ) {
110119 return nil , fmt .Errorf ("requires refresh interval(%v) greater than 0s" , duration )
111120 }
112- i := & FileWatcherInterceptor {policyFile : file , refreshDuration : duration }
121+ i := & FileWatcherInterceptor {policyFile : file , refreshDuration : duration , onPolicyUpdate : onPolicyUpdate }
113122 if err := i .updateInternalInterceptor (); err != nil {
114123 return nil , err
115124 }
@@ -155,6 +164,7 @@ func (i *FileWatcherInterceptor) updateInternalInterceptor() error {
155164 }
156165 atomic .StorePointer (& i .internalInterceptor , unsafe .Pointer (interceptor ))
157166 logger .Infof ("authorization policy reload status: successfully loaded new policy %v" , policyContentsString )
167+ i .onPolicyUpdate ()
158168 return nil
159169}
160170
0 commit comments