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