TODO watchAndSendKiteEvents takes too many arguments. Refactor it.
( watcher *Watcher, watcherID string, disconnect chan bool, etcdKey string, callback dnode.Function, token string, hasConstraint bool, constraint version.Constraints, keyRest string, )
| 35 | |
| 36 | // TODO watchAndSendKiteEvents takes too many arguments. Refactor it. |
| 37 | func (k *Kontrol) watchAndSendKiteEvents( |
| 38 | watcher *Watcher, |
| 39 | watcherID string, |
| 40 | disconnect chan bool, |
| 41 | etcdKey string, |
| 42 | callback dnode.Function, |
| 43 | token string, |
| 44 | hasConstraint bool, |
| 45 | constraint version.Constraints, |
| 46 | keyRest string, |
| 47 | ) { |
| 48 | var index uint64 = 0 |
| 49 | for { |
| 50 | select { |
| 51 | case <-disconnect: |
| 52 | return |
| 53 | case resp, ok := <-watcher.recv: |
| 54 | // Channel is closed. This happens in 3 cases: |
| 55 | // 1. Remote kite called "cancelWatcher" method and removed the watcher. |
| 56 | // 2. Remote kite has disconnected and the watcher is removed. |
| 57 | // 3. Remote kite couldn't consume messages fast enough, buffer |
| 58 | // has filled up and etcd cancelled the watcher. |
| 59 | if !ok { |
| 60 | // Do not try again if watcher is cancelled. |
| 61 | k.watchersMutex.Lock() |
| 62 | if _, ok := k.watchers[watcherID]; !ok { |
| 63 | k.watchersMutex.Unlock() |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | // Do not try again if disconnected. |
| 68 | select { |
| 69 | case <-disconnect: |
| 70 | k.watchersMutex.Unlock() |
| 71 | return |
| 72 | default: |
| 73 | } |
| 74 | k.watchersMutex.Unlock() |
| 75 | |
| 76 | // If we are here that means we did not consume fast enough and etcd |
| 77 | // has canceled our watcher. We need to create a new watcher with the same key. |
| 78 | var err error |
| 79 | |
| 80 | watcher, err = k.storage.Watch(KitesPrefix+etcdKey, index) |
| 81 | if err != nil { |
| 82 | k.Kite.Log.Error("Cannot re-watch query: %s", err.Error()) |
| 83 | callback.Call(kite.Response{ |
| 84 | Error: &kite.Error{ |
| 85 | Type: "watchError", |
| 86 | Message: err.Error(), |
| 87 | }, |
| 88 | }) |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | continue |
| 93 | } |
| 94 |