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 ( |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 19 | "context" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 20 | "time" |
| 21 | ) |
| 22 | |
| 23 | //TimerType - type of timer used |
| 24 | type TimerType string |
| 25 | |
| 26 | const ( |
| 27 | tickTimer TimerType = "TickTimer" |
| 28 | pendingPoolTimer TimerType = "PendingPoolTimer" |
| 29 | ) |
| 30 | |
| 31 | var timerMap = map[TimerType]bool{ |
| 32 | tickTimer: false, |
| 33 | pendingPoolTimer: false, |
| 34 | } |
| 35 | |
| 36 | var timerChannels = make(map[TimerType](chan bool)) |
| 37 | |
| 38 | // TimerCfg structure |
| 39 | type TimerCfg struct { |
| 40 | tick time.Duration |
| 41 | } |
| 42 | |
| 43 | // Start to start timer |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 44 | func (va *VoltApplication) Start(cntx context.Context, cfg TimerCfg, timerType TimerType) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 45 | if timerMap[timerType] { |
| 46 | logger.Warn(ctx, "Duplicate Timer!!! Timer already running") |
| 47 | return |
| 48 | } |
| 49 | timerMap[timerType] = true |
| 50 | timerChannels[timerType] = make(chan bool) |
| 51 | for { |
| 52 | select { |
| 53 | case <-time.After(cfg.tick): |
| 54 | switch timerType { |
| 55 | case tickTimer: |
| 56 | va.Tick() |
| 57 | case pendingPoolTimer: |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 58 | va.removeExpiredGroups(cntx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 59 | } |
| 60 | case <- timerChannels[timerType]: |
| 61 | return |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // StopTimer to stop timers |
| 67 | func StopTimer() { |
| 68 | for _, ch := range timerChannels { |
| 69 | ch <- true |
| 70 | } |
| 71 | } |