2017-04-20 22:40:52 +00:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 16:50:19 +00:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 22:40:52 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-07 13:32:42 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2020-11-20 11:29:02 +00:00
|
|
|
"net"
|
2017-04-07 13:32:42 +00:00
|
|
|
"net/http"
|
2020-11-20 11:29:02 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-04-07 13:32:42 +00:00
|
|
|
"time"
|
|
|
|
|
2017-04-10 14:12:18 +00:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2020-07-30 10:15:46 +00:00
|
|
|
keyapi "github.com/matrix-org/dendrite/keyserver/api"
|
2020-09-04 13:25:01 +00:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-07-30 10:15:46 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/internal"
|
2021-01-08 16:59:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/notifier"
|
2017-04-20 16:22:44 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2021-01-08 16:59:06 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/streams"
|
2017-04-20 16:22:44 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2020-06-16 13:10:55 +00:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2017-04-07 13:32:42 +00:00
|
|
|
"github.com/matrix-org/util"
|
2020-12-16 15:02:39 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-04-07 13:32:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RequestPool manages HTTP long-poll connections for /sync
|
|
|
|
type RequestPool struct {
|
2020-06-16 16:05:38 +00:00
|
|
|
db storage.Database
|
2020-11-20 11:29:02 +00:00
|
|
|
cfg *config.SyncAPI
|
2020-06-16 16:05:38 +00:00
|
|
|
userAPI userapi.UserInternalAPI
|
2020-07-30 10:15:46 +00:00
|
|
|
keyAPI keyapi.KeyInternalAPI
|
2020-09-04 13:25:01 +00:00
|
|
|
rsAPI roomserverAPI.RoomserverInternalAPI
|
2020-11-20 11:29:02 +00:00
|
|
|
lastseen sync.Map
|
2021-01-08 16:59:06 +00:00
|
|
|
streams *streams.Streams
|
|
|
|
Notifier *notifier.Notifier
|
2017-04-10 14:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRequestPool makes a new RequestPool
|
2020-07-30 10:15:46 +00:00
|
|
|
func NewRequestPool(
|
2021-01-08 16:59:06 +00:00
|
|
|
db storage.Database, cfg *config.SyncAPI,
|
2020-11-20 11:29:02 +00:00
|
|
|
userAPI userapi.UserInternalAPI, keyAPI keyapi.KeyInternalAPI,
|
2020-09-07 13:47:59 +00:00
|
|
|
rsAPI roomserverAPI.RoomserverInternalAPI,
|
2021-01-08 16:59:06 +00:00
|
|
|
streams *streams.Streams, notifier *notifier.Notifier,
|
2020-07-30 10:15:46 +00:00
|
|
|
) *RequestPool {
|
2021-01-08 16:59:06 +00:00
|
|
|
rp := &RequestPool{
|
|
|
|
db: db,
|
|
|
|
cfg: cfg,
|
|
|
|
userAPI: userAPI,
|
|
|
|
keyAPI: keyAPI,
|
|
|
|
rsAPI: rsAPI,
|
|
|
|
lastseen: sync.Map{},
|
|
|
|
streams: streams,
|
|
|
|
Notifier: notifier,
|
|
|
|
}
|
2020-11-20 11:29:02 +00:00
|
|
|
go rp.cleanLastSeen()
|
|
|
|
return rp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RequestPool) cleanLastSeen() {
|
|
|
|
for {
|
|
|
|
rp.lastseen.Range(func(key interface{}, _ interface{}) bool {
|
|
|
|
rp.lastseen.Delete(key)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RequestPool) updateLastSeen(req *http.Request, device *userapi.Device) {
|
|
|
|
if _, ok := rp.lastseen.LoadOrStore(device.UserID+device.ID, struct{}{}); ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteAddr := req.RemoteAddr
|
|
|
|
if rp.cfg.RealIPHeader != "" {
|
|
|
|
if header := req.Header.Get(rp.cfg.RealIPHeader); header != "" {
|
|
|
|
// TODO: Maybe this isn't great but it will satisfy both X-Real-IP
|
|
|
|
// and X-Forwarded-For (which can be a list where the real client
|
|
|
|
// address is the first listed address). Make more intelligent?
|
|
|
|
addresses := strings.Split(header, ",")
|
|
|
|
if ip := net.ParseIP(addresses[0]); ip != nil {
|
|
|
|
remoteAddr = addresses[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lsreq := &userapi.PerformLastSeenUpdateRequest{
|
|
|
|
UserID: device.UserID,
|
|
|
|
DeviceID: device.ID,
|
|
|
|
RemoteAddr: remoteAddr,
|
|
|
|
}
|
|
|
|
lsres := &userapi.PerformLastSeenUpdateResponse{}
|
|
|
|
go rp.userAPI.PerformLastSeenUpdate(req.Context(), lsreq, lsres) // nolint:errcheck
|
|
|
|
|
|
|
|
rp.lastseen.Store(device.UserID+device.ID, time.Now())
|
2017-04-07 13:32:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 15:02:39 +00:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(
|
|
|
|
activeSyncRequests, waitingSyncRequests,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeSyncRequests = prometheus.NewGauge(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: "dendrite",
|
|
|
|
Subsystem: "syncapi",
|
|
|
|
Name: "active_sync_requests",
|
|
|
|
Help: "The number of sync requests that are active right now",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
var waitingSyncRequests = prometheus.NewGauge(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: "dendrite",
|
|
|
|
Subsystem: "syncapi",
|
|
|
|
Name: "waiting_sync_requests",
|
|
|
|
Help: "The number of sync requests that are waiting to be woken by a notifier",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2017-04-07 13:32:42 +00: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.
|
2020-06-16 13:10:55 +00:00
|
|
|
func (rp *RequestPool) OnIncomingSyncRequest(req *http.Request, device *userapi.Device) util.JSONResponse {
|
2017-04-07 13:32:42 +00:00
|
|
|
// Extract values from request
|
2020-06-26 14:34:41 +00:00
|
|
|
syncReq, err := newSyncRequest(req, *device, rp.db)
|
2017-04-10 14:12:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 15:55:45 +00:00
|
|
|
Code: http.StatusBadRequest,
|
2017-04-10 14:12:18 +00:00
|
|
|
JSON: jsonerror.Unknown(err.Error()),
|
|
|
|
}
|
|
|
|
}
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 16:50:19 +00:00
|
|
|
|
2020-12-16 15:02:39 +00:00
|
|
|
activeSyncRequests.Inc()
|
|
|
|
defer activeSyncRequests.Dec()
|
|
|
|
|
2020-11-20 11:29:02 +00:00
|
|
|
rp.updateLastSeen(req, device)
|
|
|
|
|
2020-12-16 15:02:39 +00:00
|
|
|
waitingSyncRequests.Inc()
|
|
|
|
defer waitingSyncRequests.Dec()
|
|
|
|
|
2021-01-08 16:59:06 +00:00
|
|
|
currentPos := rp.Notifier.CurrentPosition()
|
2017-10-16 12:34:08 +00:00
|
|
|
|
2021-04-26 15:33:31 +00:00
|
|
|
if !rp.shouldReturnImmediately(syncReq, currentPos) {
|
2021-01-08 16:59:06 +00:00
|
|
|
timer := time.NewTimer(syncReq.Timeout) // case of timeout=0 is handled above
|
|
|
|
defer timer.Stop()
|
2017-10-16 12:34:08 +00:00
|
|
|
|
2021-01-08 16:59:06 +00:00
|
|
|
userStreamListener := rp.Notifier.GetListener(*syncReq)
|
|
|
|
defer userStreamListener.Close()
|
2017-10-26 10:34:54 +00:00
|
|
|
|
2021-01-08 16:59:06 +00:00
|
|
|
giveup := func() util.JSONResponse {
|
|
|
|
syncReq.Response.NextBatch = syncReq.Since
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: syncReq.Response,
|
|
|
|
}
|
2017-10-16 12:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 16:59:06 +00:00
|
|
|
select {
|
|
|
|
case <-syncReq.Context.Done(): // Caller gave up
|
|
|
|
return giveup()
|
|
|
|
|
|
|
|
case <-timer.C: // Timeout reached
|
|
|
|
return giveup()
|
|
|
|
|
|
|
|
case <-userStreamListener.GetNotifyChannel(syncReq.Since):
|
|
|
|
syncReq.Log.Debugln("Responding to sync after wake-up")
|
|
|
|
currentPos.ApplyUpdates(userStreamListener.GetSyncPosition())
|
2017-10-16 12:34:08 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
} else {
|
|
|
|
syncReq.Log.Debugln("Responding to sync immediately")
|
|
|
|
}
|
2018-12-18 17:56:08 +00:00
|
|
|
|
2021-01-08 16:59:06 +00:00
|
|
|
if syncReq.Since.IsEmpty() {
|
|
|
|
// Complete sync
|
|
|
|
syncReq.Response.NextBatch = types.StreamingToken{
|
|
|
|
PDUPosition: rp.streams.PDUStreamProvider.CompleteSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
),
|
|
|
|
TypingPosition: rp.streams.TypingStreamProvider.CompleteSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
),
|
|
|
|
ReceiptPosition: rp.streams.ReceiptStreamProvider.CompleteSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
),
|
|
|
|
InvitePosition: rp.streams.InviteStreamProvider.CompleteSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
),
|
|
|
|
SendToDevicePosition: rp.streams.SendToDeviceStreamProvider.CompleteSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
),
|
|
|
|
AccountDataPosition: rp.streams.AccountDataStreamProvider.CompleteSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
),
|
|
|
|
DeviceListPosition: rp.streams.DeviceListStreamProvider.CompleteSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Incremental sync
|
|
|
|
syncReq.Response.NextBatch = types.StreamingToken{
|
|
|
|
PDUPosition: rp.streams.PDUStreamProvider.IncrementalSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
syncReq.Since.PDUPosition, currentPos.PDUPosition,
|
|
|
|
),
|
|
|
|
TypingPosition: rp.streams.TypingStreamProvider.IncrementalSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
syncReq.Since.TypingPosition, currentPos.TypingPosition,
|
|
|
|
),
|
|
|
|
ReceiptPosition: rp.streams.ReceiptStreamProvider.IncrementalSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
syncReq.Since.ReceiptPosition, currentPos.ReceiptPosition,
|
|
|
|
),
|
|
|
|
InvitePosition: rp.streams.InviteStreamProvider.IncrementalSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
syncReq.Since.InvitePosition, currentPos.InvitePosition,
|
|
|
|
),
|
|
|
|
SendToDevicePosition: rp.streams.SendToDeviceStreamProvider.IncrementalSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
syncReq.Since.SendToDevicePosition, currentPos.SendToDevicePosition,
|
|
|
|
),
|
|
|
|
AccountDataPosition: rp.streams.AccountDataStreamProvider.IncrementalSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
syncReq.Since.AccountDataPosition, currentPos.AccountDataPosition,
|
|
|
|
),
|
|
|
|
DeviceListPosition: rp.streams.DeviceListStreamProvider.IncrementalSync(
|
|
|
|
syncReq.Context, syncReq,
|
|
|
|
syncReq.Since.DeviceListPosition, currentPos.DeviceListPosition,
|
|
|
|
),
|
2017-04-10 14:12:18 +00:00
|
|
|
}
|
2017-04-07 13:32:42 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusOK,
|
|
|
|
JSON: syncReq.Response,
|
|
|
|
}
|
2017-04-07 13:32:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 13:52:21 +00:00
|
|
|
func (rp *RequestPool) OnIncomingKeyChangeRequest(req *http.Request, device *userapi.Device) util.JSONResponse {
|
|
|
|
from := req.URL.Query().Get("from")
|
|
|
|
to := req.URL.Query().Get("to")
|
|
|
|
if from == "" || to == "" {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 400,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue("missing ?from= or ?to="),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fromToken, err := types.NewStreamTokenFromString(from)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 400,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue("bad 'from' value"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
toToken, err := types.NewStreamTokenFromString(to)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 400,
|
|
|
|
JSON: jsonerror.InvalidArgumentValue("bad 'to' value"),
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
syncReq, err := newSyncRequest(req, *device, rp.db)
|
2020-07-30 13:52:21 +00:00
|
|
|
if err != nil {
|
2021-01-08 16:59:06 +00:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("newSyncRequest failed")
|
2020-07-30 13:52:21 +00:00
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
rp.streams.PDUStreamProvider.IncrementalSync(req.Context(), syncReq, fromToken.PDUPosition, toToken.PDUPosition)
|
|
|
|
_, _, err = internal.DeviceListCatchup(
|
|
|
|
req.Context(), rp.keyAPI, rp.rsAPI, syncReq.Device.UserID,
|
|
|
|
syncReq.Response, fromToken.DeviceListPosition, toToken.DeviceListPosition,
|
|
|
|
)
|
2020-07-30 13:52:21 +00:00
|
|
|
if err != nil {
|
2021-01-08 16:59:06 +00:00
|
|
|
util.GetLogger(req.Context()).WithError(err).Error("Failed to DeviceListCatchup info")
|
2020-07-30 13:52:21 +00:00
|
|
|
return jsonerror.InternalServerError()
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: struct {
|
|
|
|
Changed []string `json:"changed"`
|
|
|
|
Left []string `json:"left"`
|
|
|
|
}{
|
2021-01-08 16:59:06 +00:00
|
|
|
Changed: syncReq.Response.DeviceLists.Changed,
|
|
|
|
Left: syncReq.Response.DeviceLists.Left,
|
2020-07-30 13:52:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 04:36:13 +00:00
|
|
|
// shouldReturnImmediately returns whether the /sync request is an initial sync,
|
|
|
|
// or timeout=0, or full_state=true, in any of the cases the request should
|
|
|
|
// return immediately.
|
2021-04-26 15:33:31 +00:00
|
|
|
func (rp *RequestPool) shouldReturnImmediately(syncReq *types.SyncRequest, currentPos types.StreamingToken) bool {
|
|
|
|
if currentPos.IsAfter(syncReq.Since) || syncReq.Timeout == 0 || syncReq.WantFullState {
|
Send-to-device support (#1072)
* Groundwork for send-to-device messaging
* Update sample config
* Add unstable routing for now
* Send to device consumer in sync API
* Start the send-to-device consumer
* fix indentation in dendrite-config.yaml
* Create send-to-device database tables, other tweaks
* Add some logic for send-to-device messages, add them into sync stream
* Handle incoming send-to-device messages, count them with EDU stream pos
* Undo changes to test
* pq.Array
* Fix sync
* Logging
* Fix a couple of transaction things, fix client API
* Add send-to-device test, hopefully fix bugs
* Comments
* Refactor a bit
* Fix schema
* Fix queries
* Debug logging
* Fix storing and retrieving of send-to-device messages
* Try to avoid database locks
* Update sync position
* Use latest sync position
* Jiggle about sync a bit
* Fix tests
* Break out the retrieval from the update/delete behaviour
* Comments
* nolint on getResponseWithPDUsForCompleteSync
* Try to line up sync tokens again
* Implement wildcard
* Add all send-to-device tests to whitelist, what could possibly go wrong?
* Only care about wildcard when targeted locally
* Deduplicate transactions
* Handle tokens properly, return immediately if waiting send-to-device messages
* Fix sync
* Update sytest-whitelist
* Fix copyright notice (need to do more of this)
* Comments, copyrights
* Return errors from Do, fix dendritejs
* Review comments
* Comments
* Constructor for TransactionWriter
* defletions
* Update gomatrixserverlib, sytest-blacklist
2020-06-01 16:50:19 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
return false
|
2019-08-01 04:36:13 +00:00
|
|
|
}
|