mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-17 15:51:23 +01:00
sending.rs: move handler functions out of service
These don't take `self`, no reason for them to be associated functions.
This commit is contained in:
parent
13f79dfee1
commit
9071e11e06
1 changed files with 239 additions and 245 deletions
|
|
@ -191,7 +191,7 @@ impl Service {
|
||||||
for (outgoing_kind, events) in initial_transactions {
|
for (outgoing_kind, events) in initial_transactions {
|
||||||
current_transaction_status
|
current_transaction_status
|
||||||
.insert(outgoing_kind.clone(), TransactionStatus::Running);
|
.insert(outgoing_kind.clone(), TransactionStatus::Running);
|
||||||
futures.push(Self::handle_events(outgoing_kind.clone(), events));
|
futures.push(handle_events(outgoing_kind.clone(), events));
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|
@ -204,7 +204,7 @@ impl Service {
|
||||||
response,
|
response,
|
||||||
&mut current_transaction_status,
|
&mut current_transaction_status,
|
||||||
)? {
|
)? {
|
||||||
futures.push(Self::handle_events(kind, events));
|
futures.push(handle_events(kind, events));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(data) = receiver.recv() => {
|
Some(data) = receiver.recv() => {
|
||||||
|
|
@ -214,7 +214,7 @@ impl Service {
|
||||||
}) = self
|
}) = self
|
||||||
.handle_receiver(data, &mut current_transaction_status)
|
.handle_receiver(data, &mut current_transaction_status)
|
||||||
{
|
{
|
||||||
futures.push(Self::handle_events(kind, events));
|
futures.push(handle_events(kind, events));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -626,11 +626,62 @@ impl Service {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(events))]
|
#[tracing::instrument(skip(self, request))]
|
||||||
async fn handle_appservice_event(
|
pub(crate) async fn send_federation_request<T>(
|
||||||
|
&self,
|
||||||
|
destination: &ServerName,
|
||||||
|
request: T,
|
||||||
|
) -> Result<T::IncomingResponse>
|
||||||
|
where
|
||||||
|
T: OutgoingRequest + Debug,
|
||||||
|
{
|
||||||
|
debug!("Waiting for permit");
|
||||||
|
let permit = self.maximum_requests.acquire().await;
|
||||||
|
debug!("Got permit");
|
||||||
|
let response = tokio::time::timeout(
|
||||||
|
Duration::from_secs(2 * 60),
|
||||||
|
server_server::send_request(destination, request),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|_| {
|
||||||
|
warn!("Timeout waiting for server response of {destination}");
|
||||||
|
Error::BadServerResponse("Timeout waiting for server response")
|
||||||
|
})?;
|
||||||
|
drop(permit);
|
||||||
|
|
||||||
|
response
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends a request to an appservice
|
||||||
|
///
|
||||||
|
/// Only returns None if there is no url specified in the appservice
|
||||||
|
/// registration file
|
||||||
|
#[tracing::instrument(
|
||||||
|
skip(self, registration, request),
|
||||||
|
fields(appservice_id = registration.id),
|
||||||
|
)]
|
||||||
|
pub(crate) async fn send_appservice_request<T>(
|
||||||
|
&self,
|
||||||
|
registration: Registration,
|
||||||
|
request: T,
|
||||||
|
) -> Result<Option<T::IncomingResponse>>
|
||||||
|
where
|
||||||
|
T: OutgoingRequest + Debug,
|
||||||
|
{
|
||||||
|
let permit = self.maximum_requests.acquire().await;
|
||||||
|
let response =
|
||||||
|
appservice_server::send_request(registration, request).await;
|
||||||
|
drop(permit);
|
||||||
|
|
||||||
|
response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(events))]
|
||||||
|
async fn handle_appservice_event(
|
||||||
id: &str,
|
id: &str,
|
||||||
events: Vec<SendingEventType>,
|
events: Vec<SendingEventType>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut pdu_jsons = Vec::new();
|
let mut pdu_jsons = Vec::new();
|
||||||
|
|
||||||
for event in &events {
|
for event in &events {
|
||||||
|
|
@ -643,8 +694,8 @@ impl Service {
|
||||||
.get_pdu_from_id(pdu_id)?
|
.get_pdu_from_id(pdu_id)?
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
Error::bad_database(
|
Error::bad_database(
|
||||||
"[Appservice] Event in \
|
"[Appservice] Event in servernameevent_data \
|
||||||
servernameevent_data not found in db.",
|
not found in db.",
|
||||||
)
|
)
|
||||||
})?
|
})?
|
||||||
.to_room_event(),
|
.to_room_event(),
|
||||||
|
|
@ -659,13 +710,11 @@ impl Service {
|
||||||
let permit = services().sending.maximum_requests.acquire().await;
|
let permit = services().sending.maximum_requests.acquire().await;
|
||||||
|
|
||||||
appservice_server::send_request(
|
appservice_server::send_request(
|
||||||
services().appservice.get_registration(id).await.ok_or_else(
|
services().appservice.get_registration(id).await.ok_or_else(|| {
|
||||||
|| {
|
|
||||||
Error::bad_database(
|
Error::bad_database(
|
||||||
"[Appservice] Could not load registration from db.",
|
"[Appservice] Could not load registration from db.",
|
||||||
)
|
)
|
||||||
},
|
})?,
|
||||||
)?,
|
|
||||||
appservice::event::push_events::v1::Request {
|
appservice::event::push_events::v1::Request {
|
||||||
events: pdu_jsons,
|
events: pdu_jsons,
|
||||||
txn_id: general_purpose::URL_SAFE_NO_PAD
|
txn_id: general_purpose::URL_SAFE_NO_PAD
|
||||||
|
|
@ -686,14 +735,14 @@ impl Service {
|
||||||
drop(permit);
|
drop(permit);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(events))]
|
#[tracing::instrument(skip(events))]
|
||||||
async fn handle_push_event(
|
async fn handle_push_event(
|
||||||
userid: &UserId,
|
userid: &UserId,
|
||||||
pushkey: &str,
|
pushkey: &str,
|
||||||
events: Vec<SendingEventType>,
|
events: Vec<SendingEventType>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut pdus = Vec::new();
|
let mut pdus = Vec::new();
|
||||||
|
|
||||||
for event in &events {
|
for event in &events {
|
||||||
|
|
@ -769,13 +818,13 @@ impl Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip(events))]
|
#[tracing::instrument(skip(events))]
|
||||||
async fn handle_federation_event(
|
async fn handle_federation_event(
|
||||||
server: &ServerName,
|
server: &ServerName,
|
||||||
events: Vec<SendingEventType>,
|
events: Vec<SendingEventType>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut edu_jsons = Vec::new();
|
let mut edu_jsons = Vec::new();
|
||||||
let mut pdu_jsons = Vec::new();
|
let mut pdu_jsons = Vec::new();
|
||||||
|
|
||||||
|
|
@ -784,23 +833,19 @@ impl Service {
|
||||||
SendingEventType::Pdu(pdu_id) => {
|
SendingEventType::Pdu(pdu_id) => {
|
||||||
// TODO: check room version and remove event_id if
|
// TODO: check room version and remove event_id if
|
||||||
// needed
|
// needed
|
||||||
pdu_jsons.push(
|
pdu_jsons.push(PduEvent::convert_to_outgoing_federation_event(
|
||||||
PduEvent::convert_to_outgoing_federation_event(
|
|
||||||
services()
|
services()
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.get_pdu_json_from_id(pdu_id)?
|
.get_pdu_json_from_id(pdu_id)?
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
error!(
|
error!("event not found: {server} {pdu_id:?}");
|
||||||
"event not found: {server} {pdu_id:?}"
|
|
||||||
);
|
|
||||||
Error::bad_database(
|
Error::bad_database(
|
||||||
"[Normal] Event in \
|
"[Normal] Event in servernamevent_datas not \
|
||||||
servernamevent_datas not found in db.",
|
found in db.",
|
||||||
)
|
)
|
||||||
})?,
|
})?,
|
||||||
),
|
));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
SendingEventType::Edu(edu) => {
|
SendingEventType::Edu(edu) => {
|
||||||
if let Ok(raw) = serde_json::from_slice(edu) {
|
if let Ok(raw) = serde_json::from_slice(edu) {
|
||||||
|
|
@ -843,22 +888,22 @@ impl Service {
|
||||||
drop(permit);
|
drop(permit);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip_all)]
|
#[tracing::instrument(skip_all)]
|
||||||
async fn handle_events(
|
async fn handle_events(
|
||||||
kind: OutgoingKind,
|
kind: OutgoingKind,
|
||||||
events: Vec<SendingEventType>,
|
events: Vec<SendingEventType>,
|
||||||
) -> HandlerResponse {
|
) -> HandlerResponse {
|
||||||
let ret = match &kind {
|
let ret = match &kind {
|
||||||
OutgoingKind::Appservice(id) => {
|
OutgoingKind::Appservice(id) => {
|
||||||
Self::handle_appservice_event(id, events).await
|
handle_appservice_event(id, events).await
|
||||||
}
|
}
|
||||||
OutgoingKind::Push(userid, pushkey) => {
|
OutgoingKind::Push(userid, pushkey) => {
|
||||||
Self::handle_push_event(userid, pushkey, events).await
|
handle_push_event(userid, pushkey, events).await
|
||||||
}
|
}
|
||||||
OutgoingKind::Normal(server) => {
|
OutgoingKind::Normal(server) => {
|
||||||
Self::handle_federation_event(server, events).await
|
handle_federation_event(server, events).await
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -866,55 +911,4 @@ impl Service {
|
||||||
Ok(()) => Ok(kind),
|
Ok(()) => Ok(kind),
|
||||||
Err(e) => Err((kind, e)),
|
Err(e) => Err((kind, e)),
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(skip(self, request))]
|
|
||||||
pub(crate) async fn send_federation_request<T>(
|
|
||||||
&self,
|
|
||||||
destination: &ServerName,
|
|
||||||
request: T,
|
|
||||||
) -> Result<T::IncomingResponse>
|
|
||||||
where
|
|
||||||
T: OutgoingRequest + Debug,
|
|
||||||
{
|
|
||||||
debug!("Waiting for permit");
|
|
||||||
let permit = self.maximum_requests.acquire().await;
|
|
||||||
debug!("Got permit");
|
|
||||||
let response = tokio::time::timeout(
|
|
||||||
Duration::from_secs(2 * 60),
|
|
||||||
server_server::send_request(destination, request),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map_err(|_| {
|
|
||||||
warn!("Timeout waiting for server response of {destination}");
|
|
||||||
Error::BadServerResponse("Timeout waiting for server response")
|
|
||||||
})?;
|
|
||||||
drop(permit);
|
|
||||||
|
|
||||||
response
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sends a request to an appservice
|
|
||||||
///
|
|
||||||
/// Only returns None if there is no url specified in the appservice
|
|
||||||
/// registration file
|
|
||||||
#[tracing::instrument(
|
|
||||||
skip(self, registration, request),
|
|
||||||
fields(appservice_id = registration.id),
|
|
||||||
)]
|
|
||||||
pub(crate) async fn send_appservice_request<T>(
|
|
||||||
&self,
|
|
||||||
registration: Registration,
|
|
||||||
request: T,
|
|
||||||
) -> Result<Option<T::IncomingResponse>>
|
|
||||||
where
|
|
||||||
T: OutgoingRequest + Debug,
|
|
||||||
{
|
|
||||||
let permit = self.maximum_requests.acquire().await;
|
|
||||||
let response =
|
|
||||||
appservice_server::send_request(registration, request).await;
|
|
||||||
drop(permit);
|
|
||||||
|
|
||||||
response
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue