Skip to content

Commit 46e36b2

Browse files
Split handle in handle_with_options. For now keep handle_with_options private.
1 parent 16de55d commit 46e36b2

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

src/endpoint/builder.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,27 +186,13 @@ impl HandlerOptions {
186186
}
187187

188188
/// Builder for [`Endpoint`]
189+
#[derive(Default)]
189190
pub struct Builder {
190191
svcs: HashMap<String, BoxedService>,
191-
discovery: crate::discovery::Endpoint,
192+
discovery_services: Vec<crate::discovery::Service>,
192193
identity_verifier: IdentityVerifier,
193194
}
194195

195-
impl Default for Builder {
196-
fn default() -> Self {
197-
Self {
198-
svcs: Default::default(),
199-
discovery: crate::discovery::Endpoint {
200-
max_protocol_version: 5,
201-
min_protocol_version: 5,
202-
protocol_mode: Some(crate::discovery::ProtocolMode::BidiStream),
203-
services: vec![],
204-
},
205-
identity_verifier: Default::default(),
206-
}
207-
}
208-
}
209-
210196
impl Builder {
211197
/// Create a new builder for [`Endpoint`].
212198
pub fn new() -> Self {
@@ -249,7 +235,7 @@ impl Builder {
249235
let boxed_service = BoxedService::new(s);
250236
self.svcs
251237
.insert(service_metadata.name.to_string(), boxed_service);
252-
self.discovery.services.push(service_metadata);
238+
self.discovery_services.push(service_metadata);
253239
self
254240
}
255241

@@ -263,7 +249,7 @@ impl Builder {
263249
pub fn build(self) -> Endpoint {
264250
Endpoint(Arc::new(EndpointInner {
265251
svcs: self.svcs,
266-
discovery: self.discovery,
252+
discovery_services: self.discovery_services,
267253
identity_verifier: self.identity_verifier,
268254
}))
269255
}

src/endpoint/mod.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,40 @@ impl Endpoint {
167167

168168
struct EndpointInner {
169169
svcs: HashMap<String, BoxedService>,
170-
discovery: crate::discovery::Endpoint,
170+
discovery_services: Vec<crate::discovery::Service>,
171171
identity_verifier: IdentityVerifier,
172172
}
173173

174+
#[derive(Default)]
175+
pub(crate) enum ProtocolMode {
176+
#[allow(dead_code)]
177+
RequestResponse,
178+
#[default]
179+
BidiStream,
180+
}
181+
182+
/// Options for [`Endpoint::handle`].
183+
#[derive(Default)]
184+
pub(crate) struct HandleOptions {
185+
pub(crate) protocol_mode: ProtocolMode,
186+
}
187+
174188
impl Endpoint {
189+
/// Handle an [`http::Request`], producing an [`http::Response`].
175190
pub fn handle<B: Body<Data = Bytes, Error: Into<BoxError> + Send> + Send + 'static>(
176191
&self,
177192
req: http::Request<B>,
193+
) -> Result<http::Response<ResponseBody>, Error> {
194+
self.handle_with_options(req, HandleOptions::default())
195+
}
196+
197+
/// Handle an [`http::Request`], producing an [`http::Response`].
198+
pub(crate) fn handle_with_options<
199+
B: Body<Data = Bytes, Error: Into<BoxError> + Send> + Send + 'static,
200+
>(
201+
&self,
202+
req: http::Request<B>,
203+
options: HandleOptions,
178204
) -> Result<http::Response<ResponseBody>, Error> {
179205
let (parts, body) = req.into_parts();
180206
let path = parts.uri.path();
@@ -190,7 +216,7 @@ impl Endpoint {
190216
return self.handle_health();
191217
}
192218
if parts.last() == Some(&"discover") {
193-
return self.handle_discovery(headers);
219+
return self.handle_discovery(headers, options.protocol_mode);
194220
}
195221

196222
// Parse service name/handler name
@@ -262,6 +288,7 @@ impl Endpoint {
262288
fn handle_discovery(
263289
&self,
264290
headers: http::HeaderMap,
291+
protocol_mode: ProtocolMode,
265292
) -> Result<http::Response<ResponseBody>, Error> {
266293
// Extract Accept header from request
267294
let accept_header = match headers
@@ -300,7 +327,18 @@ impl Endpoint {
300327
value: content_type.into(),
301328
}],
302329
Bytes::from(
303-
serde_json::to_string(&self.0.discovery).expect("Discovery should be serializable"),
330+
serde_json::to_string(&crate::discovery::Endpoint {
331+
max_protocol_version: 5,
332+
min_protocol_version: 5,
333+
protocol_mode: Some(match protocol_mode {
334+
ProtocolMode::RequestResponse => {
335+
crate::discovery::ProtocolMode::RequestResponse
336+
}
337+
ProtocolMode::BidiStream => crate::discovery::ProtocolMode::BidiStream,
338+
}),
339+
services: self.0.discovery_services.clone(),
340+
})
341+
.expect("Discovery should be serializable"),
304342
),
305343
))
306344
}
@@ -309,7 +347,7 @@ impl Endpoint {
309347
// Validate that new discovery fields aren't used with older protocol versions
310348
if version <= 2 {
311349
// Check for new discovery fields in version 3 that shouldn't be used in version 2 or lower
312-
for service in &self.0.discovery.services {
350+
for service in &self.0.discovery_services {
313351
if service.inactivity_timeout.is_some() {
314352
Err(ErrorInner::FieldRequiresMinimumVersion(
315353
"inactivity_timeout",

src/hyper.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Hyper integration.
22
33
use crate::endpoint;
4-
use crate::endpoint::Endpoint;
4+
use crate::endpoint::{Endpoint, HandleOptions, ProtocolMode};
55

66
use http::{Request, Response};
77
use hyper::body::Incoming;
@@ -24,6 +24,11 @@ impl Service<Request<Incoming>> for HyperEndpoint {
2424
type Future = Ready<Result<Self::Response, Self::Error>>;
2525

2626
fn call(&self, req: Request<Incoming>) -> Self::Future {
27-
ready(self.0.handle(req))
27+
ready(self.0.handle_with_options(
28+
req,
29+
HandleOptions {
30+
protocol_mode: ProtocolMode::BidiStream,
31+
},
32+
))
2833
}
2934
}

0 commit comments

Comments
 (0)