mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-01 19:47:37 +03:00
Simplify Singleton usage by leveraging Go 1.18's generics
This commit is contained in:
parent
a2d9aaeff8
commit
d613b19306
6 changed files with 45 additions and 38 deletions
|
@ -45,7 +45,7 @@ type playTracker struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPlayTracker(ds model.DataStore, broker events.Broker) PlayTracker {
|
func GetPlayTracker(ds model.DataStore, broker events.Broker) PlayTracker {
|
||||||
instance := singleton.Get(playTracker{}, func() interface{} {
|
return singleton.GetInstance(func() *playTracker {
|
||||||
m := ttlcache.NewCache()
|
m := ttlcache.NewCache()
|
||||||
m.SkipTTLExtensionOnHit(true)
|
m.SkipTTLExtensionOnHit(true)
|
||||||
_ = m.SetTTL(nowPlayingExpire)
|
_ = m.SetTTL(nowPlayingExpire)
|
||||||
|
@ -60,7 +60,6 @@ func GetPlayTracker(ds model.DataStore, broker events.Broker) PlayTracker {
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
})
|
})
|
||||||
return instance.(*playTracker)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *playTracker) NowPlaying(ctx context.Context, playerId string, playerName string, trackId string) error {
|
func (p *playTracker) NowPlaying(ctx context.Context, playerId string, playerName string, trackId string) error {
|
||||||
|
|
3
db/db.go
3
db/db.go
|
@ -19,7 +19,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Db() *sql.DB {
|
func Db() *sql.DB {
|
||||||
instance := singleton.Get(&sql.DB{}, func() interface{} {
|
return singleton.GetInstance(func() *sql.DB {
|
||||||
Path = conf.Server.DbPath
|
Path = conf.Server.DbPath
|
||||||
if Path == ":memory:" {
|
if Path == ":memory:" {
|
||||||
Path = "file::memory:?cache=shared&_foreign_keys=on"
|
Path = "file::memory:?cache=shared&_foreign_keys=on"
|
||||||
|
@ -32,7 +32,6 @@ func Db() *sql.DB {
|
||||||
}
|
}
|
||||||
return instance
|
return instance
|
||||||
})
|
})
|
||||||
return instance.(*sql.DB)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnsureLatestVersion() {
|
func EnsureLatestVersion() {
|
||||||
|
|
|
@ -13,13 +13,12 @@ type Scheduler interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetInstance() Scheduler {
|
func GetInstance() Scheduler {
|
||||||
instance := singleton.Get(&scheduler{}, func() interface{} {
|
return singleton.GetInstance(func() *scheduler {
|
||||||
c := cron.New(cron.WithLogger(&logger{}))
|
c := cron.New(cron.WithLogger(&logger{}))
|
||||||
return &scheduler{
|
return &scheduler{
|
||||||
c: c,
|
c: c,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return instance.(*scheduler)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type scheduler struct {
|
type scheduler struct {
|
||||||
|
|
|
@ -65,7 +65,7 @@ type broker struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBroker() Broker {
|
func GetBroker() Broker {
|
||||||
instance := singleton.Get(&broker{}, func() interface{} {
|
return singleton.GetInstance(func() *broker {
|
||||||
// Instantiate a broker
|
// Instantiate a broker
|
||||||
broker := &broker{
|
broker := &broker{
|
||||||
publish: make(messageChan, 2),
|
publish: make(messageChan, 2),
|
||||||
|
@ -77,8 +77,6 @@ func GetBroker() Broker {
|
||||||
go broker.listen()
|
go broker.listen()
|
||||||
return broker
|
return broker
|
||||||
})
|
})
|
||||||
|
|
||||||
return instance.(*broker)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *broker) SendMessage(ctx context.Context, evt Event) {
|
func (b *broker) SendMessage(ctx context.Context, evt Event) {
|
||||||
|
|
|
@ -1,33 +1,37 @@
|
||||||
package singleton
|
package singleton
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/navidrome/navidrome/log"
|
"github.com/navidrome/navidrome/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
instances = make(map[string]interface{})
|
instances = make(map[string]any)
|
||||||
getOrCreateC = make(chan *entry, 1)
|
getOrCreateC = make(chan entry)
|
||||||
)
|
)
|
||||||
|
|
||||||
type entry struct {
|
type entry struct {
|
||||||
constructor func() interface{}
|
f func() any
|
||||||
object interface{}
|
object any
|
||||||
resultC chan interface{}
|
resultC chan any
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns an existing instance of object. If it is not yet created, calls `constructor`, stores the
|
// GetInstance returns an existing instance of object. If it is not yet created, calls `constructor`, stores the
|
||||||
// result for future calls and return it
|
// result for future calls and return it
|
||||||
func Get(object interface{}, constructor func() interface{}) interface{} {
|
func GetInstance[T any](constructor func() T) T {
|
||||||
e := &entry{
|
var t T
|
||||||
constructor: constructor,
|
e := entry{
|
||||||
object: object,
|
object: t,
|
||||||
resultC: make(chan interface{}),
|
f: func() any {
|
||||||
|
return constructor()
|
||||||
|
},
|
||||||
|
resultC: make(chan any),
|
||||||
}
|
}
|
||||||
getOrCreateC <- e
|
getOrCreateC <- e
|
||||||
return <-e.resultC
|
v := <-e.resultC
|
||||||
|
return v.(T)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -35,11 +39,10 @@ func init() {
|
||||||
for {
|
for {
|
||||||
e := <-getOrCreateC
|
e := <-getOrCreateC
|
||||||
name := reflect.TypeOf(e.object).String()
|
name := reflect.TypeOf(e.object).String()
|
||||||
name = strings.TrimPrefix(name, "*")
|
|
||||||
v, created := instances[name]
|
v, created := instances[name]
|
||||||
if !created {
|
if !created {
|
||||||
v = e.constructor()
|
v = e.f()
|
||||||
log.Trace("Created new singleton", "object", name, "instance", v)
|
log.Trace("Created new singleton", "type", name, "instance", fmt.Sprintf("%+v", v))
|
||||||
instances[name] = v
|
instances[name] = v
|
||||||
}
|
}
|
||||||
e.resultC <- v
|
e.resultC <- v
|
||||||
|
|
|
@ -17,38 +17,43 @@ func TestSingleton(t *testing.T) {
|
||||||
RunSpecs(t, "Singleton Suite")
|
RunSpecs(t, "Singleton Suite")
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ = Describe("Get", func() {
|
var _ = Describe("GetInstance", func() {
|
||||||
type T struct{ id string }
|
type T struct{ id string }
|
||||||
var numInstances int
|
var numInstances int
|
||||||
constructor := func() interface{} {
|
constructor := func() *T {
|
||||||
numInstances++
|
numInstances++
|
||||||
return &T{id: uuid.NewString()}
|
return &T{id: uuid.NewString()}
|
||||||
}
|
}
|
||||||
|
|
||||||
It("calls the constructor to create a new instance", func() {
|
It("calls the constructor to create a new instance", func() {
|
||||||
instance := singleton.Get(T{}, constructor)
|
instance := singleton.GetInstance(constructor)
|
||||||
Expect(numInstances).To(Equal(1))
|
Expect(numInstances).To(Equal(1))
|
||||||
Expect(instance).To(BeAssignableToTypeOf(&T{}))
|
Expect(instance).To(BeAssignableToTypeOf(&T{}))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("does not call the constructor the next time", func() {
|
It("does not call the constructor the next time", func() {
|
||||||
instance := singleton.Get(T{}, constructor)
|
instance := singleton.GetInstance(constructor)
|
||||||
newInstance := singleton.Get(T{}, constructor)
|
newInstance := singleton.GetInstance(constructor)
|
||||||
|
|
||||||
Expect(newInstance.(*T).id).To(Equal(instance.(*T).id))
|
Expect(newInstance.id).To(Equal(instance.id))
|
||||||
Expect(numInstances).To(Equal(1))
|
Expect(numInstances).To(Equal(1))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("does not call the constructor even if a pointer is passed as the object", func() {
|
It("makes a distinction between a type and its pointer", func() {
|
||||||
instance := singleton.Get(T{}, constructor)
|
instance := singleton.GetInstance(constructor)
|
||||||
newInstance := singleton.Get(&T{}, constructor)
|
newInstance := singleton.GetInstance(func() T {
|
||||||
|
numInstances++
|
||||||
|
return T{id: uuid.NewString()}
|
||||||
|
})
|
||||||
|
|
||||||
Expect(newInstance.(*T).id).To(Equal(instance.(*T).id))
|
Expect(instance).To(BeAssignableToTypeOf(&T{}))
|
||||||
Expect(numInstances).To(Equal(1))
|
Expect(newInstance).To(BeAssignableToTypeOf(T{}))
|
||||||
|
Expect(newInstance.id).ToNot(Equal(instance.id))
|
||||||
|
Expect(numInstances).To(Equal(2))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("only calls the constructor once when called concurrently", func() {
|
It("only calls the constructor once when called concurrently", func() {
|
||||||
const maxCalls = 2000
|
const maxCalls = 20000
|
||||||
var numCalls int32
|
var numCalls int32
|
||||||
start := sync.WaitGroup{}
|
start := sync.WaitGroup{}
|
||||||
start.Add(1)
|
start.Add(1)
|
||||||
|
@ -56,10 +61,14 @@ var _ = Describe("Get", func() {
|
||||||
prepare.Add(maxCalls)
|
prepare.Add(maxCalls)
|
||||||
done := sync.WaitGroup{}
|
done := sync.WaitGroup{}
|
||||||
done.Add(maxCalls)
|
done.Add(maxCalls)
|
||||||
|
numInstances = 0
|
||||||
for i := 0; i < maxCalls; i++ {
|
for i := 0; i < maxCalls; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
start.Wait()
|
start.Wait()
|
||||||
singleton.Get(T{}, constructor)
|
singleton.GetInstance(func() struct{ I int } {
|
||||||
|
numInstances++
|
||||||
|
return struct{ I int }{I: 1}
|
||||||
|
})
|
||||||
atomic.AddInt32(&numCalls, 1)
|
atomic.AddInt32(&numCalls, 1)
|
||||||
done.Done()
|
done.Done()
|
||||||
}()
|
}()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue