@@ -33,7 +33,7 @@ type Watch struct {
3333
3434 // lock for read/writing the endpoints slice
3535 lock sync.RWMutex
36- endpoints []string
36+ endpoints []Entity
3737}
3838
3939// Watch creates a new watch on this server set. Changes to the set will
@@ -121,6 +121,19 @@ func (ss *ServerSet) Watch() (*Watch, error) {
121121func (w * Watch ) Endpoints () []string {
122122 w .lock .RLock ()
123123 defer w .lock .RUnlock ()
124+ endpoints := make ([]string , 0 , len (w .endpoints ))
125+ for _ , e := range w .endpoints {
126+ endpoints = append (endpoints , net .JoinHostPort (e .ServiceEndpoint .Host , strconv .Itoa (e .ServiceEndpoint .Port )))
127+ }
128+
129+ sort .Strings (endpoints )
130+ return endpoints
131+ }
132+
133+ // EndpointEntities returns a slice of the current list of Entites associated with this watch, collected at the last event.
134+ func (w * Watch ) EndpointEntities () []Entity {
135+ w .lock .RLock ()
136+ defer w .lock .RUnlock ()
124137
125138 return w .endpoints
126139}
@@ -172,8 +185,8 @@ func (w *Watch) watch(connection *zk.Conn) ([]string, <-chan zk.Event, error) {
172185 return children , events , err
173186}
174187
175- func (w * Watch ) updateEndpoints (connection * zk.Conn , keys []string ) ([]string , error ) {
176- endpoints := make ([]string , 0 , len (keys ))
188+ func (w * Watch ) updateEndpoints (connection * zk.Conn , keys []string ) ([]Entity , error ) {
189+ endpoints := make ([]Entity , 0 , len (keys ))
177190
178191 for _ , k := range keys {
179192 if ! strings .HasPrefix (k , MemberPrefix ) {
@@ -191,16 +204,15 @@ func (w *Watch) updateEndpoints(connection *zk.Conn, keys []string) ([]string, e
191204 }
192205
193206 if e .Status == statusAlive {
194- endpoints = append (endpoints , net . JoinHostPort ( e . ServiceEndpoint . Host , strconv . Itoa ( e . ServiceEndpoint . Port )) )
207+ endpoints = append (endpoints , * e )
195208 }
196209 }
197210
198- sort .Strings (endpoints )
199211 return endpoints , nil
200212
201213}
202214
203- func (w * Watch ) getEndpoint (connection * zk.Conn , key string ) (* entity , error ) {
215+ func (w * Watch ) getEndpoint (connection * zk.Conn , key string ) (* Entity , error ) {
204216
205217 data , _ , err := connection .Get (w .serverSet .directoryPath () + "/" + key )
206218 if err == zk .ErrNoNode {
@@ -221,7 +233,7 @@ func (w *Watch) getEndpoint(connection *zk.Conn, key string) (*entity, error) {
221233 return w .getEndpoint (connection , key )
222234 }
223235
224- e := & entity {}
236+ e := & Entity {}
225237 err = json .Unmarshal (data , & e )
226238 if err != nil {
227239 return nil , err
0 commit comments