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