blob: 1a80cb5466108ddfd99a9427588d3d1e4a3551c2 [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 (
19 "time"
20)
21
22//TimerType - type of timer used
23type TimerType string
24
25const (
26 tickTimer TimerType = "TickTimer"
27 pendingPoolTimer TimerType = "PendingPoolTimer"
28)
29
30var timerMap = map[TimerType]bool{
31 tickTimer: false,
32 pendingPoolTimer: false,
33}
34
35var timerChannels = make(map[TimerType](chan bool))
36
37// TimerCfg structure
38type TimerCfg struct {
39 tick time.Duration
40}
41
42// Start to start timer
43func (va *VoltApplication) Start(cfg TimerCfg, timerType TimerType) {
44 if timerMap[timerType] {
45 logger.Warn(ctx, "Duplicate Timer!!! Timer already running")
46 return
47 }
48 timerMap[timerType] = true
49 timerChannels[timerType] = make(chan bool)
50 for {
51 select {
52 case <-time.After(cfg.tick):
53 switch timerType {
54 case tickTimer:
55 va.Tick()
56 case pendingPoolTimer:
57 va.removeExpiredGroups()
58 }
59 case <- timerChannels[timerType]:
60 return
61 }
62 }
63}
64
65// StopTimer to stop timers
66func StopTimer() {
67 for _, ch := range timerChannels {
68 ch <- true
69 }
70}