Actually get a working timeout

This commit is contained in:
Kegan Dougal 2017-04-07 16:40:21 +01:00
parent 622b7af311
commit 148a868b04

View file

@ -1,7 +1,6 @@
package sync package sync
import ( import (
"context"
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
@ -78,11 +77,25 @@ func (rp *RequestPool) OnIncomingSyncRequest(req *http.Request) util.JSONRespons
"current": rp.currPos, "current": rp.currPos,
}).Info("Incoming /sync request") }).Info("Incoming /sync request")
// set the timeout going // Set up a timer based on the provided timeout value.
ctx, cancel := context.WithTimeout(req.Context(), timeout) // In a separate goroutine, wait for it to expire or the server to respond.
defer cancel() // TODO: Send a response if timed out.
done := make(chan struct{})
timer := time.NewTimer(timeout)
go func() {
select {
case <-timer.C:
logger.Warn("Timed out!")
// timed out
case <-done:
logger.Info("Serviced.")
// serviced request before timeout expired
timer.Stop()
}
}()
res, err := rp.currentSyncForUser(ctx, syncReq) res, err := rp.currentSyncForUser(syncReq)
close(done) // signal that the work is complete
if err != nil { if err != nil {
return httputil.LogThenError(req, err) return httputil.LogThenError(req, err)
} }
@ -118,7 +131,7 @@ func (rp *RequestPool) waitForEvents(req syncRequest) syncStreamPosition {
return currentPos return currentPos
} }
func (rp *RequestPool) currentSyncForUser(ctx context.Context, req syncRequest) ([]gomatrixserverlib.Event, error) { func (rp *RequestPool) currentSyncForUser(req syncRequest) ([]gomatrixserverlib.Event, error) {
currentPos := rp.waitForEvents(req) currentPos := rp.waitForEvents(req)
return rp.db.EventsInRange(int64(req.since), int64(currentPos)) return rp.db.EventsInRange(int64(req.since), int64(currentPos))