Skip to content

Commit

Permalink
feat(http): Add connection timeout configuration for HTTP requests
Browse files Browse the repository at this point in the history
- Introduce new 'ConnectionTimeout' parameter in HTTP module
- Default connection timeout set to 10 seconds
- Update RequestBase to support configuring connection timeout
- Use reqwest's connect_timeout method to set connection establishment timeout
  • Loading branch information
sinkingsugar committed Jan 30, 2025
1 parent a8f7550 commit 334311a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion shards/core/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3254,7 +3254,7 @@ SHVar shards_deserialize_var(const SHVar *bytes_buffer_var) {
}

SHBool shards_cancel_abort(SHContext *context) {
if (context->shouldStop()) {
if (context->shouldStop() || context->onLastResume) {
// ok this flow should stop already... so we can just return false
return false;
}
Expand Down
14 changes: 13 additions & 1 deletion shards/modules/http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ lazy_static! {
INT_TYPES_SLICE
)
.into(),
(
cstr!("ConnectionTimeout"),
shccstr!("How many seconds to wait for a connection to be established. Defaults to 10 seconds."),
INT_TYPES_SLICE
)
.into(),

];
}

Expand Down Expand Up @@ -191,6 +198,7 @@ struct RequestBase {
keep_alive: bool,
required: ExposedTypes,
streaming: bool,
connection_timeout: u64,
}

impl Default for RequestBase {
Expand All @@ -209,6 +217,7 @@ impl Default for RequestBase {
keep_alive: false,
required: Vec::new(),
streaming: false,
connection_timeout: 10,
}
}
}
Expand Down Expand Up @@ -242,6 +251,7 @@ impl RequestBase {
7 => Ok(self.keep_alive = value.try_into().map_err(|_x| "Failed to set keep_alive")?),
8 => Ok(self.streaming = value.try_into().map_err(|_x| "Failed to set streaming")?),
9 => Ok(self.backoff = value.try_into().map_err(|_x| "Failed to set backoff")?),
10 => Ok(self.connection_timeout = value.try_into().map_err(|_x| "Failed to set connection_timeout")?),
_ => unreachable!(),
}
}
Expand All @@ -258,6 +268,7 @@ impl RequestBase {
7 => self.keep_alive.into(),
8 => self.streaming.into(),
9 => self.backoff.try_into().expect("A valid integer in range"),
10 => self.connection_timeout.try_into().expect("A valid integer in range"),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -292,7 +303,8 @@ impl RequestBase {
self.client = Some(
reqwest::Client::builder()
.danger_accept_invalid_certs(self.invalid_certs)
.timeout(Duration::from_secs(self.timeout))
.read_timeout(Duration::from_secs(self.timeout))
.connect_timeout(Duration::from_secs(self.connection_timeout))
.build()
.map_err(|e| {
print_error(&e);
Expand Down

0 comments on commit 334311a

Please sign in to comment.