Fix the sync api returning an empty sync response when reaching timeout, regardless of the since token

This commit is contained in:
Brendan Abolivier 2018-12-05 17:02:14 +00:00
parent 2133e6bf59
commit 176f088215
No known key found for this signature in database
GPG key ID: 8EF1500759F70623

View file

@ -84,38 +84,33 @@ func (rp *RequestPool) OnIncomingSyncRequest(req *http.Request, device *authtype
userStreamListener := rp.notifier.GetListener(*syncReq) userStreamListener := rp.notifier.GetListener(*syncReq)
defer userStreamListener.Close() defer userStreamListener.Close()
for { select {
select { // Wait for notifier to wake us up
// Wait for notifier to wake us up case <-userStreamListener.GetNotifyChannel(currPos):
case <-userStreamListener.GetNotifyChannel(currPos): currPos = userStreamListener.GetStreamPosition()
currPos = userStreamListener.GetStreamPosition() // Or for timeout to expire
// Or for timeout to expire case <-timer.C:
case <-timer.C: // We just need to ensure we get out of the select after reaching the
return util.JSONResponse{ // timeout, but there's nothing specific we want to do in this case
Code: http.StatusOK, // apart from that, so we do nothing.
JSON: types.NewResponse(currPos), // Or for the request to be cancelled
} case <-req.Context().Done():
// Or for the request to be cancelled return httputil.LogThenError(req, req.Context().Err())
case <-req.Context().Done(): }
return httputil.LogThenError(req, req.Context().Err())
}
// Note that we don't time out during calculation of sync // Note that we don't time out during calculation of sync
// response. This ensures that we don't waste the hard work // response. This ensures that we don't waste the hard work
// of calculating the sync only to get timed out before we // of calculating the sync only to get timed out before we
// can respond // can respond
syncData, err := rp.currentSyncForUser(*syncReq, currPos) syncData, err := rp.currentSyncForUser(*syncReq, currPos)
if err != nil { if err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
if !syncData.IsEmpty() {
return util.JSONResponse{
Code: http.StatusOK,
JSON: syncData,
}
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: syncData,
} }
} }