blob: 42b16d8e48e161a8c90385a07a22373e6cd5fb3a [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"
Naveen Sampath04696f72022-06-13 15:19:14 +053020 "time"
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053021 "voltha-go-controller/log"
Naveen Sampath04696f72022-06-13 15:19:14 +053022)
23
vinokuma926cb3e2023-03-29 11:41:06 +053024// TimerType - type of timer used
Naveen Sampath04696f72022-06-13 15:19:14 +053025type TimerType string
26
27const (
28 tickTimer TimerType = "TickTimer"
29 pendingPoolTimer TimerType = "PendingPoolTimer"
30)
31
32var timerMap = map[TimerType]bool{
33 tickTimer: false,
34 pendingPoolTimer: false,
35}
36
37var timerChannels = make(map[TimerType](chan bool))
38
39// TimerCfg structure
40type TimerCfg struct {
41 tick time.Duration
42}
43
44// Start to start timer
Tinoj Joseph07cc5372022-07-18 22:53:51 +053045func (va *VoltApplication) Start(cntx context.Context, cfg TimerCfg, timerType TimerType) {
Hitesh Chhabra2b2347d2023-07-31 17:36:48 +053046 logger.Infow(ctx, " Timer Starts", log.Fields{"Duration ": cfg})
Naveen Sampath04696f72022-06-13 15:19:14 +053047 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 Joseph07cc5372022-07-18 22:53:51 +053060 va.removeExpiredGroups(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +053061 }
vinokuma926cb3e2023-03-29 11:41:06 +053062 case <-timerChannels[timerType]:
Naveen Sampath04696f72022-06-13 15:19:14 +053063 return
64 }
65 }
66}
67
68// StopTimer to stop timers
69func StopTimer() {
70 for _, ch := range timerChannels {
71 ch <- true
72 }
73}