2020-04-28 10:53:07 +01:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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 producers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
"crypto/ed25519"
|
2020-04-28 10:53:07 +01:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RoomserverProducer produces events for the roomserver to consume.
|
|
|
|
type RoomserverProducer struct {
|
2020-05-01 10:48:17 +01:00
|
|
|
InputAPI api.RoomserverInternalAPI
|
2020-04-28 10:53:07 +01:00
|
|
|
serverName gomatrixserverlib.ServerName
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
keyID gomatrixserverlib.KeyID
|
|
|
|
privateKey ed25519.PrivateKey
|
2020-04-28 10:53:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRoomserverProducer creates a new RoomserverProducer
|
|
|
|
func NewRoomserverProducer(
|
2020-05-01 10:48:17 +01:00
|
|
|
rsAPI api.RoomserverInternalAPI, serverName gomatrixserverlib.ServerName,
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
keyID gomatrixserverlib.KeyID, privateKey ed25519.PrivateKey,
|
2020-04-28 10:53:07 +01:00
|
|
|
) *RoomserverProducer {
|
|
|
|
return &RoomserverProducer{
|
2020-05-01 10:48:17 +01:00
|
|
|
InputAPI: rsAPI,
|
2020-04-28 10:53:07 +01:00
|
|
|
serverName: serverName,
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
keyID: keyID,
|
|
|
|
privateKey: privateKey,
|
2020-04-28 10:53:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendInviteResponse drops an invite response back into the roomserver so that users
|
|
|
|
// already in the room will be notified of the new invite. The invite response is signed
|
|
|
|
// by the remote side.
|
|
|
|
func (c *RoomserverProducer) SendInviteResponse(
|
|
|
|
ctx context.Context, res gomatrixserverlib.RespInviteV2, roomVersion gomatrixserverlib.RoomVersion,
|
|
|
|
) (string, error) {
|
Improve federation sender performance, implement backoff and blacklisting, fix up invites a bit (#1007)
* Improve federation sender performance and behaviour, add backoff
* Tweaks
* Tweaks
* Tweaks
* Take copies of events before passing to destination queues
* Don't accidentally drop queued messages
* Don't take copies again
* Tidy up a bit
* Break out statistics (tracked component-wide), report success and failures from Perform actions
* Fix comment, use atomic add
* Improve logic a bit, don't block on wakeup, move idle check
* Don't retry sucessful invites, don't dispatch sendEvent, sendInvite etc
* Dedupe destinations, fix other bug hopefully
* Dispatch sends again
* Federation sender to ignore invites that are destined locally
* Loopback invite events
* Remodel a bit with channels
* Linter
* Only loopback invite event if we know the room
* We should tell other resident servers about the invite if we know about the room
* Correct invite signing
* Fix invite loopback
* Check HTTP response codes, push new invites to front of queue
* Review comments
2020-05-07 12:42:06 +01:00
|
|
|
ev := res.Event.Sign(string(c.serverName), c.keyID, c.privateKey).Headered(roomVersion)
|
2020-04-28 10:53:07 +01:00
|
|
|
ire := api.InputRoomEvent{
|
|
|
|
Kind: api.KindNew,
|
|
|
|
Event: ev,
|
|
|
|
AuthEventIDs: ev.AuthEventIDs(),
|
|
|
|
SendAsServer: string(c.serverName),
|
|
|
|
TransactionID: nil,
|
|
|
|
}
|
|
|
|
return c.SendInputRoomEvents(ctx, []api.InputRoomEvent{ire})
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:29:39 +01:00
|
|
|
// SendEventWithState writes an event with KindNew to the roomserver input log
|
|
|
|
// with the state at the event as KindOutlier before it.
|
|
|
|
func (c *RoomserverProducer) SendEventWithState(
|
|
|
|
ctx context.Context, state gomatrixserverlib.RespState, event gomatrixserverlib.HeaderedEvent,
|
|
|
|
) error {
|
|
|
|
outliers, err := state.Events()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ires []api.InputRoomEvent
|
|
|
|
for _, outlier := range outliers {
|
|
|
|
ires = append(ires, api.InputRoomEvent{
|
|
|
|
Kind: api.KindOutlier,
|
|
|
|
Event: outlier.Headered(event.RoomVersion),
|
|
|
|
AuthEventIDs: outlier.AuthEventIDs(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
stateEventIDs := make([]string, len(state.StateEvents))
|
|
|
|
for i := range state.StateEvents {
|
|
|
|
stateEventIDs[i] = state.StateEvents[i].EventID()
|
|
|
|
}
|
|
|
|
|
|
|
|
ires = append(ires, api.InputRoomEvent{
|
|
|
|
Kind: api.KindNew,
|
|
|
|
Event: event,
|
|
|
|
AuthEventIDs: event.AuthEventIDs(),
|
|
|
|
HasState: true,
|
|
|
|
StateEventIDs: stateEventIDs,
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = c.SendInputRoomEvents(ctx, ires)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-28 10:53:07 +01:00
|
|
|
// SendInputRoomEvents writes the given input room events to the roomserver input API.
|
|
|
|
func (c *RoomserverProducer) SendInputRoomEvents(
|
|
|
|
ctx context.Context, ires []api.InputRoomEvent,
|
|
|
|
) (eventID string, err error) {
|
|
|
|
request := api.InputRoomEventsRequest{InputRoomEvents: ires}
|
|
|
|
var response api.InputRoomEventsResponse
|
|
|
|
err = c.InputAPI.InputRoomEvents(ctx, &request, &response)
|
|
|
|
eventID = response.EventID
|
|
|
|
return
|
|
|
|
}
|