Browse Source

Statically ensure that our only failure mode is `NoConnection`

By creating a dedicated error type, we can communicate that the only
failure mode of this function is a missing connection.
new-http-api
Thomas Eizinger 3 years ago
parent
commit
ca2bd5e291
No known key found for this signature in database GPG Key ID: 651AC83A6C6C8B96
  1. 17
      daemon/src/maker_inc_connections.rs

17
daemon/src/maker_inc_connections.rs

@ -2,7 +2,7 @@ use crate::maker_cfd::{FromTaker, NewTakerOnline};
use crate::model::cfd::{Order, OrderId};
use crate::model::{BitMexPriceEventId, TakerId};
use crate::{forward_only_ok, maker_cfd, noise, send_to_socket, wire, HEARTBEAT_INTERVAL};
use anyhow::{Context as AnyhowContext, Result};
use anyhow::Result;
use futures::{StreamExt, TryStreamExt};
use std::collections::HashMap;
use std::io;
@ -84,11 +84,15 @@ impl Actor {
}
}
async fn send_to_taker(&mut self, taker_id: &TakerId, msg: wire::MakerToTaker) -> Result<()> {
async fn send_to_taker(
&mut self,
taker_id: &TakerId,
msg: wire::MakerToTaker,
) -> Result<(), NoConnection> {
let conn = self
.write_connections
.get(taker_id)
.context("no connection to taker_id")?;
.ok_or_else(|| NoConnection(*taker_id))?;
let msg_str = msg.to_string();
@ -156,6 +160,10 @@ impl Actor {
}
}
#[derive(Debug, thiserror::Error)]
#[error("No connection to taker {0}")]
pub struct NoConnection(TakerId);
#[xtra_productivity]
impl Actor {
async fn handle_broadcast_order(&mut self, msg: BroadcastOrder) {
@ -166,7 +174,7 @@ impl Actor {
}
}
async fn handle_taker_message(&mut self, msg: TakerMessage) -> Result<()> {
async fn handle_taker_message(&mut self, msg: TakerMessage) -> Result<(), NoConnection> {
match msg.command {
TakerCommand::SendOrder { order } => {
self.send_to_taker(&msg.taker_id, wire::MakerToTaker::CurrentOrder(order))
@ -221,6 +229,7 @@ impl Actor {
.await?;
}
}
Ok(())
}

Loading…
Cancel
Save