@@ -313,86 +313,81 @@ func (c *cache) update(res *registry.Result) {
313313// run starts the cache watcher loop
314314// it creates a new watcher if there's a problem
315315func (c * cache ) run (service string ) {
316- c .Lock ()
317- c .running = true
318- c .Unlock ()
319-
320316 // reset watcher on exit
321317 defer func () {
322318 c .Lock ()
323- c .watched = make (map [string ]bool )
324- c .running = false
319+ if _ , ok := c .watched [service ]; ok {
320+ delete (c .watched , service )
321+ }
325322 c .Unlock ()
326323 }()
327324
328- go func () {
329- var a , b int
325+ var a , b int
330326
331- for {
332- // exit early if already dead
327+ for {
328+ // exit early if already dead
329+ if c .quit () {
330+ return
331+ }
332+
333+ // jitter before starting
334+ j := rand .Int63n (100 )
335+ time .Sleep (time .Duration (j ) * time .Millisecond )
336+
337+ // create new watcher
338+ watchOption := func (wo * registry.WatchOptions ) {
339+ wo .Service = service
340+ }
341+ w , err := c .Registry .Watch (watchOption )
342+ if err != nil {
333343 if c .quit () {
334344 return
335345 }
336346
337- // jitter before starting
338- j := rand .Int63n (100 )
339- time .Sleep (time .Duration (j ) * time .Millisecond )
347+ d := backoff (a )
348+ c .setStatus (err )
340349
341- // create new watcher
342- watchOption := func (wo * registry.WatchOptions ) {
343- wo .Service = service
344- }
345- w , err := c .Registry .Watch (watchOption )
346- if err != nil {
347- if c .quit () {
348- return
350+ if a > 3 {
351+ if logger .V (logger .DebugLevel , logger .DefaultLogger ) {
352+ logger .Debug ("rcache: " , err , " backing off " , d )
349353 }
354+ a = 0
355+ }
350356
351- d := backoff ( a )
352- c . setStatus ( err )
357+ time . Sleep ( d )
358+ a ++
353359
354- if a > 3 {
355- if logger .V (logger .DebugLevel , logger .DefaultLogger ) {
356- logger .Debug ("rcache: " , err , " backing off " , d )
357- }
358- a = 0
359- }
360+ continue
361+ }
360362
361- time . Sleep ( d )
362- a ++
363+ // reset a
364+ a = 0
363365
364- continue
366+ // watch for events
367+ if err := c .watch (w ); err != nil {
368+ if c .quit () {
369+ return
365370 }
366371
367- // reset a
368- a = 0
369-
370- // watch for events
371- if err := c .watch (w ); err != nil {
372- if c .quit () {
373- return
374- }
375-
376- d := backoff (b )
377- c .setStatus (err )
372+ d := backoff (b )
373+ c .setStatus (err )
378374
379- if b > 3 {
380- if logger .V (logger .DebugLevel , logger .DefaultLogger ) {
381- logger .Debug ("rcache: " , err , " backing off " , d )
382- }
383- b = 0
375+ if b > 3 {
376+ if logger .V (logger .DebugLevel , logger .DefaultLogger ) {
377+ logger .Debug ("rcache: " , err , " backing off " , d )
384378 }
385-
386- time .Sleep (d )
387- b ++
388-
389- continue
379+ b = 0
390380 }
391381
392- // reset b
393- b = 0
382+ time .Sleep (d )
383+ b ++
384+
385+ continue
394386 }
395- }()
387+
388+ // reset b
389+ b = 0
390+ }
396391}
397392
398393// watch loops the next event and calls update
0 commit comments