1
0

In ListenerCounter a sync.Mutex was replaced by a sync.RWMutex.

This commit is contained in:
Alexander Andreev 2023-10-07 23:21:00 +04:00
parent da4e97f1aa
commit d84d985962
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -10,7 +10,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 {
sync.Mutex sync.RWMutex
current, peak int current, peak int
} }
@ -21,15 +21,15 @@ 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() int {
l.Lock() l.RLock()
defer l.Unlock() defer l.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() int {
l.Lock() l.RLock()
defer l.Unlock() defer l.RUnlock()
return l.peak return l.peak
} }
@ -61,8 +61,8 @@ func (l *ListenerCounter) Dec() (int, error) {
} }
func (l *ListenerCounter) MarshalJSON() ([]byte, error) { func (l *ListenerCounter) MarshalJSON() ([]byte, error) {
l.Lock() l.RLock()
defer l.Unlock() defer l.RUnlock()
return json.Marshal(&struct { return json.Marshal(&struct {
Current int `json:"current"` Current int `json:"current"`
Peak int `json:"peak"` Peak int `json:"peak"`