This commit is contained in:
Neil Alexander 2021-01-12 14:33:28 +00:00
parent 53abc2c0ec
commit 6ae9c1fea8
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
6 changed files with 25 additions and 24 deletions

View file

@ -105,6 +105,8 @@ func (s *OutputSendToDeviceEventConsumer) onMessage(msg *sarama.ConsumerMessage)
return err
}
util.GetLogger(context.TODO()).Infof("Stored send-to-device message at position %d", streamPos)
s.stream.Advance(streamPos)
s.notifier.OnNewSendToDevice(
output.UserID,

View file

@ -50,7 +50,7 @@ const insertSendToDeviceMessageSQL = `
const selectSendToDeviceMessagesSQL = `
SELECT id, user_id, device_id, content
FROM syncapi_send_to_device
WHERE user_id = $1 AND device_id = $2 AND id > $3 AND id <= $4
WHERE user_id = $1 AND device_id = $2 AND id <= $3
ORDER BY id DESC
`
@ -98,16 +98,16 @@ func (s *sendToDeviceStatements) InsertSendToDeviceMessage(
}
func (s *sendToDeviceStatements) SelectSendToDeviceMessages(
ctx context.Context, txn *sql.Tx, userID, deviceID string, from, to types.StreamPosition,
ctx context.Context, txn *sql.Tx, userID, deviceID string, to types.StreamPosition,
) (lastPos types.StreamPosition, events []types.SendToDeviceEvent, err error) {
rows, err := sqlutil.TxStmt(txn, s.selectSendToDeviceMessagesStmt).QueryContext(ctx, userID, deviceID, from, to)
rows, err := sqlutil.TxStmt(txn, s.selectSendToDeviceMessagesStmt).QueryContext(ctx, userID, deviceID, to)
if err != nil {
return
}
defer internal.CloseAndLogIfError(ctx, rows, "SelectSendToDeviceMessages: rows.close() failed")
for rows.Next() {
var id types.SendToDeviceNID
var id types.StreamPosition
var userID, deviceID, content string
if err = rows.Scan(&id, &userID, &deviceID, &content); err != nil {
return
@ -118,11 +118,11 @@ func (s *sendToDeviceStatements) SelectSendToDeviceMessages(
DeviceID: deviceID,
}
if err = json.Unmarshal([]byte(content), &event.SendToDeviceEvent); err != nil {
return
continue
}
events = append(events, event)
if types.StreamPosition(id) > lastPos {
lastPos = types.StreamPosition(id)
if id > lastPos {
lastPos = id
}
}

View file

@ -29,6 +29,7 @@ import (
"github.com/matrix-org/dendrite/syncapi/storage/tables"
"github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
@ -893,7 +894,7 @@ func (d *Database) StoreNewSendForDeviceMessage(
if err != nil {
return 0, err
}
return
return newPos, nil
}
func (d *Database) SendToDeviceUpdatesForSync(
@ -902,14 +903,14 @@ func (d *Database) SendToDeviceUpdatesForSync(
from, to types.StreamPosition,
) (types.StreamPosition, []types.SendToDeviceEvent, error) {
// First of all, get our send-to-device updates for this user.
lastPos, events, err := d.SendToDevice.SelectSendToDeviceMessages(ctx, nil, userID, deviceID, from, to)
lastPos, events, err := d.SendToDevice.SelectSendToDeviceMessages(ctx, nil, userID, deviceID, to)
if err != nil {
return 0, nil, fmt.Errorf("d.SendToDevice.SelectSendToDeviceMessages: %w", err)
return from, nil, fmt.Errorf("d.SendToDevice.SelectSendToDeviceMessages: %w", err)
}
// If there's nothing to do then stop here.
if len(events) == 0 {
return 0, nil, nil
return to, nil, nil
}
// If we've advanced past this stream position for this
@ -917,7 +918,7 @@ func (d *Database) SendToDeviceUpdatesForSync(
if err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
return d.SendToDevice.DeleteSendToDeviceMessages(ctx, txn, userID, deviceID, from)
}); err != nil {
return 0, nil, fmt.Errorf("d.Writer.Do: %w", err)
logrus.WithError(err).Errorf("Failed to clean up old send-to-device messages for user %q device %q", userID, deviceID)
}
return lastPos, events, nil

View file

@ -47,7 +47,7 @@ const insertSendToDeviceMessageSQL = `
const selectSendToDeviceMessagesSQL = `
SELECT id, user_id, device_id, content
FROM syncapi_send_to_device
WHERE user_id = $1 AND device_id = $2 AND id > $3 AND id <= $4
WHERE user_id = $1 AND device_id = $2 AND id <= $3
ORDER BY id DESC
`
@ -104,16 +104,16 @@ func (s *sendToDeviceStatements) InsertSendToDeviceMessage(
}
func (s *sendToDeviceStatements) SelectSendToDeviceMessages(
ctx context.Context, txn *sql.Tx, userID, deviceID string, from, to types.StreamPosition,
ctx context.Context, txn *sql.Tx, userID, deviceID string, to types.StreamPosition,
) (lastPos types.StreamPosition, events []types.SendToDeviceEvent, err error) {
rows, err := sqlutil.TxStmt(txn, s.selectSendToDeviceMessagesStmt).QueryContext(ctx, userID, deviceID, from, to)
rows, err := sqlutil.TxStmt(txn, s.selectSendToDeviceMessagesStmt).QueryContext(ctx, userID, deviceID, to)
if err != nil {
return
}
defer internal.CloseAndLogIfError(ctx, rows, "SelectSendToDeviceMessages: rows.close() failed")
for rows.Next() {
var id types.SendToDeviceNID
var id types.StreamPosition
var userID, deviceID, content string
if err = rows.Scan(&id, &userID, &deviceID, &content); err != nil {
return
@ -124,11 +124,11 @@ func (s *sendToDeviceStatements) SelectSendToDeviceMessages(
DeviceID: deviceID,
}
if err = json.Unmarshal([]byte(content), &event.SendToDeviceEvent); err != nil {
return
continue
}
events = append(events, event)
if types.StreamPosition(id) > lastPos {
lastPos = types.StreamPosition(id)
if id > lastPos {
lastPos = id
}
}

View file

@ -147,8 +147,8 @@ type BackwardsExtremities interface {
// sync response, as the client is seemingly trying to repeat the same /sync.
type SendToDevice interface {
InsertSendToDeviceMessage(ctx context.Context, txn *sql.Tx, userID, deviceID, content string) (pos types.StreamPosition, err error)
SelectSendToDeviceMessages(ctx context.Context, txn *sql.Tx, userID, deviceID string, from, to types.StreamPosition) (lastPos types.StreamPosition, events []types.SendToDeviceEvent, err error)
DeleteSendToDeviceMessages(ctx context.Context, txn *sql.Tx, userID, deviceID string, pos types.StreamPosition) (err error)
SelectSendToDeviceMessages(ctx context.Context, txn *sql.Tx, userID, deviceID string, to types.StreamPosition) (lastPos types.StreamPosition, events []types.SendToDeviceEvent, err error)
DeleteSendToDeviceMessages(ctx context.Context, txn *sql.Tx, userID, deviceID string, from types.StreamPosition) (err error)
SelectMaxSendToDeviceMessageID(ctx context.Context, txn *sql.Tx) (id int64, err error)
}

View file

@ -492,11 +492,9 @@ func NewLeaveResponse() *LeaveResponse {
return &res
}
type SendToDeviceNID int
type SendToDeviceEvent struct {
gomatrixserverlib.SendToDeviceEvent
ID SendToDeviceNID
ID StreamPosition
UserID string
DeviceID string
}