consul/agent/consul/session_timers.go
hashicorp-copywrite[bot] 5fb9df1640
[COMPLIANCE] License changes (#18443)
* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* add missing license headers

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 09:12:13 -04:00

85 lines
1.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package consul
import (
"sync"
"time"
)
// SessionTimers provides a map of named timers which
// is safe for concurrent use.
type SessionTimers struct {
sync.RWMutex
m map[string]*time.Timer
}
func NewSessionTimers() *SessionTimers {
return &SessionTimers{m: make(map[string]*time.Timer)}
}
// Get returns the timer with the given id or nil.
func (t *SessionTimers) Get(id string) *time.Timer {
t.RLock()
defer t.RUnlock()
return t.m[id]
}
// Set stores the timer under given id. If tm is nil the timer
// with the given id is removed.
func (t *SessionTimers) Set(id string, tm *time.Timer) {
t.Lock()
defer t.Unlock()
if tm == nil {
delete(t.m, id)
} else {
t.m[id] = tm
}
}
// Del removes the timer with the given id.
func (t *SessionTimers) Del(id string) {
t.Set(id, nil)
}
// Len returns the number of registered timers.
func (t *SessionTimers) Len() int {
t.RLock()
defer t.RUnlock()
return len(t.m)
}
// ResetOrCreate sets the ttl of the timer with the given id or creates a new
// one if it does not exist.
func (t *SessionTimers) ResetOrCreate(id string, ttl time.Duration, afterFunc func()) {
t.Lock()
defer t.Unlock()
if tm := t.m[id]; tm != nil {
tm.Reset(ttl)
return
}
t.m[id] = time.AfterFunc(ttl, afterFunc)
}
// Stop stops the timer with the given id and removes it.
func (t *SessionTimers) Stop(id string) {
t.Lock()
defer t.Unlock()
if tm := t.m[id]; tm != nil {
tm.Stop()
delete(t.m, id)
}
}
// StopAll stops and removes all registered timers.
func (t *SessionTimers) StopAll() {
t.Lock()
defer t.Unlock()
for _, tm := range t.m {
tm.Stop()
}
t.m = make(map[string]*time.Timer)
}