deploy: 283be077760a8348dabb5c1c112c6f837fbd48e9

This commit is contained in:
dignifiedquire
2024-12-26 13:44:52 +00:00
parent 46db223576
commit eeca6a4305
2 changed files with 143 additions and 253 deletions
@@ -672,7 +672,51 @@
<a href="#671" id="671">671</a>
<a href="#672" id="672">672</a>
<a href="#673" id="673">673</a>
<a href="#674" id="674">674</a></pre></div><pre class="rust"><code><span class="doccomment">//! The server-side representation of an ongoing client relaying connection.
<a href="#674" id="674">674</a>
<a href="#675" id="675">675</a>
<a href="#676" id="676">676</a>
<a href="#677" id="677">677</a>
<a href="#678" id="678">678</a>
<a href="#679" id="679">679</a>
<a href="#680" id="680">680</a>
<a href="#681" id="681">681</a>
<a href="#682" id="682">682</a>
<a href="#683" id="683">683</a>
<a href="#684" id="684">684</a>
<a href="#685" id="685">685</a>
<a href="#686" id="686">686</a>
<a href="#687" id="687">687</a>
<a href="#688" id="688">688</a>
<a href="#689" id="689">689</a>
<a href="#690" id="690">690</a>
<a href="#691" id="691">691</a>
<a href="#692" id="692">692</a>
<a href="#693" id="693">693</a>
<a href="#694" id="694">694</a>
<a href="#695" id="695">695</a>
<a href="#696" id="696">696</a>
<a href="#697" id="697">697</a>
<a href="#698" id="698">698</a>
<a href="#699" id="699">699</a>
<a href="#700" id="700">700</a>
<a href="#701" id="701">701</a>
<a href="#702" id="702">702</a>
<a href="#703" id="703">703</a>
<a href="#704" id="704">704</a>
<a href="#705" id="705">705</a>
<a href="#706" id="706">706</a>
<a href="#707" id="707">707</a>
<a href="#708" id="708">708</a>
<a href="#709" id="709">709</a>
<a href="#710" id="710">710</a>
<a href="#711" id="711">711</a>
<a href="#712" id="712">712</a>
<a href="#713" id="713">713</a>
<a href="#714" id="714">714</a>
<a href="#715" id="715">715</a>
<a href="#716" id="716">716</a>
<a href="#717" id="717">717</a>
<a href="#718" id="718">718</a></pre></div><pre class="rust"><code><span class="doccomment">//! The server-side representation of an ongoing client relaying connection.
</span><span class="kw">use </span>std::{future::Future, num::NonZeroU32, pin::Pin, sync::Arc, task::Poll, time::Duration};
@@ -705,6 +749,9 @@
</span><span class="kw">pub</span>(<span class="kw">super</span>) data: Bytes,
}
<span class="doccomment">/// Number of times we try to send to a client connection before dropping the data;
</span><span class="kw">const </span>RETRIES: usize = <span class="number">3</span>;
<span class="doccomment">/// Configuration for a [`ClientConn`].
</span><span class="attr">#[derive(Debug)]
</span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">struct </span>ClientConnConfig {
@@ -721,29 +768,27 @@
/// [`Client`]: crate::client::Client
</span><span class="attr">#[derive(Debug)]
</span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">struct </span>ClientConn {
<span class="doccomment">/// Unique counter, incremented each time we accept a new connection.
</span><span class="kw">pub</span>(<span class="kw">super</span>) conn_num: usize,
<span class="doccomment">/// Identity of the connected peer.
</span><span class="kw">pub</span>(<span class="kw">super</span>) key: NodeId,
</span>node_id: NodeId,
<span class="doccomment">/// Used to close the connection loop.
</span>done: CancellationToken,
<span class="doccomment">/// Actor handle.
</span>handle: AbortOnDropHandle&lt;()&gt;,
<span class="doccomment">/// Queue of packets intended for the client.
</span><span class="kw">pub</span>(<span class="kw">super</span>) send_queue: mpsc::Sender&lt;Packet&gt;,
</span>send_queue: mpsc::Sender&lt;Packet&gt;,
<span class="doccomment">/// Queue of disco packets intended for the client.
</span><span class="kw">pub</span>(<span class="kw">super</span>) disco_send_queue: mpsc::Sender&lt;Packet&gt;,
</span>disco_send_queue: mpsc::Sender&lt;Packet&gt;,
<span class="doccomment">/// Channel to notify the client that a previous sender has disconnected.
</span><span class="kw">pub</span>(<span class="kw">super</span>) peer_gone: mpsc::Sender&lt;NodeId&gt;,
</span>peer_gone: mpsc::Sender&lt;NodeId&gt;,
}
<span class="kw">impl </span>ClientConn {
<span class="doccomment">/// Creates a client from a connection &amp; starts a read and write loop to handle io to and from
/// the client
/// Call [`ClientConn::shutdown`] to close the read and write loops before dropping the [`ClientConn`]
</span><span class="kw">pub fn </span>new(config: ClientConnConfig, clients: <span class="kw-2">&amp;</span>Clients, conn_num: usize) -&gt; ClientConn {
</span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>new(config: ClientConnConfig, clients: <span class="kw-2">&amp;</span>Clients) -&gt; ClientConn {
<span class="kw">let </span>ClientConnConfig {
node_id: key,
node_id,
stream: io,
write_timeout,
channel_capacity,
@@ -763,7 +808,6 @@
};
<span class="kw">let </span>done = CancellationToken::new();
<span class="kw">let </span>client_id = (key, conn_num);
<span class="kw">let </span>(send_queue_s, send_queue_r) = mpsc::channel(channel_capacity);
<span class="kw">let </span>(disco_send_queue_s, disco_send_queue_r) = mpsc::channel(channel_capacity);
@@ -775,25 +819,23 @@
send_queue: send_queue_r,
disco_send_queue: disco_send_queue_r,
node_gone: peer_gone_r,
key,
node_id,
preferred: <span class="bool-val">false</span>,
clients: clients.clone(),
};
<span class="comment">// start io loop
</span><span class="kw">let </span>io_done = done.clone();
<span class="kw">let </span>io_client_id = client_id;
<span class="kw">let </span>io_client_id = node_id;
<span class="kw">let </span>handle = tokio::task::spawn(
<span class="kw">async move </span>{
<span class="kw">let </span>(key, conn_num) = io_client_id;
<span class="kw">let </span>key = io_client_id;
<span class="kw">match </span>actor.run(io_done).<span class="kw">await </span>{
<span class="prelude-val">Err</span>(e) =&gt; {
<span class="macro">warn!</span>(
<span class="string">"connection manager for {key:?} {conn_num}: writer closed in error {e}"
</span>);
<span class="macro">warn!</span>(<span class="string">"connection manager for {key:?}: writer closed in error {e}"</span>);
}
<span class="prelude-val">Ok</span>(()) =&gt; {
<span class="macro">info!</span>(<span class="string">"connection manager for {key:?} {conn_num}: writer closed"</span>);
<span class="macro">info!</span>(<span class="string">"connection manager for {key:?}: writer closed"</span>);
}
}
}
@@ -801,8 +843,7 @@
);
ClientConn {
conn_num,
key,
node_id,
handle: AbortOnDropHandle::new(handle),
done,
send_queue: send_queue_s,
@@ -814,15 +855,60 @@
<span class="doccomment">/// Shutdown the reader and writer loops and closes the connection.
///
/// Any shutdown errors will be logged as warnings.
</span><span class="kw">pub async fn </span>shutdown(<span class="self">self</span>) {
</span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">async fn </span>shutdown(<span class="self">self</span>) {
<span class="self">self</span>.done.cancel();
<span class="kw">if let </span><span class="prelude-val">Err</span>(e) = <span class="self">self</span>.handle.<span class="kw">await </span>{
<span class="macro">warn!</span>(
<span class="string">"error closing actor loop for client connection {:?} {}: {e:?}"</span>,
<span class="self">self</span>.key, <span class="self">self</span>.conn_num
<span class="string">"error closing actor loop for client connection {:?}: {e:?}"</span>,
<span class="self">self</span>.node_id,
);
};
}
<span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>send_packet(<span class="kw-2">&amp;</span><span class="self">self</span>, src: NodeId, data: Bytes) -&gt; <span class="prelude-ty">Result</span>&lt;(), SendError&gt; {
try_send(<span class="kw-2">&amp;</span><span class="self">self</span>.send_queue, Packet { src, data })
}
<span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>send_disco_packet(<span class="kw-2">&amp;</span><span class="self">self</span>, src: NodeId, data: Bytes) -&gt; <span class="prelude-ty">Result</span>&lt;(), SendError&gt; {
try_send(<span class="kw-2">&amp;</span><span class="self">self</span>.disco_send_queue, Packet { src, data })
}
<span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">fn </span>send_peer_gone(<span class="kw-2">&amp;</span><span class="self">self</span>, key: NodeId) -&gt; <span class="prelude-ty">Result</span>&lt;(), SendError&gt; {
<span class="kw">let </span>res = try_send(<span class="kw-2">&amp;</span><span class="self">self</span>.peer_gone, key);
<span class="kw">match </span>res {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) =&gt; {
<span class="macro">inc!</span>(Metrics, other_packets_sent);
}
<span class="prelude-val">Err</span>(<span class="kw">_</span>) =&gt; {
<span class="macro">inc!</span>(Metrics, other_packets_dropped);
}
}
res
}
}
<span class="attr">#[derive(Debug, thiserror::Error)]
</span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">enum </span>SendError {
<span class="attr">#[error(<span class="string">"packet dropped"</span>)]
</span>PacketDropped,
<span class="attr">#[error(<span class="string">"sender closed"</span>)]
</span>SenderClosed,
}
<span class="doccomment">/// Tries up to `3` times to send a message into the given channel, retrying iff it is full.
</span><span class="kw">fn </span>try_send&lt;T&gt;(sender: <span class="kw-2">&amp;</span>mpsc::Sender&lt;T&gt;, msg: T) -&gt; <span class="prelude-ty">Result</span>&lt;(), SendError&gt; {
<span class="kw">let </span><span class="kw-2">mut </span>msg = msg;
<span class="kw">for _ in </span><span class="number">0</span>..RETRIES {
<span class="kw">match </span>sender.try_send(msg) {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) =&gt; <span class="kw">return </span><span class="prelude-val">Ok</span>(()),
<span class="comment">// if the queue is full, try again (max 3 times)
</span><span class="prelude-val">Err</span>(mpsc::error::TrySendError::Full(m)) =&gt; msg = m,
<span class="comment">// only other option is `TrySendError::Closed`, report the
// closed error
</span><span class="prelude-val">Err</span>(<span class="kw">_</span>) =&gt; <span class="kw">return </span><span class="prelude-val">Err</span>(SendError::SenderClosed),
}
}
<span class="prelude-val">Err</span>(SendError::PacketDropped)
}
<span class="doccomment">/// Manages all the reads and writes to this client. It periodically sends a `KEEP_ALIVE`
@@ -856,7 +942,7 @@
<span class="doccomment">/// Notify the client that a previous sender has disconnected
</span>node_gone: mpsc::Receiver&lt;NodeId&gt;,
<span class="doccomment">/// [`NodeId`] of this client
</span>key: NodeId,
</span>node_id: NodeId,
<span class="doccomment">/// Reference to the other connected clients.
</span>clients: Clients,
<span class="doccomment">/// Notes that the client considers this the preferred connection (important in cases
@@ -978,10 +1064,12 @@
<span class="kw">async fn </span>handle_frame_send_packet(<span class="kw-2">&amp;</span><span class="self">self</span>, dst: NodeId, data: Bytes) -&gt; <span class="prelude-ty">Result</span>&lt;()&gt; {
<span class="kw">if </span>disco::looks_like_disco_wrapper(<span class="kw-2">&amp;</span>data) {
<span class="macro">inc!</span>(Metrics, disco_packets_recv);
<span class="self">self</span>.clients.send_disco_packet(dst, data, <span class="self">self</span>.key).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="self">self</span>.clients
.send_disco_packet(dst, data, <span class="self">self</span>.node_id)
.<span class="kw">await</span><span class="question-mark">?</span>;
} <span class="kw">else </span>{
<span class="macro">inc!</span>(Metrics, send_packets_recv);
<span class="self">self</span>.clients.send_packet(dst, data, <span class="self">self</span>.key).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="self">self</span>.clients.send_packet(dst, data, <span class="self">self</span>.node_id).<span class="kw">await</span><span class="question-mark">?</span>;
}
<span class="prelude-val">Ok</span>(())
}
@@ -1183,7 +1271,7 @@
<span class="kw">let </span>(disco_send_queue_s, disco_send_queue_r) = mpsc::channel(<span class="number">10</span>);
<span class="kw">let </span>(peer_gone_s, peer_gone_r) = mpsc::channel(<span class="number">10</span>);
<span class="kw">let </span>key = SecretKey::generate(rand::thread_rng()).public();
<span class="kw">let </span>node_id = SecretKey::generate(rand::thread_rng()).public();
<span class="kw">let </span>(io, io_rw) = tokio::io::duplex(<span class="number">1024</span>);
<span class="kw">let </span><span class="kw-2">mut </span>io_rw = Framed::new(io_rw, RelayCodec::test());
<span class="kw">let </span>stream =
@@ -1196,7 +1284,7 @@
send_queue: send_queue_r,
disco_send_queue: disco_send_queue_r,
node_gone: peer_gone_r,
key,
node_id,
clients: clients.clone(),
preferred: <span class="bool-val">true</span>,
};
@@ -1212,7 +1300,7 @@
<span class="comment">// send packet
</span><span class="macro">println!</span>(<span class="string">" send packet"</span>);
<span class="kw">let </span>packet = Packet {
src: key,
src: node_id,
data: Bytes::from(<span class="kw-2">&amp;</span>data[..]),
};
send_queue_s.send(packet.clone()).<span class="kw">await</span><span class="question-mark">?</span>;
@@ -1220,7 +1308,7 @@
<span class="macro">assert_eq!</span>(
frame,
Frame::RecvPacket {
src_key: key,
src_key: node_id,
content: data.to_vec().into()
}
);
@@ -1232,16 +1320,16 @@
<span class="macro">assert_eq!</span>(
frame,
Frame::RecvPacket {
src_key: key,
src_key: node_id,
content: data.to_vec().into()
}
);
<span class="comment">// send peer_gone
</span><span class="macro">println!</span>(<span class="string">"send peer gone"</span>);
peer_gone_s.send(key).<span class="kw">await</span><span class="question-mark">?</span>;
peer_gone_s.send(node_id).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="kw">let </span>frame = recv_frame(FrameType::PeerGone, <span class="kw-2">&amp;mut </span>io_rw).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="macro">assert_eq!</span>(frame, Frame::NodeGone { node_id: key });
<span class="macro">assert_eq!</span>(frame, Frame::NodeGone { node_id });
<span class="comment">// Read tests
</span><span class="macro">println!</span>(<span class="string">"--read"</span>);
@@ -192,128 +192,18 @@
<a href="#191" id="191">191</a>
<a href="#192" id="192">192</a>
<a href="#193" id="193">193</a>
<a href="#194" id="194">194</a>
<a href="#195" id="195">195</a>
<a href="#196" id="196">196</a>
<a href="#197" id="197">197</a>
<a href="#198" id="198">198</a>
<a href="#199" id="199">199</a>
<a href="#200" id="200">200</a>
<a href="#201" id="201">201</a>
<a href="#202" id="202">202</a>
<a href="#203" id="203">203</a>
<a href="#204" id="204">204</a>
<a href="#205" id="205">205</a>
<a href="#206" id="206">206</a>
<a href="#207" id="207">207</a>
<a href="#208" id="208">208</a>
<a href="#209" id="209">209</a>
<a href="#210" id="210">210</a>
<a href="#211" id="211">211</a>
<a href="#212" id="212">212</a>
<a href="#213" id="213">213</a>
<a href="#214" id="214">214</a>
<a href="#215" id="215">215</a>
<a href="#216" id="216">216</a>
<a href="#217" id="217">217</a>
<a href="#218" id="218">218</a>
<a href="#219" id="219">219</a>
<a href="#220" id="220">220</a>
<a href="#221" id="221">221</a>
<a href="#222" id="222">222</a>
<a href="#223" id="223">223</a>
<a href="#224" id="224">224</a>
<a href="#225" id="225">225</a>
<a href="#226" id="226">226</a>
<a href="#227" id="227">227</a>
<a href="#228" id="228">228</a>
<a href="#229" id="229">229</a>
<a href="#230" id="230">230</a>
<a href="#231" id="231">231</a>
<a href="#232" id="232">232</a>
<a href="#233" id="233">233</a>
<a href="#234" id="234">234</a>
<a href="#235" id="235">235</a>
<a href="#236" id="236">236</a>
<a href="#237" id="237">237</a>
<a href="#238" id="238">238</a>
<a href="#239" id="239">239</a>
<a href="#240" id="240">240</a>
<a href="#241" id="241">241</a>
<a href="#242" id="242">242</a>
<a href="#243" id="243">243</a>
<a href="#244" id="244">244</a>
<a href="#245" id="245">245</a>
<a href="#246" id="246">246</a>
<a href="#247" id="247">247</a>
<a href="#248" id="248">248</a>
<a href="#249" id="249">249</a>
<a href="#250" id="250">250</a>
<a href="#251" id="251">251</a>
<a href="#252" id="252">252</a>
<a href="#253" id="253">253</a>
<a href="#254" id="254">254</a>
<a href="#255" id="255">255</a>
<a href="#256" id="256">256</a>
<a href="#257" id="257">257</a>
<a href="#258" id="258">258</a>
<a href="#259" id="259">259</a>
<a href="#260" id="260">260</a>
<a href="#261" id="261">261</a>
<a href="#262" id="262">262</a>
<a href="#263" id="263">263</a>
<a href="#264" id="264">264</a>
<a href="#265" id="265">265</a>
<a href="#266" id="266">266</a>
<a href="#267" id="267">267</a>
<a href="#268" id="268">268</a>
<a href="#269" id="269">269</a>
<a href="#270" id="270">270</a>
<a href="#271" id="271">271</a>
<a href="#272" id="272">272</a>
<a href="#273" id="273">273</a>
<a href="#274" id="274">274</a>
<a href="#275" id="275">275</a>
<a href="#276" id="276">276</a>
<a href="#277" id="277">277</a>
<a href="#278" id="278">278</a>
<a href="#279" id="279">279</a>
<a href="#280" id="280">280</a>
<a href="#281" id="281">281</a>
<a href="#282" id="282">282</a>
<a href="#283" id="283">283</a>
<a href="#284" id="284">284</a>
<a href="#285" id="285">285</a>
<a href="#286" id="286">286</a>
<a href="#287" id="287">287</a>
<a href="#288" id="288">288</a>
<a href="#289" id="289">289</a>
<a href="#290" id="290">290</a>
<a href="#291" id="291">291</a>
<a href="#292" id="292">292</a>
<a href="#293" id="293">293</a></pre></div><pre class="rust"><code><span class="doccomment">//! The "Server" side of the client. Uses the `ClientConnManager`.
<a href="#194" id="194">194</a></pre></div><pre class="rust"><code><span class="doccomment">//! The "Server" side of the client. Uses the `ClientConnManager`.
</span><span class="comment">// Based on tailscale/derp/derp_server.go
</span><span class="kw">use </span>std::{
collections::HashSet,
sync::{atomic::AtomicUsize, Arc},
};
</span><span class="kw">use </span>std::{collections::HashSet, sync::Arc};
<span class="kw">use </span>anyhow::{bail, <span class="prelude-ty">Result</span>};
<span class="kw">use </span>bytes::Bytes;
<span class="kw">use </span>dashmap::DashMap;
<span class="kw">use </span>iroh_base::NodeId;
<span class="kw">use </span>iroh_metrics::inc;
<span class="kw">use </span>tokio::sync::mpsc;
<span class="kw">use </span>tracing::{trace, warn};
<span class="kw">use super</span>::{
client_conn::{ClientConn, ClientConnConfig, Packet},
metrics::Metrics,
};
<span class="doccomment">/// Number of times we try to send to a client connection before dropping the data;
</span><span class="kw">const </span>RETRIES: usize = <span class="number">3</span>;
<span class="kw">use </span><span class="kw">super</span>::client_conn::{ClientConn, ClientConnConfig, SendError};
<span class="doccomment">/// Manages the connections to all currently connected clients.
</span><span class="attr">#[derive(Debug, Default, Clone)]
@@ -322,9 +212,9 @@
<span class="attr">#[derive(Debug, Default)]
</span><span class="kw">struct </span>Inner {
<span class="doccomment">/// The list of all currently connected clients.
</span>clients: DashMap&lt;NodeId, Client&gt;,
<span class="doccomment">/// The next connection number to use.
</span>conn_num: AtomicUsize,
</span>clients: DashMap&lt;NodeId, ClientConn&gt;,
<span class="doccomment">/// Map of which client has sent where
</span>sent_to: DashMap&lt;NodeId, HashSet&lt;NodeId&gt;&gt;,
}
<span class="kw">impl </span>Clients {
@@ -339,22 +229,11 @@
.<span class="kw">await</span>;
}
<span class="kw">fn </span>next_conn_num(<span class="kw-2">&amp;</span><span class="self">self</span>) -&gt; usize {
<span class="self">self</span>.<span class="number">0
</span>.conn_num
.fetch_add(<span class="number">1</span>, std::sync::atomic::Ordering::Relaxed)
}
<span class="doccomment">/// Builds the client handler and starts the read &amp; write loops for the connection.
</span><span class="kw">pub async fn </span>register(<span class="kw-2">&amp;</span><span class="self">self</span>, client_config: ClientConnConfig) {
<span class="kw">let </span>key = client_config.node_id;
<span class="macro">trace!</span>(<span class="string">"registering client: {:?}"</span>, key);
<span class="kw">let </span>conn_num = <span class="self">self</span>.next_conn_num();
<span class="kw">let </span>client = ClientConn::new(client_config, <span class="self">self</span>, conn_num);
<span class="comment">// TODO: in future, do not remove clients that share a NodeId, instead,
// expand the `Client` struct to handle multiple connections &amp; a policy for
// how to handle who we write to when multiple connections exist.
</span><span class="kw">let </span>client = Client::new(client);
<span class="kw">let </span>client = ClientConn::new(client_config, <span class="self">self</span>);
<span class="kw">if let </span><span class="prelude-val">Some</span>(old_client) = <span class="self">self</span>.<span class="number">0</span>.clients.insert(key, client) {
<span class="macro">warn!</span>(<span class="string">"multiple connections found for {key:?}, pruning old connection"</span>,);
old_client.shutdown().<span class="kw">await</span>;
@@ -364,24 +243,19 @@
<span class="doccomment">/// Removes the client from the map of clients, &amp; sends a notification
/// to each client that peers has sent data to, to let them know that
/// peer is gone from the network.
</span><span class="kw">pub async fn </span>unregister(<span class="kw-2">&amp;</span><span class="self">self</span>, dst: NodeId, conn_num: <span class="prelude-ty">Option</span>&lt;usize&gt;) {
</span><span class="kw">async fn </span>unregister(<span class="kw-2">&amp;</span><span class="self">self</span>, dst: NodeId) {
<span class="macro">trace!</span>(<span class="string">"unregistering client: {:?}"</span>, dst);
<span class="kw">if let </span><span class="prelude-val">Some</span>((<span class="kw">_</span>, client)) = <span class="self">self</span>.<span class="number">0</span>.clients.remove(<span class="kw-2">&amp;</span>dst) {
<span class="kw">if let </span><span class="prelude-val">Some</span>(conn_num) = conn_num {
<span class="kw">if </span>client.conn.conn_num != conn_num {
<span class="comment">// put it back
</span><span class="self">self</span>.<span class="number">0</span>.clients.insert(dst, client);
<span class="kw">return</span>;
}
}
<span class="kw">for </span>key <span class="kw">in </span>client.sent_to.iter() {
<span class="kw">match </span>client.send_peer_gone(dst) {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) =&gt; {}
<span class="prelude-val">Err</span>(SendError::PacketDropped) =&gt; {
<span class="macro">warn!</span>(<span class="string">"client {key:?} too busy to receive packet, dropping packet"</span>);
}
<span class="prelude-val">Err</span>(SendError::SenderClosed) =&gt; {
<span class="macro">warn!</span>(<span class="string">"Can no longer write to client {key:?}"</span>);
<span class="kw">if let </span><span class="prelude-val">Some</span>((<span class="kw">_</span>, sent_to)) = <span class="self">self</span>.<span class="number">0</span>.sent_to.remove(<span class="kw-2">&amp;</span>dst) {
<span class="kw">for </span>key <span class="kw">in </span>sent_to {
<span class="kw">match </span>client.send_peer_gone(key) {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) =&gt; {}
<span class="prelude-val">Err</span>(SendError::PacketDropped) =&gt; {
<span class="macro">warn!</span>(<span class="string">"client {key:?} too busy to receive packet, dropping packet"</span>);
}
<span class="prelude-val">Err</span>(SendError::SenderClosed) =&gt; {
<span class="macro">warn!</span>(<span class="string">"Can no longer write to client {key:?}"</span>);
}
}
}
}
@@ -415,9 +289,9 @@
) -&gt; <span class="prelude-ty">Result</span>&lt;()&gt; {
<span class="kw">match </span>res {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) =&gt; {
<span class="kw">if let </span><span class="prelude-val">Some</span>(<span class="kw-2">ref mut </span>client) = <span class="self">self</span>.<span class="number">0</span>.clients.get_mut(<span class="kw-2">&amp;</span>src) {
client.record_send(dst);
}
<span class="comment">// Record send_to relationship
</span><span class="kw">let </span><span class="kw-2">mut </span>e = <span class="self">self</span>.<span class="number">0</span>.sent_to.entry(src).or_default();
e.insert(dst);
<span class="kw">return </span><span class="prelude-val">Ok</span>(());
}
<span class="prelude-val">Err</span>(SendError::PacketDropped) =&gt; {
@@ -425,85 +299,13 @@
}
<span class="prelude-val">Err</span>(SendError::SenderClosed) =&gt; {
<span class="macro">warn!</span>(<span class="string">"Can no longer write to client {dst:?}, dropping message and pruning connection"</span>);
<span class="self">self</span>.unregister(dst, <span class="prelude-val">None</span>).<span class="kw">await</span>;
<span class="self">self</span>.unregister(dst).<span class="kw">await</span>;
}
}
<span class="macro">bail!</span>(<span class="string">"unable to send msg"</span>);
}
}
<span class="doccomment">/// Represents a connection to a client.
</span><span class="comment">// TODO: expand to allow for _multiple connections_ associated with a single NodeId. This
// introduces some questions around which connection should be prioritized when forwarding packets
</span><span class="attr">#[derive(Debug)]
</span><span class="kw">pub</span>(<span class="kw">super</span>) <span class="kw">struct </span>Client {
<span class="doccomment">/// The client connection associated with the [`NodeId`]
</span>conn: ClientConn,
<span class="doccomment">/// list of peers we have sent messages to
</span>sent_to: HashSet&lt;NodeId&gt;,
}
<span class="kw">impl </span>Client {
<span class="kw">fn </span>new(conn: ClientConn) -&gt; <span class="self">Self </span>{
<span class="self">Self </span>{
conn,
sent_to: HashSet::default(),
}
}
<span class="doccomment">/// Record that this client sent a packet to the `dst` client
</span><span class="kw">fn </span>record_send(<span class="kw-2">&amp;mut </span><span class="self">self</span>, dst: NodeId) {
<span class="self">self</span>.sent_to.insert(dst);
}
<span class="kw">async fn </span>shutdown(<span class="self">self</span>) {
<span class="self">self</span>.conn.shutdown().<span class="kw">await</span>;
}
<span class="kw">fn </span>send_packet(<span class="kw-2">&amp;</span><span class="self">self</span>, src: NodeId, data: Bytes) -&gt; <span class="prelude-ty">Result</span>&lt;(), SendError&gt; {
try_send(<span class="kw-2">&amp;</span><span class="self">self</span>.conn.send_queue, Packet { src, data })
}
<span class="kw">fn </span>send_disco_packet(<span class="kw-2">&amp;</span><span class="self">self</span>, src: NodeId, data: Bytes) -&gt; <span class="prelude-ty">Result</span>&lt;(), SendError&gt; {
try_send(<span class="kw-2">&amp;</span><span class="self">self</span>.conn.disco_send_queue, Packet { src, data })
}
<span class="kw">fn </span>send_peer_gone(<span class="kw-2">&amp;</span><span class="self">self</span>, key: NodeId) -&gt; <span class="prelude-ty">Result</span>&lt;(), SendError&gt; {
<span class="kw">let </span>res = try_send(<span class="kw-2">&amp;</span><span class="self">self</span>.conn.peer_gone, key);
<span class="kw">match </span>res {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) =&gt; {
<span class="macro">inc!</span>(Metrics, other_packets_sent);
}
<span class="prelude-val">Err</span>(<span class="kw">_</span>) =&gt; {
<span class="macro">inc!</span>(Metrics, other_packets_dropped);
}
}
res
}
}
<span class="doccomment">/// Tries up to `3` times to send a message into the given channel, retrying iff it is full.
</span><span class="kw">fn </span>try_send&lt;T&gt;(sender: <span class="kw-2">&amp;</span>mpsc::Sender&lt;T&gt;, msg: T) -&gt; <span class="prelude-ty">Result</span>&lt;(), SendError&gt; {
<span class="kw">let </span><span class="kw-2">mut </span>msg = msg;
<span class="kw">for _ in </span><span class="number">0</span>..RETRIES {
<span class="kw">match </span>sender.try_send(msg) {
<span class="prelude-val">Ok</span>(<span class="kw">_</span>) =&gt; <span class="kw">return </span><span class="prelude-val">Ok</span>(()),
<span class="comment">// if the queue is full, try again (max 3 times)
</span><span class="prelude-val">Err</span>(mpsc::error::TrySendError::Full(m)) =&gt; msg = m,
<span class="comment">// only other option is `TrySendError::Closed`, report the
// closed error
</span><span class="prelude-val">Err</span>(<span class="kw">_</span>) =&gt; <span class="kw">return </span><span class="prelude-val">Err</span>(SendError::SenderClosed),
}
}
<span class="prelude-val">Err</span>(SendError::PacketDropped)
}
<span class="attr">#[derive(Debug)]
</span><span class="kw">enum </span>SendError {
PacketDropped,
SenderClosed,
}
<span class="attr">#[cfg(test)]
</span><span class="kw">mod </span>tests {
<span class="kw">use </span>std::time::Duration;
@@ -576,7 +378,7 @@
);
<span class="comment">// send peer_gone
</span>clients.unregister(a_key, <span class="prelude-val">None</span>).<span class="kw">await</span>;
</span>clients.unregister(a_key).<span class="kw">await</span>;
<span class="macro">assert!</span>(!clients.<span class="number">0</span>.clients.contains_key(<span class="kw-2">&amp;</span>a_key));
clients.shutdown().<span class="kw">await</span>;