Handle contracts.Initer in common.NewSyncSubject()

contracts.EntitiyFactoryFunc.WithInit() checked for
contracts.Initer every time.
Now it is only done once in common.NewSyncSubject().
This commit is contained in:
Eric Lippmann 2021-10-04 13:59:20 +02:00
parent c78326ad1b
commit 4d65c62f77
4 changed files with 18 additions and 16 deletions

View file

@ -181,7 +181,7 @@ func run() int {
g.Go(func() error {
defer configInitSync.Done()
return s.SyncAfterDump(synctx, common.NewSyncSubject(factory.WithInit), dump)
return s.SyncAfterDump(synctx, common.NewSyncSubject(factory), dump)
})
}
@ -192,7 +192,7 @@ func run() int {
g.Go(func() error {
defer stateInitSync.Done()
return s.SyncAfterDump(synctx, common.NewSyncSubject(factory.WithInit), dump)
return s.SyncAfterDump(synctx, common.NewSyncSubject(factory), dump)
})
}

View file

@ -15,11 +15,24 @@ type SyncSubject struct {
// NewSyncSubject returns a new SyncSubject.
func NewSyncSubject(factoryFunc contracts.EntityFactoryFunc) *SyncSubject {
e := factoryFunc()
var factory contracts.EntityFactoryFunc
if _, ok := e.(contracts.Initer); ok {
factory = func() contracts.Entity {
e := factoryFunc()
e.(contracts.Initer).Init()
return e
}
} else {
factory = factoryFunc
}
_, withChecksum := e.(contracts.Checksumer)
return &SyncSubject{
entity: e,
factory: factoryFunc,
factory: factory,
withChecksum: withChecksum,
}
}
@ -29,7 +42,7 @@ func (s SyncSubject) Entity() contracts.Entity {
return s.entity
}
// Factory returns the entity factory function.
// Factory returns the entity factory function that calls Init() on the created contracts.Entity if applicable.
func (s SyncSubject) Factory() contracts.EntityFactoryFunc {
return s.factory
}

View file

@ -49,17 +49,6 @@ type Checksumer interface {
// EntityFactoryFunc knows how to create an Entity.
type EntityFactoryFunc func() Entity
// WithInit calls Init() on the created Entity if applicable.
func (f EntityFactoryFunc) WithInit() Entity {
e := f()
if initer, ok := e.(Initer); ok {
initer.Init()
}
return e
}
// Waiter implements the Wait method,
// which blocks until execution is complete.
type Waiter interface {

View file

@ -61,7 +61,7 @@ func (r *RuntimeUpdates) Sync(ctx context.Context, factoryFuncs []contracts.Enti
updateMessagesByKey := make(map[string]chan<- redis.XMessage)
for _, factoryFunc := range factoryFuncs {
s := common.NewSyncSubject(factoryFunc.WithInit)
s := common.NewSyncSubject(factoryFunc)
updateMessages := make(chan redis.XMessage, bulkSize)
upsertEntities := make(chan contracts.Entity, bulkSize)