Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package application |
| 17 | |
| 18 | import ( |
| 19 | "time" |
| 20 | ) |
| 21 | |
| 22 | //TimerType - type of timer used |
| 23 | type TimerType string |
| 24 | |
| 25 | const ( |
| 26 | tickTimer TimerType = "TickTimer" |
| 27 | pendingPoolTimer TimerType = "PendingPoolTimer" |
| 28 | ) |
| 29 | |
| 30 | var timerMap = map[TimerType]bool{ |
| 31 | tickTimer: false, |
| 32 | pendingPoolTimer: false, |
| 33 | } |
| 34 | |
| 35 | var timerChannels = make(map[TimerType](chan bool)) |
| 36 | |
| 37 | // TimerCfg structure |
| 38 | type TimerCfg struct { |
| 39 | tick time.Duration |
| 40 | } |
| 41 | |
| 42 | // Start to start timer |
| 43 | func (va *VoltApplication) Start(cfg TimerCfg, timerType TimerType) { |
| 44 | if timerMap[timerType] { |
| 45 | logger.Warn(ctx, "Duplicate Timer!!! Timer already running") |
| 46 | return |
| 47 | } |
| 48 | timerMap[timerType] = true |
| 49 | timerChannels[timerType] = make(chan bool) |
| 50 | for { |
| 51 | select { |
| 52 | case <-time.After(cfg.tick): |
| 53 | switch timerType { |
| 54 | case tickTimer: |
| 55 | va.Tick() |
| 56 | case pendingPoolTimer: |
| 57 | va.removeExpiredGroups() |
| 58 | } |
| 59 | case <- timerChannels[timerType]: |
| 60 | return |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // StopTimer to stop timers |
| 66 | func StopTimer() { |
| 67 | for _, ch := range timerChannels { |
| 68 | ch <- true |
| 69 | } |
| 70 | } |