blob: aa156a8d36ade9e609e27d29f8519428d08d1192 [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"
21)
22
23//TimerType - type of timer used
24type TimerType string
25
26const (
27 tickTimer TimerType = "TickTimer"
28 pendingPoolTimer TimerType = "PendingPoolTimer"
29)
30
31var timerMap = map[TimerType]bool{
32 tickTimer: false,
33 pendingPoolTimer: false,
34}
35
36var timerChannels = make(map[TimerType](chan bool))
37
38// TimerCfg structure
39type TimerCfg struct {
40 tick time.Duration
41}
42
43// Start to start timer
Tinoj Joseph07cc5372022-07-18 22:53:51 +053044func (va *VoltApplication) Start(cntx context.Context, cfg TimerCfg, timerType TimerType) {
Naveen Sampath04696f72022-06-13 15:19:14 +053045 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 Joseph07cc5372022-07-18 22:53:51 +053058 va.removeExpiredGroups(cntx)
Naveen Sampath04696f72022-06-13 15:19:14 +053059 }
60 case <- timerChannels[timerType]:
61 return
62 }
63 }
64}
65
66// StopTimer to stop timers
67func StopTimer() {
68 for _, ch := range timerChannels {
69 ch <- true
70 }
71}