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