StartAutoLoadPolicy starts a go routine that will every specified duration call LoadPolicy.
(d time.Duration)
| 87 | |
| 88 | // StartAutoLoadPolicy starts a go routine that will every specified duration call LoadPolicy. |
| 89 | func (e *SyncedEnforcer) StartAutoLoadPolicy(d time.Duration) { |
| 90 | // Don't start another goroutine if there is already one running |
| 91 | if !atomic.CompareAndSwapInt32(&e.autoLoadRunning, 0, 1) { |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | ticker := time.NewTicker(d) |
| 96 | go func() { |
| 97 | defer func() { |
| 98 | ticker.Stop() |
| 99 | atomic.StoreInt32(&(e.autoLoadRunning), int32(0)) |
| 100 | }() |
| 101 | n := 1 |
| 102 | for { |
| 103 | select { |
| 104 | case <-ticker.C: |
| 105 | // error intentionally ignored |
| 106 | _ = e.LoadPolicy() |
| 107 | // Uncomment this line to see when the policy is loaded. |
| 108 | // log.Print("Load policy for time: ", n) |
| 109 | n++ |
| 110 | case <-e.stopAutoLoad: |
| 111 | return |
| 112 | } |
| 113 | } |
| 114 | }() |
| 115 | } |
| 116 | |
| 117 | // StopAutoLoadPolicy causes the go routine to exit. |
| 118 | func (e *SyncedEnforcer) StopAutoLoadPolicy() { |