mirror of
https://github.com/hoernschen/dendrite.git
synced 2024-12-28 16:08:27 +00:00
Persist events/state in order
This commit is contained in:
parent
9a7b42c5ed
commit
d1d82656e7
2 changed files with 20 additions and 48 deletions
|
@ -39,13 +39,6 @@ type Inputer struct {
|
||||||
latestEventsMutexes sync.Map // room ID -> sync.Mutex
|
latestEventsMutexes sync.Map // room ID -> sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type inputTask struct {
|
|
||||||
ctx context.Context
|
|
||||||
event *api.InputRoomEvent
|
|
||||||
wg *sync.WaitGroup
|
|
||||||
err error // written back by worker, only safe to read when all tasks are done
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteOutputEvents implements OutputRoomEventWriter
|
// WriteOutputEvents implements OutputRoomEventWriter
|
||||||
func (r *Inputer) WriteOutputEvents(roomID string, updates []api.OutputEvent) error {
|
func (r *Inputer) WriteOutputEvents(roomID string, updates []api.OutputEvent) error {
|
||||||
messages := make([]*sarama.ProducerMessage, len(updates))
|
messages := make([]*sarama.ProducerMessage, len(updates))
|
||||||
|
@ -94,40 +87,14 @@ func (r *Inputer) InputRoomEvents(
|
||||||
request *api.InputRoomEventsRequest,
|
request *api.InputRoomEventsRequest,
|
||||||
response *api.InputRoomEventsResponse,
|
response *api.InputRoomEventsResponse,
|
||||||
) {
|
) {
|
||||||
// Create a wait group. Each task that we dispatch will call Done on
|
|
||||||
// this wait group so that we know when all of our events have been
|
|
||||||
// processed.
|
|
||||||
wg := &sync.WaitGroup{}
|
|
||||||
wg.Add(len(request.InputRoomEvents))
|
|
||||||
tasks := make([]*inputTask, len(request.InputRoomEvents))
|
|
||||||
|
|
||||||
for i := range request.InputRoomEvents {
|
for i := range request.InputRoomEvents {
|
||||||
tasks[i] = &inputTask{
|
hooks.Run(hooks.KindNewEventReceived, &request.InputRoomEvents[i])
|
||||||
ctx: context.Background(),
|
if _, err := r.processRoomEvent(context.Background(), &request.InputRoomEvents[i]); err == nil {
|
||||||
event: &request.InputRoomEvents[i],
|
hooks.Run(hooks.KindNewEventPersisted, &request.InputRoomEvents[i])
|
||||||
wg: wg,
|
} else {
|
||||||
}
|
response.ErrMsg = err.Error()
|
||||||
go func(task *inputTask) {
|
_, rejected := err.(*gomatrixserverlib.NotAllowed)
|
||||||
hooks.Run(hooks.KindNewEventReceived, task.event.Event)
|
|
||||||
_, task.err = r.processRoomEvent(task.ctx, task.event)
|
|
||||||
if task.err == nil {
|
|
||||||
hooks.Run(hooks.KindNewEventPersisted, task.event.Event)
|
|
||||||
}
|
|
||||||
task.wg.Done()
|
|
||||||
}(tasks[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for all of the workers to return results about our tasks.
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
// If any of the tasks returned an error, we should probably report
|
|
||||||
// that back to the caller.
|
|
||||||
for _, task := range tasks {
|
|
||||||
if task.err != nil {
|
|
||||||
response.ErrMsg = task.err.Error()
|
|
||||||
_, rejected := task.err.(*gomatrixserverlib.NotAllowed)
|
|
||||||
response.NotAllowed = rejected
|
response.NotAllowed = rejected
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -192,15 +192,20 @@ func (r *Inputer) processRoomEvent(
|
||||||
|
|
||||||
switch input.Kind {
|
switch input.Kind {
|
||||||
case api.KindNew:
|
case api.KindNew:
|
||||||
if err = r.updateLatestEvents(
|
errch := make(chan error)
|
||||||
ctx, // context
|
go func() {
|
||||||
roomInfo, // room info for the room being updated
|
errch <- r.updateLatestEvents(
|
||||||
stateAtEvent, // state at event (below)
|
ctx, // context
|
||||||
event, // event
|
roomInfo, // room info for the room being updated
|
||||||
input.SendAsServer, // send as server
|
stateAtEvent, // state at event (below)
|
||||||
input.TransactionID, // transaction ID
|
event, // event
|
||||||
input.HasState, // rewrites state?
|
input.SendAsServer, // send as server
|
||||||
); err != nil {
|
input.TransactionID, // transaction ID
|
||||||
|
input.HasState, // rewrites state?
|
||||||
|
)
|
||||||
|
close(errch)
|
||||||
|
}()
|
||||||
|
if err = <-errch; err != nil {
|
||||||
return "", fmt.Errorf("r.updateLatestEvents: %w", err)
|
return "", fmt.Errorf("r.updateLatestEvents: %w", err)
|
||||||
}
|
}
|
||||||
case api.KindOld:
|
case api.KindOld:
|
||||||
|
|
Loading…
Reference in a new issue