1
0

In ListenerCounter struct int -> int64.

This commit is contained in:
Alexander Andreev 2024-05-09 20:18:06 +04:00
parent 0244f6afd5
commit ec5b6a028b
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34

View File

@ -11,7 +11,7 @@ import (
// ListenerCounter stores the current and peak numbers of listeners. // ListenerCounter stores the current and peak numbers of listeners.
type ListenerCounter struct { type ListenerCounter struct {
mutex sync.RWMutex mutex sync.RWMutex
current, peak int current, peak int64
} }
// NewListenerCounter returns a new ListenerCounter struct instance. // NewListenerCounter returns a new ListenerCounter struct instance.
@ -20,26 +20,26 @@ func NewListenerCounter() *ListenerCounter {
} }
// Current returns a number of current listeners. // Current returns a number of current listeners.
func (l *ListenerCounter) Current() int { func (l *ListenerCounter) Current() int64 {
l.mutex.RLock() l.mutex.RLock()
defer l.mutex.RUnlock() defer l.mutex.RUnlock()
return l.current return l.current
} }
// Current returns a number of peak listeners. // Current returns a number of peak listeners.
func (l *ListenerCounter) Peak() int { func (l *ListenerCounter) Peak() int64 {
l.mutex.RLock() l.mutex.RLock()
defer l.mutex.RUnlock() defer l.mutex.RUnlock()
return l.peak return l.peak
} }
// Inc increments by 1 a current number of listeners and updates a peak number. // Inc increments by 1 a current number of listeners and updates a peak number.
func (l *ListenerCounter) Inc() int { func (l *ListenerCounter) Inc() int64 {
l.mutex.Lock() l.mutex.Lock()
defer l.mutex.Unlock() defer l.mutex.Unlock()
if l.current == math.MaxInt { if l.current == math.MaxInt64 {
// We panic here because if this will ever happen, then something's going certainly wrong. // We panic here because if this will ever happen, then something's going certainly wrong.
panic(fmt.Sprint("a current number of listeners exceeded MaxInt which is", math.MaxInt)) panic(fmt.Sprint("a current number of listeners exceeded MaxInt64 which is", math.MaxInt))
} }
l.current++ l.current++
if l.current > l.peak { if l.current > l.peak {
@ -50,7 +50,7 @@ func (l *ListenerCounter) Inc() int {
// Dec decrements by 1 a current number of listeners. An error will occur if // Dec decrements by 1 a current number of listeners. An error will occur if
// a resulting number is less than 0. // a resulting number is less than 0.
func (l *ListenerCounter) Dec() (int, error) { func (l *ListenerCounter) Dec() (int64, error) {
l.mutex.Lock() l.mutex.Lock()
defer l.mutex.Unlock() defer l.mutex.Unlock()
if l.current == 0 { if l.current == 0 {
@ -64,8 +64,8 @@ func (l *ListenerCounter) MarshalJSON() ([]byte, error) {
l.mutex.RLock() l.mutex.RLock()
defer l.mutex.RUnlock() defer l.mutex.RUnlock()
return json.Marshal(&struct { return json.Marshal(&struct {
Current int `json:"current"` Current int64 `json:"current"`
Peak int `json:"peak"` Peak int64 `json:"peak"`
}{ }{
Current: l.current, Current: l.current,
Peak: l.peak}) Peak: l.peak})