| 15 | ) |
| 16 | |
| 17 | func (k *Kontrol) HandleHeartbeat(rw http.ResponseWriter, req *http.Request) { |
| 18 | id := req.URL.Query().Get("id") |
| 19 | if id == "" { |
| 20 | http.Error(rw, "query id is empty", http.StatusBadRequest) |
| 21 | return |
| 22 | } |
| 23 | |
| 24 | k.heartbeatsMu.Lock() |
| 25 | defer k.heartbeatsMu.Unlock() |
| 26 | |
| 27 | k.log.Debug("Heartbeat received '%s'", id) |
| 28 | if h, ok := k.heartbeats[id]; ok { |
| 29 | // try to reset the timer every time the remote kite sends us a |
| 30 | // heartbeat. Because the timer get reset, the timer is never fired, so |
| 31 | // the value get always updated with the updater in the background |
| 32 | // according to the write interval. If the kite doesn't send any |
| 33 | // heartbeat, the timer func is being called, which stops the updater |
| 34 | // so the key is being deleted automatically via the TTL mechanism. |
| 35 | h.timer.Reset(HeartbeatInterval + HeartbeatDelay) |
| 36 | |
| 37 | k.log.Debug("Sending pong '%s'", id) |
| 38 | rw.Write([]byte("pong")) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | // if we reach here than it has several meanings: |
| 43 | // * kite was registered before, but kontrol is restarted |
| 44 | // * kite was registered before, but kontrol has lost track |
| 45 | // * kite was no registered and someone else sends an heartbeat |
| 46 | // we send back "registeragain" so the caller can be added in to the |
| 47 | // heartbeats map above. |
| 48 | k.log.Debug("Sending registeragain '%s'", id) |
| 49 | rw.Write([]byte("registeragain")) |
| 50 | } |
| 51 | |
| 52 | func (k *Kontrol) HandleRegisterHTTP(rw http.ResponseWriter, req *http.Request) { |
| 53 | var args protocol.RegisterArgs |