NewFileWatcher returns a new FileWatcherInterceptor from a policy file that contains JSON string of authorization policy and a refresh duration to specify the amount of time between policy refreshes.
(file string, duration time.Duration)
| 103 | // that contains JSON string of authorization policy and a refresh duration to |
| 104 | // specify the amount of time between policy refreshes. |
| 105 | func NewFileWatcher(file string, duration time.Duration) (*FileWatcherInterceptor, error) { |
| 106 | if file == "" { |
| 107 | return nil, fmt.Errorf("authorization policy file path is empty") |
| 108 | } |
| 109 | if duration <= time.Duration(0) { |
| 110 | return nil, fmt.Errorf("requires refresh interval(%v) greater than 0s", duration) |
| 111 | } |
| 112 | i := &FileWatcherInterceptor{policyFile: file, refreshDuration: duration} |
| 113 | if err := i.updateInternalInterceptor(); err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | ctx, cancel := context.WithCancel(context.Background()) |
| 117 | i.cancel = cancel |
| 118 | // Create a background go routine for policy refresh. |
| 119 | go i.run(ctx) |
| 120 | return i, nil |
| 121 | } |
| 122 | |
| 123 | func (i *FileWatcherInterceptor) run(ctx context.Context) { |
| 124 | ticker := time.NewTicker(i.refreshDuration) |