Let's actually hide RWMutex's methods to clean a namespace.
This commit is contained in:
parent
5de3f8e9af
commit
c9e30f76e6
@ -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"`
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user