blob: 7ad35c3660c801f27dae96c136ce4727e16ad39b [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
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
16package application
17
18import (
Tinoj Joseph07cc5372022-07-18 22:53:51 +053019 "context"
Akash Sonief452f12024-12-12 18:20:28 +053020 "sync"
Naveen Sampath04696f72022-06-13 15:19:14 +053021 "time"
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053022 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053023)
24
vinokuma926cb3e2023-03-29 11:41:06 +053025// TimerType - type of timer used
Naveen Sampath04696f72022-06-13 15:19:14 +053026type TimerType string
27
28const (
29 tickTimer TimerType = "TickTimer"
30 pendingPoolTimer TimerType = "PendingPoolTimer"
31)
32
33var timerMap = map[TimerType]bool{
34 tickTimer: false,
35 pendingPoolTimer: false,
36}
37
Akash Sonief452f12024-12-12 18:20:28 +053038var timerChannels = sync.Map{}
Naveen Sampath04696f72022-06-13 15:19:14 +053039
40// TimerCfg structure
41type TimerCfg struct {
42 tick time.Duration
43}
44
45// Start to start timer
Tinoj Joseph07cc5372022-07-18 22:53:51 +053046func (va *VoltApplication) Start(cntx context.Context, cfg TimerCfg, timerType TimerType) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053047 logger.Infow(ctx, " Timer Starts", log.Fields{"Duration ": cfg})
Naveen Sampath04696f72022-06-13 15:19:14 +053048 if timerMap[timerType] {
49 logger.Warn(ctx, "Duplicate Timer!!! Timer already running")
50 return
51 }
52 timerMap[timerType] = true
Akash Sonief452f12024-12-12 18:20:28 +053053 ch := make(chan bool)
54 timerChannels.Store(timerType, ch)
Naveen Sampath04696f72022-06-13 15:19:14 +053055 for {
56 select {
57 case <-time.After(cfg.tick):
58 switch timerType {
59 case tickTimer:
60 va.Tick()
61 case pendingPoolTimer:
Tinoj Joseph07cc5372022-07-18 22:53:51 +053062 va.removeExpiredGroups(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +053063 }
Akash Sonief452f12024-12-12 18:20:28 +053064 case <-ch:
Naveen Sampath04696f72022-06-13 15:19:14 +053065 return
66 }
67 }
68}
69
70// StopTimer to stop timers
71func StopTimer() {
Akash Sonief452f12024-12-12 18:20:28 +053072 timerChannels.Range(func(key, value interface{}) bool {
73 ch := value.(chan bool)
Naveen Sampath04696f72022-06-13 15:19:14 +053074 ch <- true
Akash Sonief452f12024-12-12 18:20:28 +053075 /* 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 Sampath04696f72022-06-13 15:19:14 +053079}