Exemple #1
0
func NewEtcdWatcher(baseKey string, urls ...string) Watcher {
	client := etcd.NewClient(urls)

	ew := etcdWatcher{
		client:         client,
		baseKey:        baseKey,
		serviceChannel: make(chan ServiceUpdate),
		listeners:      make(map[string][]chan ServiceUpdate),
	}

	go util.Forever(func() { ew.watchEtcdForChanges(baseKey) }, time.Second)
	go util.Forever(func() { ew.handleUpdates() }, time.Second)

	return ew
}
Exemple #2
0
// NewClient returns a new Client to handle requests to the
// service at the other end of the connection.
// A watcher is used to get updates about service endpoints
func NewClient(serviceName string, watcher watch.Watcher) *Client {
	// Dial with no addresses means adding no endpoints
	client, err := Dial()
	if err != nil {
		glog.Fatal(err)
	}

	// Handle updates about service endpoints
	serviceChannel := make(chan watch.ServiceUpdate)
	go util.Forever(func() {
		serviceUpdate := <-serviceChannel
		switch serviceUpdate.Op {
		case watch.ADD:
			if err := client.endpoints.add(serviceUpdate.Value); err != nil {
				glog.Error(err)
			}
		case watch.REMOVE:
			if err := client.endpoints.del(serviceUpdate.Value); err != nil {
				glog.Error(err)
			}
		default:
			glog.Warning("zrpc: unknown service update op")
		}
	}, time.Second)

	// Register as a listener for passed service name
	watcher.Watch(serviceName, serviceChannel)

	return client
}