-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yuri Shkuro
committed
Mar 8, 2017
1 parent
212ef2c
commit 39f0c3e
Showing
7 changed files
with
259 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cache | ||
|
||
import ( | ||
"github.com/go-kit/kit/endpoint" | ||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/sd" | ||
) | ||
|
||
// TODO if Cache is moved to endpoint/cache, then this can be renamed to Cache. | ||
|
||
// Observer subscribes to updates from a service discovery system, | ||
// creates endpoints for them using a factory, and makes them available | ||
// to consumers. It implements Subscriber and Discoverer interfaces. | ||
type Observer struct { | ||
cache *Cache | ||
notifications chan []string // used to receive notifications | ||
notifier sd.Notifier | ||
} | ||
|
||
var _ sd.Subscriber = &Observer{} // API check | ||
var _ sd.Discoverer = &Observer{} // API check | ||
|
||
// NewObserver crates a new Observer. | ||
func NewObserver( | ||
discoverer sd.Discoverer, | ||
notifier sd.Notifier, | ||
factory sd.Factory, | ||
logger log.Logger, | ||
) *Observer { | ||
obs := &Observer{ | ||
cache: New(factory, logger), | ||
notifier: notifier, | ||
notifications: make(chan []string, 10), | ||
} | ||
|
||
go obs.observe() | ||
notifier.Register(obs.notifications) | ||
|
||
// TBD - a bit of a race condition here if notifier and discoverer are not in sync | ||
|
||
instances, err := discoverer.Instances() | ||
if err == nil { | ||
logger.Log("instances", len(instances)) | ||
} else { | ||
logger.Log("err", err) | ||
} | ||
obs.cache.Update(instances) | ||
|
||
return obs | ||
} | ||
|
||
// Close stops the observer. | ||
func (o *Observer) Close() { | ||
o.notifier.Unregister(o.notifications) | ||
close(o.notifications) | ||
} | ||
|
||
// Instances implements the Discoverer interface. | ||
func (o *Observer) Instances() ([]string, error) { | ||
return o.cache.Instances(), nil | ||
} | ||
|
||
// Endpoints implements the Subscriber interface. | ||
func (o *Observer) Endpoints() ([]endpoint.Endpoint, error) { | ||
return o.cache.Endpoints(), nil | ||
} | ||
|
||
func (o *Observer) observe() { | ||
for instances := range o.notifications { | ||
o.cache.Update(instances) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.