2017-04-07 14:32:42 +01:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-04-10 15:12:18 +01:00
|
|
|
"sync"
|
2017-04-07 14:32:42 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
2017-04-10 15:12:18 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2017-04-12 16:06:26 +01:00
|
|
|
"github.com/matrix-org/dendrite/syncserver/storage"
|
|
|
|
"github.com/matrix-org/dendrite/syncserver/types"
|
2017-04-07 14:32:42 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RequestPool manages HTTP long-poll connections for /sync
|
|
|
|
type RequestPool struct {
|
|
|
|
db *storage.SyncServerDatabase
|
2017-04-10 15:12:18 +01:00
|
|
|
// The latest sync stream position: guarded by 'cond'.
|
2017-04-12 16:06:26 +01:00
|
|
|
currPos types.StreamPosition
|
2017-04-10 15:12:18 +01:00
|
|
|
// A condition variable to notify all waiting goroutines of a new sync stream position
|
|
|
|
cond *sync.Cond
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRequestPool makes a new RequestPool
|
|
|
|
func NewRequestPool(db *storage.SyncServerDatabase) (*RequestPool, error) {
|
|
|
|
pos, err := db.SyncStreamPosition()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-12 16:06:26 +01:00
|
|
|
return &RequestPool{db, types.StreamPosition(pos), sync.NewCond(&sync.Mutex{})}, nil
|
2017-04-07 14:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnIncomingSyncRequest is called when a client makes a /sync request. This function MUST be
|
|
|
|
// called in a dedicated goroutine for this request. This function will block the goroutine
|
|
|
|
// until a response is ready, or it times out.
|
|
|
|
func (rp *RequestPool) OnIncomingSyncRequest(req *http.Request) util.JSONResponse {
|
|
|
|
// Extract values from request
|
|
|
|
logger := util.GetLogger(req.Context())
|
|
|
|
userID, resErr := auth.VerifyAccessToken(req)
|
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
2017-04-18 10:32:32 +01:00
|
|
|
syncReq, err := newSyncRequest(req, userID)
|
2017-04-10 15:12:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 400,
|
|
|
|
JSON: jsonerror.Unknown(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 14:32:42 +01:00
|
|
|
logger.WithFields(log.Fields{
|
|
|
|
"userID": userID,
|
2017-04-18 10:32:32 +01:00
|
|
|
"since": syncReq.since,
|
|
|
|
"timeout": syncReq.timeout,
|
2017-04-07 14:32:42 +01:00
|
|
|
}).Info("Incoming /sync request")
|
|
|
|
|
2017-04-10 15:12:18 +01:00
|
|
|
// Fork off 2 goroutines: one to do the work, and one to serve as a timeout.
|
|
|
|
// Whichever returns first is the one we will serve back to the client.
|
|
|
|
// TODO: Currently this means that cpu work is timed, which may not be what we want long term.
|
|
|
|
timeoutChan := make(chan struct{})
|
2017-04-18 10:32:32 +01:00
|
|
|
timer := time.AfterFunc(syncReq.timeout, func() {
|
2017-04-10 15:12:18 +01:00
|
|
|
close(timeoutChan) // signal that the timeout has expired
|
|
|
|
})
|
|
|
|
|
|
|
|
done := make(chan util.JSONResponse)
|
|
|
|
go func() {
|
2017-04-18 10:32:32 +01:00
|
|
|
syncData, err := rp.currentSyncForUser(*syncReq)
|
2017-04-10 15:12:18 +01:00
|
|
|
timer.Stop()
|
|
|
|
var res util.JSONResponse
|
|
|
|
if err != nil {
|
|
|
|
res = httputil.LogThenError(req, err)
|
|
|
|
} else {
|
|
|
|
res = util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: syncData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done <- res
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-timeoutChan: // timeout fired
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
2017-04-18 10:32:32 +01:00
|
|
|
JSON: types.NewResponse(syncReq.since),
|
2017-04-10 15:12:18 +01:00
|
|
|
}
|
|
|
|
case res := <-done: // received a response
|
|
|
|
return res
|
2017-04-07 14:32:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 15:12:18 +01:00
|
|
|
// OnNewEvent is called when a new event is received from the room server. Must only be
|
|
|
|
// called from a single goroutine, to avoid races between updates which could set the
|
|
|
|
// current position in the stream incorrectly.
|
2017-04-12 16:06:26 +01:00
|
|
|
func (rp *RequestPool) OnNewEvent(ev *gomatrixserverlib.Event, pos types.StreamPosition) {
|
2017-04-10 15:12:18 +01:00
|
|
|
// update the current position in a guard and then notify all /sync streams
|
|
|
|
rp.cond.L.Lock()
|
|
|
|
rp.currPos = pos
|
|
|
|
rp.cond.L.Unlock()
|
2017-04-07 14:32:42 +01:00
|
|
|
|
2017-04-10 15:12:18 +01:00
|
|
|
rp.cond.Broadcast() // notify ALL waiting goroutines
|
2017-04-07 14:32:42 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 16:06:26 +01:00
|
|
|
func (rp *RequestPool) waitForEvents(req syncRequest) types.StreamPosition {
|
2017-04-10 15:12:18 +01:00
|
|
|
// In a guard, check if the /sync request should block, and block it until we get a new position
|
|
|
|
rp.cond.L.Lock()
|
|
|
|
currentPos := rp.currPos
|
|
|
|
for req.since == currentPos {
|
|
|
|
// we need to wait for a new event.
|
|
|
|
// TODO: This waits for ANY new event, we need to only wait for events which we care about.
|
|
|
|
rp.cond.Wait() // atomically unlocks and blocks goroutine, then re-acquires lock on unblock
|
|
|
|
currentPos = rp.currPos
|
2017-04-07 14:32:42 +01:00
|
|
|
}
|
2017-04-10 15:12:18 +01:00
|
|
|
rp.cond.L.Unlock()
|
|
|
|
return currentPos
|
|
|
|
}
|
2017-04-07 14:32:42 +01:00
|
|
|
|
2017-04-12 16:06:26 +01:00
|
|
|
func (rp *RequestPool) currentSyncForUser(req syncRequest) (*types.Response, error) {
|
2017-04-20 18:01:45 +01:00
|
|
|
currentPos := rp.waitForEvents(req)
|
|
|
|
|
2017-04-13 16:56:46 +01:00
|
|
|
if req.since == types.StreamPosition(0) {
|
2017-04-18 10:32:32 +01:00
|
|
|
pos, data, err := rp.db.CompleteSync(req.userID, req.limit)
|
2017-04-13 16:56:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-18 10:32:32 +01:00
|
|
|
res := types.NewResponse(pos)
|
2017-04-13 16:56:46 +01:00
|
|
|
for roomID, d := range data {
|
|
|
|
jr := types.NewJoinResponse()
|
|
|
|
jr.Timeline.Events = gomatrixserverlib.ToClientEvents(d.RecentEvents, gomatrixserverlib.FormatSync)
|
|
|
|
jr.Timeline.Limited = true
|
|
|
|
jr.State.Events = gomatrixserverlib.ToClientEvents(d.State, gomatrixserverlib.FormatSync)
|
|
|
|
res.Rooms.Join[roomID] = *jr
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2017-04-11 11:52:26 +01:00
|
|
|
// TODO: handle ignored users
|
|
|
|
|
2017-04-19 16:04:01 +01:00
|
|
|
data, err := rp.db.IncrementalSync(req.userID, req.since, currentPos, req.limit)
|
2017-04-11 11:52:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-18 10:32:32 +01:00
|
|
|
res := types.NewResponse(currentPos)
|
2017-04-19 16:04:01 +01:00
|
|
|
for roomID, d := range data {
|
|
|
|
jr := types.NewJoinResponse()
|
|
|
|
jr.Timeline.Events = gomatrixserverlib.ToClientEvents(d.RecentEvents, gomatrixserverlib.FormatSync)
|
|
|
|
jr.Timeline.Limited = false // TODO: if len(events) >= numRecents + 1 and then set limited:true
|
|
|
|
jr.State.Events = gomatrixserverlib.ToClientEvents(d.State, gomatrixserverlib.FormatSync)
|
|
|
|
res.Rooms.Join[roomID] = *jr
|
2017-04-11 11:52:26 +01:00
|
|
|
}
|
|
|
|
return res, nil
|
2017-04-07 14:32:42 +01:00
|
|
|
}
|