Skip to content

Commit

Permalink
Refactor fallback profile listener to avoid repetition (#2228)
Browse files Browse the repository at this point in the history
Refactor fallback profile listener to avoid repetition

Signed-off-by: Alex Leong <alex@buoyant.io>
  • Loading branch information
adleong authored Feb 8, 2019
1 parent 5ef8f4d commit 030767d
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions controller/api/proxy/fallback_profile_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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()
}

0 comments on commit 030767d

Please sign in to comment.