Skip to content

Commit fe5baf8

Browse files
authored
Add connection hints to HELLO response (#239)
* Add connection hints to HELLO response * Make field optional
1 parent 1f146ac commit fe5baf8

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

lib/src/bolt/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ mod structs;
1414
mod summary;
1515

1616
pub use request::{
17-
Begin, Commit, Discard, Goodbye, Hello, HelloBuilder, Pull, Reset, Rollback, WrapExtra,
17+
Begin, Commit, ConnectionsHints, Discard, Goodbye, Hello, HelloBuilder, Pull, Reset, Rollback,
18+
WrapExtra,
1819
};
1920
pub use structs::{
2021
Bolt, BoltRef, Date, DateDuration, DateTime, DateTimeZoneId, DateTimeZoneIdRef, Duration,
@@ -24,7 +25,7 @@ pub use structs::{
2425
};
2526
pub use summary::{Failure, Success, Summary};
2627

27-
use crate::packstream::{self, de, from_bytes, from_bytes_ref, ser, to_bytes, Data};
28+
use crate::packstream::{de, from_bytes, from_bytes_ref, ser, to_bytes, Data};
2829

2930
pub(crate) trait Message: Serialize {
3031
/// Serialize this type into a packstream encoded byte slice.

lib/src/bolt/request/hello.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::{borrow::Borrow, collections::HashMap};
1+
use std::borrow::Borrow;
22

33
use crate::{
44
bolt::{ExpectedResponse, Summary},
55
Version,
66
};
7-
use serde::{ser::SerializeMap, Deserialize, Serialize};
7+
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize};
88

99
#[derive(Debug, Clone, PartialEq, Eq)]
1010
pub struct Hello<'a> {
@@ -119,6 +119,13 @@ impl Serialize for ServerRouting<'_> {
119119
pub struct Response {
120120
pub(crate) server: String,
121121
pub(crate) connection_id: String,
122+
pub(crate) hints: Option<ConnectionsHints>,
123+
}
124+
125+
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
126+
pub struct ConnectionsHints {
127+
#[serde(rename = "connection.recv_timeout_seconds")]
128+
connection_recv_timeout_seconds: Option<u32>,
122129
}
123130

124131
impl ExpectedResponse for Hello<'_> {
@@ -255,4 +262,33 @@ mod tests {
255262
assert_eq!(response.server, "Neo4j/4.1.4");
256263
assert_eq!(response.connection_id, "bolt-31");
257264
}
265+
266+
#[test]
267+
fn parse_with_hints() {
268+
let data = bolt()
269+
.tiny_map(3)
270+
.tiny_string("server")
271+
.tiny_string("Neo4j/4.1.4")
272+
.tiny_string("connection_id")
273+
.tiny_string("bolt-31")
274+
.tiny_string("hints")
275+
.tiny_map(1)
276+
.string8("connection.recv_timeout_seconds")
277+
.tiny_int(120)
278+
.build();
279+
280+
let response = Response::parse(data).unwrap();
281+
282+
assert_eq!(response.server, "Neo4j/4.1.4");
283+
assert_eq!(response.connection_id, "bolt-31");
284+
assert!(response.hints.is_some());
285+
assert_eq!(
286+
response
287+
.hints
288+
.unwrap()
289+
.connection_recv_timeout_seconds
290+
.unwrap(),
291+
120
292+
);
293+
}
258294
}

lib/src/bolt/request/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub use commit::Commit;
1414
pub use discard::Discard;
1515
pub use extra::WrapExtra;
1616
pub use goodbye::Goodbye;
17-
pub use hello::{Hello, HelloBuilder};
17+
pub use hello::{ConnectionsHints, Hello, HelloBuilder};
1818
pub use pull::Pull;
1919
pub use reset::Reset;
2020
pub use rollback::Rollback;

lib/src/connection.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::messages::HelloBuilder;
44
#[cfg(feature = "unstable-bolt-protocol-impl-v2")]
55
use {
66
crate::bolt::{
7-
ExpectedResponse, Hello, HelloBuilder, Message, MessageResponse, Reset, Summary,
7+
ConnectionsHints, ExpectedResponse, Hello, HelloBuilder, Message, MessageResponse, Reset,
8+
Summary,
89
},
910
log::debug,
1011
};
@@ -45,6 +46,9 @@ const MAX_CHUNK_SIZE: usize = 65_535 - mem::size_of::<u16>();
4546
pub struct Connection {
4647
version: Version,
4748
stream: BufStream<ConnectionStream>,
49+
#[allow(unused)]
50+
#[cfg(feature = "unstable-bolt-protocol-impl-v2")]
51+
hints: Option<ConnectionsHints>,
4852
}
4953

5054
impl Connection {
@@ -94,6 +98,8 @@ impl Connection {
9498
Connection {
9599
version,
96100
stream: BufStream::new(stream.into()),
101+
#[cfg(feature = "unstable-bolt-protocol-impl-v2")]
102+
hints: None,
97103
}
98104
}
99105

@@ -120,7 +126,10 @@ impl Connection {
120126
let hello = self.send_recv_as(hello).await?;
121127

122128
match hello {
123-
Summary::Success(_msg) => Ok(()),
129+
Summary::Success(msg) => {
130+
self.hints = msg.metadata.hints;
131+
Ok(())
132+
}
124133
Summary::Ignored => Err(Error::RequestIgnoredError),
125134
Summary::Failure(msg) => Err(Error::AuthenticationError(msg.message)),
126135
}

0 commit comments

Comments
 (0)