Skip to content

Commit 17b5ae4

Browse files
committed
replace async_stream in bgpdumper
1 parent 5ee5213 commit 17b5ae4

File tree

4 files changed

+65
-74
lines changed

4 files changed

+65
-74
lines changed

Cargo.lock

Lines changed: 1 addition & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ repository = "https://github.com/wobcom/fernglas"
1010

1111
[dependencies]
1212
anyhow = "1.0"
13-
async-stream = "0.3"
1413
async-trait = "0.1"
1514
axum = { version = "0.7", default-features = false, features = ["query", "http1", "tokio"] }
1615
bitvec = "1.0"

src/bgp_collector.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ use crate::bgpdumper::BgpDumper;
22
use crate::route_distinguisher::RouteDistinguisher;
33
use crate::store::{Client, RouteState, SessionId, Store, TableSelector, TableType};
44
use futures_util::future::join_all;
5-
use futures_util::{pin_mut, StreamExt};
5+
use futures_util::TryStreamExt;
66
use log::*;
77
use serde::Deserialize;
88
use std::collections::HashMap;
99
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
10+
use std::pin::pin;
1011
use tokio::net::TcpListener;
1112
use tokio::net::TcpStream;
1213
use zettabgp::prelude::BgpNotificationMessage;
@@ -43,8 +44,7 @@ pub async fn run_peer(
4344
stream,
4445
);
4546
let open_message = dumper.start_active().await?;
46-
let stream = dumper.lifecycle();
47-
pin_mut!(stream);
47+
let mut stream = dumper.lifecycle();
4848
let client_name = cfg
4949
.name_override
5050
.or(open_message.caps.iter().find_map(|x| {
@@ -70,11 +70,11 @@ pub async fn run_peer(
7070
)
7171
.await;
7272
loop {
73-
let update = match stream.next().await {
74-
Some(Ok(update)) => update,
75-
Some(Err(Ok(notification))) => break Ok(notification),
76-
Some(Err(Err(e))) => anyhow::bail!(e),
77-
None => panic!(),
73+
let update = match pin!(stream.try_next()).await {
74+
Ok(Some(update)) => update,
75+
Ok(None) => panic!(),
76+
Err(Ok(notification)) => break Ok(notification),
77+
Err(Err(e)) => anyhow::bail!(e),
7878
};
7979
store
8080
.insert_bgp_update(

src/bgpdumper.rs

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// Melnikov, which is licensed under the MIT license.
33

44
use bytes::{Buf, BytesMut};
5-
use futures_util::Stream;
65
use futures_util::StreamExt;
6+
use futures_util::TryStream;
7+
use futures_util::TryStreamExt;
78
use log::*;
89
use std::sync::Arc;
910
use tokio::io::AsyncWriteExt;
@@ -51,7 +52,11 @@ impl BgpDumper {
5152
.params
5253
.prepare_message_buf(&mut buf, BgpMessageType::Open, messagelen)?;
5354
self.write.lock().await.write_all(&buf[0..blen]).await?;
54-
let (msgtype, buf) = self.next_message().await?;
55+
let (msgtype, buf) = self
56+
.messages()
57+
.try_next()
58+
.await?
59+
.ok_or(BgpError::static_str("Expected open message"))?;
5560
if msgtype != BgpMessageType::Open {
5661
return Err(BgpError::static_str("Invalid state to start_active"));
5762
}
@@ -84,51 +89,61 @@ impl BgpDumper {
8489
});
8590
tx
8691
}
87-
async fn next_message(&mut self) -> Result<(BgpMessageType, BytesMut), BgpError> {
88-
let mut buf = self
89-
.read
90-
.next()
91-
.await
92-
.ok_or(BgpError::static_str("unexpected end of stream"))??;
93-
let msg = self.params.decode_message_head(&buf)?;
94-
buf.advance(19);
95-
buf.truncate(msg.1);
96-
Ok((msg.0, buf))
92+
fn messages(
93+
&mut self,
94+
) -> impl TryStream<Ok = (BgpMessageType, BytesMut), Error = BgpError> + Send + Unpin + use<'_>
95+
{
96+
(&mut self.read).map(|buf| {
97+
let mut buf = buf?;
98+
let msg = self.params.decode_message_head(&buf)?;
99+
buf.advance(19);
100+
buf.truncate(msg.1);
101+
Ok((msg.0, buf))
102+
})
97103
}
98104
pub fn lifecycle(
99-
mut self,
100-
) -> impl Stream<Item = Result<BgpUpdateMessage, Result<BgpNotificationMessage, BgpError>>> + Send
101-
{
105+
&mut self,
106+
) -> impl TryStream<Ok = BgpUpdateMessage, Error = Result<BgpNotificationMessage, BgpError>>
107+
+ Unpin
108+
+ Send
109+
+ use<'_> {
102110
self.stop_keepalives = Some(self.start_keepalives());
111+
let params = self.params.clone();
112+
Box::pin(
113+
self.messages()
114+
.map_err(Err)
115+
.try_filter_map(move |(msgtype, buf)| {
116+
let params = params.clone();
117+
async move {
118+
if msgtype == BgpMessageType::Keepalive {
119+
return Ok(None);
120+
}
121+
match msgtype {
122+
BgpMessageType::Open => {
123+
Err(Err(BgpError::static_str("Incorrect open message")))
124+
}
125+
BgpMessageType::Keepalive => Ok(None),
126+
BgpMessageType::Notification => {
127+
let mut msgnotification = BgpNotificationMessage::new();
128+
msgnotification
129+
.decode_from(&params, &buf[..])
130+
.map_err(Err)?;
131+
Err(Ok(msgnotification))
132+
}
133+
BgpMessageType::Update => {
134+
let mut msgupdate = BgpUpdateMessage::new();
135+
if let Err(e) = msgupdate.decode_from(&params, &buf[..]) {
136+
warn!("BGP update decode error: {:?}", e);
137+
warn!("{:x?}", &buf[..]);
138+
return Ok(None);
139+
}
103140

104-
async_stream::try_stream! {
105-
loop {
106-
let (msgtype, buf) = self.next_message().await.map_err(Err)?;
107-
if msgtype == BgpMessageType::Keepalive {
108-
continue;
109-
}
110-
match msgtype {
111-
BgpMessageType::Open => {
112-
Err(Err(BgpError::static_str("Incorrect open message")))?;
113-
}
114-
BgpMessageType::Keepalive => {}
115-
BgpMessageType::Notification => {
116-
let mut msgnotification = BgpNotificationMessage::new();
117-
msgnotification.decode_from(&self.params, &buf[..]).map_err(Err)?;
118-
Err(Ok(msgnotification))?;
119-
}
120-
BgpMessageType::Update => {
121-
let mut msgupdate = BgpUpdateMessage::new();
122-
if let Err(e) = msgupdate.decode_from(&self.params, &buf[..]) {
123-
warn!("BGP update decode error: {:?}", e);
124-
warn!("{:x?}", &buf[..]);
125-
continue;
141+
Ok(Some(msgupdate))
142+
}
126143
}
127-
yield msgupdate;
128144
}
129-
}
130-
}
131-
}
145+
}),
146+
)
132147
}
133148
}
134149
impl Drop for BgpDumper {

0 commit comments

Comments
 (0)