From c9e30f76e67581249d2935d7f993c81e70a47a37 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Mon, 9 Oct 2023 00:21:44 +0400 Subject: [PATCH] Let's actually hide RWMutex's methods to clean a namespace. --- internal/radio/listener_counter.go | 22 +++++++++++----------- internal/radio/mostlistened.go | 14 +++++++------- internal/radio/playlist.go | 10 +++++----- internal/radio/song.go | 18 +++++++++--------- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/internal/radio/listener_counter.go b/internal/radio/listener_counter.go index 8948799..e3d3839 100644 --- a/internal/radio/listener_counter.go +++ b/internal/radio/listener_counter.go @@ -10,7 +10,7 @@ import ( // ListenerCounter stores the current and peak numbers of listeners. type ListenerCounter struct { - sync.RWMutex + mutex sync.RWMutex current, peak int } @@ -21,22 +21,22 @@ func NewListenerCounter() *ListenerCounter { // Current returns a number of current listeners. func (l *ListenerCounter) Current() int { - l.RLock() - defer l.RUnlock() + l.mutex.RLock() + defer l.mutex.RUnlock() return l.current } // Current returns a number of peak listeners. func (l *ListenerCounter) Peak() int { - l.RLock() - defer l.RUnlock() + l.mutex.RLock() + defer l.mutex.RUnlock() return l.peak } // Inc increments by 1 a current number of listeners and updates a peak number. func (l *ListenerCounter) Inc() int { - l.Lock() - defer l.Unlock() + l.mutex.Lock() + defer l.mutex.Unlock() if l.current == math.MaxInt { // 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)) @@ -51,8 +51,8 @@ func (l *ListenerCounter) Inc() int { // Dec decrements by 1 a current number of listeners. An error will occur if // a resulting number is less than 0. func (l *ListenerCounter) Dec() (int, error) { - l.Lock() - defer l.Unlock() + l.mutex.Lock() + defer l.mutex.Unlock() if l.current == 0 { return l.current, errors.New("an attempt to decrement a number of current listeners down to less than 0") } @@ -61,8 +61,8 @@ func (l *ListenerCounter) Dec() (int, error) { } func (l *ListenerCounter) MarshalJSON() ([]byte, error) { - l.RLock() - defer l.RUnlock() + l.mutex.RLock() + defer l.mutex.RUnlock() return json.Marshal(&struct { Current int `json:"current"` Peak int `json:"peak"` diff --git a/internal/radio/mostlistened.go b/internal/radio/mostlistened.go index c5d1b7f..8442a1c 100644 --- a/internal/radio/mostlistened.go +++ b/internal/radio/mostlistened.go @@ -13,7 +13,7 @@ const MostListenedDateFormat string = "02 January 2006 at 15:04:05 MST" // MostListenedSong holds a metadata for a most listened song. type MostListenedSong struct { - sync.RWMutex + mutex sync.RWMutex Date time.Time `json:"date"` Listeners int `json:"listeners"` Song string `json:"song"` @@ -21,8 +21,8 @@ type MostListenedSong struct { } func (mls *MostListenedSong) Update(song Song) { - mls.Lock() - defer mls.Unlock() + mls.mutex.Lock() + defer mls.mutex.Unlock() if song.Artist == "" { return } @@ -35,8 +35,8 @@ func (mls *MostListenedSong) Update(song Song) { } func (mls *MostListenedSong) Get() *MostListenedSong { - mls.RLock() - defer mls.RUnlock() + mls.mutex.RLock() + defer mls.mutex.RUnlock() if mls.Date.Year() == 1 { return nil @@ -50,8 +50,8 @@ func (mls *MostListenedSong) Get() *MostListenedSong { // Load parses given data and fill a MostListenedSong. func (mls *MostListenedSong) Load(data []byte) (err error) { - mls.Lock() - defer mls.Unlock() + mls.mutex.Lock() + defer mls.mutex.Unlock() lines := bytes.Split(data, []byte{'\n'}) if len(lines) != 3 { diff --git a/internal/radio/playlist.go b/internal/radio/playlist.go index 52a0ebe..669e8f4 100644 --- a/internal/radio/playlist.go +++ b/internal/radio/playlist.go @@ -10,7 +10,7 @@ import ( // Playlist holds a list of paths to a song files. type Playlist struct { - sync.Mutex + mutex sync.Mutex filePath string playlist []string cur int @@ -27,8 +27,8 @@ func NewPlaylist(filePath string, repeat bool) (*Playlist, error) { // Next returns the next song to play. Returns an empty string if repeat is // false and the end of a playlist was reached. func (p *Playlist) Next() (song string) { - p.Lock() - defer p.Unlock() + p.mutex.Lock() + defer p.mutex.Unlock() if p.cur == len(p.playlist) { // If the end of a playlist was reached and repeat is set to true, // then go back to the head of it, thus repeating it. Return an empty @@ -55,10 +55,10 @@ func (p *Playlist) load() error { return errors.New("a playlist file is empty. Did not update") } - p.Lock() + p.mutex.Lock() p.playlist = strings.Split(string(data), "\n") p.cur = 0 - p.Unlock() + p.mutex.Unlock() return nil } diff --git a/internal/radio/song.go b/internal/radio/song.go index ba4ef47..cddf89e 100644 --- a/internal/radio/song.go +++ b/internal/radio/song.go @@ -42,7 +42,7 @@ func (s *Song) MarshalJSON() ([]byte, error) { // SongList holds a currently playing song and a list of previously played ones // with a maximal length of maxLen. type SongList struct { - sync.RWMutex + mutex sync.RWMutex current Song lastSongs []Song maxLen int @@ -55,8 +55,8 @@ func NewSongList(maxLen int) *SongList { // Add a new song that is currently playing and update a list. func (sl *SongList) Add(newSong Song) { - sl.Lock() - defer sl.Unlock() + sl.mutex.Lock() + defer sl.mutex.Unlock() if sl.current.StartAt.Year() == 1 { sl.current = newSong @@ -73,8 +73,8 @@ func (sl *SongList) Add(newSong Song) { // Current returns a currently playing song or nil if it isn't set yet. func (sl *SongList) Current() *Song { - sl.RLock() - defer sl.RUnlock() + sl.mutex.RLock() + defer sl.mutex.RUnlock() if sl.current.StartAt.Year() == 1 { return nil } @@ -89,8 +89,8 @@ func (sl *SongList) Current() *Song { // UpdateCurrentMaxListeners checks and updates a maximal number of listeners // for a current song. func (sl *SongList) UpdateCurrentMaxListeners(listeners int) { - sl.Lock() - defer sl.Unlock() + sl.mutex.Lock() + defer sl.mutex.Unlock() if listeners > sl.current.MaxListeners { sl.current.MaxListeners = listeners } @@ -98,8 +98,8 @@ func (sl *SongList) UpdateCurrentMaxListeners(listeners int) { // List returns a list of lastly played songs. func (sl *SongList) List() []Song { - sl.RLock() - defer sl.RUnlock() + sl.mutex.RLock() + defer sl.mutex.RUnlock() return sl.lastSongs }