1
0

Let's actually hide RWMutex's methods to clean a namespace.

This commit is contained in:
Alexander Andreev 2023-10-09 00:21:44 +04:00
parent 5de3f8e9af
commit c9e30f76e6
Signed by: Arav
GPG Key ID: D22A817D95815393
4 changed files with 32 additions and 32 deletions

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.RWMutex mutex sync.RWMutex
current, peak int current, peak int
} }
@ -21,22 +21,22 @@ 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.RLock() l.mutex.RLock()
defer l.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() int {
l.RLock() l.mutex.RLock()
defer l.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() int {
l.Lock() l.mutex.Lock()
defer l.Unlock() defer l.mutex.Unlock()
if l.current == math.MaxInt { if l.current == math.MaxInt {
// 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 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 // 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() (int, error) {
l.Lock() l.mutex.Lock()
defer l.Unlock() defer l.mutex.Unlock()
if l.current == 0 { if l.current == 0 {
return l.current, errors.New("an attempt to decrement a number of current listeners down to less than 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) { func (l *ListenerCounter) MarshalJSON() ([]byte, error) {
l.RLock() l.mutex.RLock()
defer l.RUnlock() defer l.mutex.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"`

View File

@ -13,7 +13,7 @@ const MostListenedDateFormat string = "02 January 2006 at 15:04:05 MST"
// MostListenedSong holds a metadata for a most listened song. // MostListenedSong holds a metadata for a most listened song.
type MostListenedSong struct { type MostListenedSong struct {
sync.RWMutex mutex sync.RWMutex
Date time.Time `json:"date"` Date time.Time `json:"date"`
Listeners int `json:"listeners"` Listeners int `json:"listeners"`
Song string `json:"song"` Song string `json:"song"`
@ -21,8 +21,8 @@ type MostListenedSong struct {
} }
func (mls *MostListenedSong) Update(song Song) { func (mls *MostListenedSong) Update(song Song) {
mls.Lock() mls.mutex.Lock()
defer mls.Unlock() defer mls.mutex.Unlock()
if song.Artist == "" { if song.Artist == "" {
return return
} }
@ -35,8 +35,8 @@ func (mls *MostListenedSong) Update(song Song) {
} }
func (mls *MostListenedSong) Get() *MostListenedSong { func (mls *MostListenedSong) Get() *MostListenedSong {
mls.RLock() mls.mutex.RLock()
defer mls.RUnlock() defer mls.mutex.RUnlock()
if mls.Date.Year() == 1 { if mls.Date.Year() == 1 {
return nil return nil
@ -50,8 +50,8 @@ func (mls *MostListenedSong) Get() *MostListenedSong {
// Load parses given data and fill a MostListenedSong. // Load parses given data and fill a MostListenedSong.
func (mls *MostListenedSong) Load(data []byte) (err error) { func (mls *MostListenedSong) Load(data []byte) (err error) {
mls.Lock() mls.mutex.Lock()
defer mls.Unlock() defer mls.mutex.Unlock()
lines := bytes.Split(data, []byte{'\n'}) lines := bytes.Split(data, []byte{'\n'})
if len(lines) != 3 { if len(lines) != 3 {

View File

@ -10,7 +10,7 @@ import (
// Playlist holds a list of paths to a song files. // Playlist holds a list of paths to a song files.
type Playlist struct { type Playlist struct {
sync.Mutex mutex sync.Mutex
filePath string filePath string
playlist []string playlist []string
cur int 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 // Next returns the next song to play. Returns an empty string if repeat is
// false and the end of a playlist was reached. // false and the end of a playlist was reached.
func (p *Playlist) Next() (song string) { func (p *Playlist) Next() (song string) {
p.Lock() p.mutex.Lock()
defer p.Unlock() defer p.mutex.Unlock()
if p.cur == len(p.playlist) { if p.cur == len(p.playlist) {
// If the end of a playlist was reached and repeat is set to true, // 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 // 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") return errors.New("a playlist file is empty. Did not update")
} }
p.Lock() p.mutex.Lock()
p.playlist = strings.Split(string(data), "\n") p.playlist = strings.Split(string(data), "\n")
p.cur = 0 p.cur = 0
p.Unlock() p.mutex.Unlock()
return nil return nil
} }

View File

@ -42,7 +42,7 @@ func (s *Song) MarshalJSON() ([]byte, error) {
// SongList holds a currently playing song and a list of previously played ones // SongList holds a currently playing song and a list of previously played ones
// with a maximal length of maxLen. // with a maximal length of maxLen.
type SongList struct { type SongList struct {
sync.RWMutex mutex sync.RWMutex
current Song current Song
lastSongs []Song lastSongs []Song
maxLen int maxLen int
@ -55,8 +55,8 @@ func NewSongList(maxLen int) *SongList {
// Add a new song that is currently playing and update a list. // Add a new song that is currently playing and update a list.
func (sl *SongList) Add(newSong Song) { func (sl *SongList) Add(newSong Song) {
sl.Lock() sl.mutex.Lock()
defer sl.Unlock() defer sl.mutex.Unlock()
if sl.current.StartAt.Year() == 1 { if sl.current.StartAt.Year() == 1 {
sl.current = newSong 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. // Current returns a currently playing song or nil if it isn't set yet.
func (sl *SongList) Current() *Song { func (sl *SongList) Current() *Song {
sl.RLock() sl.mutex.RLock()
defer sl.RUnlock() defer sl.mutex.RUnlock()
if sl.current.StartAt.Year() == 1 { if sl.current.StartAt.Year() == 1 {
return nil return nil
} }
@ -89,8 +89,8 @@ func (sl *SongList) Current() *Song {
// UpdateCurrentMaxListeners checks and updates a maximal number of listeners // UpdateCurrentMaxListeners checks and updates a maximal number of listeners
// for a current song. // for a current song.
func (sl *SongList) UpdateCurrentMaxListeners(listeners int) { func (sl *SongList) UpdateCurrentMaxListeners(listeners int) {
sl.Lock() sl.mutex.Lock()
defer sl.Unlock() defer sl.mutex.Unlock()
if listeners > sl.current.MaxListeners { if listeners > sl.current.MaxListeners {
sl.current.MaxListeners = listeners sl.current.MaxListeners = listeners
} }
@ -98,8 +98,8 @@ func (sl *SongList) UpdateCurrentMaxListeners(listeners int) {
// List returns a list of lastly played songs. // List returns a list of lastly played songs.
func (sl *SongList) List() []Song { func (sl *SongList) List() []Song {
sl.RLock() sl.mutex.RLock()
defer sl.RUnlock() defer sl.mutex.RUnlock()
return sl.lastSongs return sl.lastSongs
} }