Refactor fallback profile listener to avoid repetition (#2228)

Refactor fallback profile listener to avoid repetition

Signed-off-by: Alex Leong <alex@buoyant.io>
This commit is contained in:
Alex Leong 2019-02-08 14:24:10 -08:00 committed by GitHub
parent 5ef8f4da44
commit 030767d615
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 33 deletions

View File

@ -13,39 +13,58 @@ type fallbackProfileListener struct {
mutex sync.Mutex
}
type primaryProfileListener struct {
type fallbackChildListener struct {
state *sp.ServiceProfile
parent *fallbackProfileListener
}
type primaryProfileListener struct {
fallbackChildListener
}
type backupProfileListener struct {
state *sp.ServiceProfile
parent *fallbackProfileListener
fallbackChildListener
}
// newFallbackProfileListener takes an underlying profileUpdateListener and
// returns two profileUpdateListeners: a primary and a secondary. Updates to
// the primary and secondary will propagate to the underlying with updates to
// returns two profileUpdateListeners: a primary and a backup. Updates to
// the primary and backup will propagate to the underlying with updates to
// the primary always taking priority. If the value in the primary is cleared,
// the value from the secondary is used.
// the value from the backup is used.
func newFallbackProfileListener(listener profileUpdateListener) (profileUpdateListener, profileUpdateListener) {
// Primary and secondary share a lock to ensure updates are atomic.
// Primary and backup share a lock to ensure updates are atomic.
fallback := fallbackProfileListener{
mutex: sync.Mutex{},
underlying: listener,
}
primary := primaryProfileListener{
parent: &fallback,
fallbackChildListener{
parent: &fallback,
},
}
backup := backupProfileListener{
parent: &fallback,
fallbackChildListener{
parent: &fallback,
},
}
fallback.primary = &primary
fallback.backup = &backup
return &primary, &backup
}
func (f *fallbackChildListener) ClientClose() <-chan struct{} {
return f.parent.underlying.ClientClose()
}
func (f *fallbackChildListener) ServerClose() <-chan struct{} {
return f.parent.underlying.ServerClose()
}
func (f *fallbackChildListener) Stop() {
f.parent.underlying.Stop()
}
// Primary
func (p *primaryProfileListener) Update(profile *sp.ServiceProfile) {
@ -68,18 +87,6 @@ func (p *primaryProfileListener) Update(profile *sp.ServiceProfile) {
p.parent.underlying.Update(nil)
}
func (p *primaryProfileListener) ClientClose() <-chan struct{} {
return p.parent.underlying.ClientClose()
}
func (p *primaryProfileListener) ServerClose() <-chan struct{} {
return p.parent.underlying.ServerClose()
}
func (p *primaryProfileListener) Stop() {
p.parent.underlying.Stop()
}
// Backup
func (b *backupProfileListener) Update(profile *sp.ServiceProfile) {
@ -100,15 +107,3 @@ func (b *backupProfileListener) Update(profile *sp.ServiceProfile) {
// Our value was cleared and there is no primary value.
b.parent.underlying.Update(nil)
}
func (b *backupProfileListener) ClientClose() <-chan struct{} {
return b.parent.underlying.ClientClose()
}
func (b *backupProfileListener) ServerClose() <-chan struct{} {
return b.parent.underlying.ServerClose()
}
func (b *backupProfileListener) Stop() {
b.parent.underlying.Stop()
}