2017-07-13 05:41:30 -05:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Package input contains the code processes new room events
|
2020-09-02 11:13:15 -05:00
|
|
|
package input
|
2017-07-13 05:41:30 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-13 07:37:50 -05:00
|
|
|
"context"
|
2017-07-13 05:41:30 -05:00
|
|
|
"encoding/json"
|
2020-08-20 10:24:33 -05:00
|
|
|
"sync"
|
2017-07-13 05:41:30 -05:00
|
|
|
|
2020-04-22 09:26:56 -05:00
|
|
|
"github.com/Shopify/sarama"
|
2017-07-13 05:41:30 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 11:13:15 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-06-12 06:10:08 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-07-13 05:41:30 -05:00
|
|
|
)
|
|
|
|
|
2020-09-02 11:13:15 -05:00
|
|
|
type Inputer struct {
|
|
|
|
DB storage.Database
|
|
|
|
Producer sarama.SyncProducer
|
|
|
|
ServerName gomatrixserverlib.ServerName
|
|
|
|
OutputRoomEventTopic string
|
|
|
|
|
|
|
|
mutexes sync.Map // room ID -> *sync.Mutex, protects calls to processRoomEvent
|
|
|
|
}
|
|
|
|
|
2017-08-08 10:38:03 -05:00
|
|
|
// WriteOutputEvents implements OutputRoomEventWriter
|
2020-09-02 11:13:15 -05:00
|
|
|
func (r *Inputer) WriteOutputEvents(roomID string, updates []api.OutputEvent) error {
|
2017-08-08 10:38:03 -05:00
|
|
|
messages := make([]*sarama.ProducerMessage, len(updates))
|
|
|
|
for i := range updates {
|
|
|
|
value, err := json.Marshal(updates[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-12 06:10:08 -05:00
|
|
|
logger := log.WithFields(log.Fields{
|
|
|
|
"room_id": roomID,
|
|
|
|
"type": updates[i].Type,
|
|
|
|
})
|
|
|
|
if updates[i].NewRoomEvent != nil {
|
|
|
|
logger = logger.WithFields(log.Fields{
|
|
|
|
"event_type": updates[i].NewRoomEvent.Event.Type(),
|
|
|
|
"event_id": updates[i].NewRoomEvent.Event.EventID(),
|
|
|
|
"adds_state": len(updates[i].NewRoomEvent.AddsStateEventIDs),
|
|
|
|
"removes_state": len(updates[i].NewRoomEvent.RemovesStateEventIDs),
|
|
|
|
"send_as_server": updates[i].NewRoomEvent.SendAsServer,
|
|
|
|
"sender": updates[i].NewRoomEvent.Event.Sender(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
logger.Infof("Producing to topic '%s'", r.OutputRoomEventTopic)
|
2017-08-08 10:38:03 -05:00
|
|
|
messages[i] = &sarama.ProducerMessage{
|
|
|
|
Topic: r.OutputRoomEventTopic,
|
|
|
|
Key: sarama.StringEncoder(roomID),
|
|
|
|
Value: sarama.ByteEncoder(value),
|
|
|
|
}
|
2017-07-13 05:41:30 -05:00
|
|
|
}
|
2017-08-08 10:38:03 -05:00
|
|
|
return r.Producer.SendMessages(messages)
|
2017-07-13 05:41:30 -05:00
|
|
|
}
|
|
|
|
|
2020-05-01 04:48:17 -05:00
|
|
|
// InputRoomEvents implements api.RoomserverInternalAPI
|
2020-09-02 11:13:15 -05:00
|
|
|
func (r *Inputer) InputRoomEvents(
|
2017-09-13 07:37:50 -05:00
|
|
|
ctx context.Context,
|
2017-07-13 05:41:30 -05:00
|
|
|
request *api.InputRoomEventsRequest,
|
|
|
|
response *api.InputRoomEventsResponse,
|
2018-05-26 06:03:35 -05:00
|
|
|
) (err error) {
|
2020-08-20 10:24:33 -05:00
|
|
|
for i, e := range request.InputRoomEvents {
|
|
|
|
roomID := "global"
|
|
|
|
if r.DB.SupportsConcurrentRoomInputs() {
|
|
|
|
roomID = e.Event.RoomID()
|
|
|
|
}
|
|
|
|
mutex, _ := r.mutexes.LoadOrStore(roomID, &sync.Mutex{})
|
|
|
|
mutex.(*sync.Mutex).Lock()
|
2020-05-20 12:03:06 -05:00
|
|
|
if response.EventID, err = r.processRoomEvent(ctx, request.InputRoomEvents[i]); err != nil {
|
2020-08-20 10:24:33 -05:00
|
|
|
mutex.(*sync.Mutex).Unlock()
|
2017-08-21 10:37:11 -05:00
|
|
|
return err
|
|
|
|
}
|
2020-08-20 10:24:33 -05:00
|
|
|
mutex.(*sync.Mutex).Unlock()
|
2017-08-21 10:37:11 -05:00
|
|
|
}
|
2017-07-13 05:41:30 -05:00
|
|
|
return nil
|
|
|
|
}
|