Skip to content

Commit

Permalink
Merge pull request #1105 from fragcolor-xyz/gio/daily
Browse files Browse the repository at this point in the history
refactor(core): enhance cancel_abort logic with return value and retr…
  • Loading branch information
sinkingsugar authored Jan 15, 2025
2 parents e16ac95 + ed08e67 commit 3db1e3f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
7 changes: 6 additions & 1 deletion shards/core/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3253,9 +3253,14 @@ SHVar shards_deserialize_var(const SHVar *bytes_buffer_var) {
return leaking_tmp;
}

void shards_cancel_abort(SHContext *context) {
SHBool shards_cancel_abort(SHContext *context) {
if (context->shouldStop()) {
// ok this flow should stop already... so we can just return false
return false;
}
context->resetErrorStack();
context->continueFlow();
return true;
}
}

Expand Down
8 changes: 6 additions & 2 deletions shards/modules/core/wires.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ struct BaseRunner : public WireBase {

SHTypeInfo compose(const SHInstanceData &data) {
resolveWire();
assert(wire && "wire should be set at this point");
if (!wire) {
throw std::runtime_error("wire should be set at this point");
}
// Start/Resume need to capture all it needs, so we need deeper informations
// this is triggered by populating requiredVariables variable
auto dataCopy = data;
Expand Down Expand Up @@ -218,7 +220,9 @@ struct BaseRunner : public WireBase {

void warmup(SHContext *ctx) {
if (capturing) {
assert(wire && "wire should be set at this point");
if (!wire) {
throw std::runtime_error("wire should be set at this point");
}

for (auto &v : _vars) {
SHLOG_TRACE("BaseRunner: warming up variable: {}, wire: {}", v.variableName(), wire->name);
Expand Down
22 changes: 14 additions & 8 deletions shards/modules/http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,13 @@ macro_rules! get_like {
if retries == 0 {
return Err("Request failed");
} else {
shards::core::cancel_abort(context);
shlog_debug!("Retrying request, {} tries left", retries);
shards::core::suspend(context, self.rb.backoff as f64); // use backoff instead of hardcoded 1.0
retries -= 1;
if shards::core::cancel_abort(context) {
shlog_debug!("Retrying request, {} tries left", retries);
shards::core::suspend(context, self.rb.backoff as f64); // use backoff instead of hardcoded 1.0
retries -= 1;
} else {
return Err("Cannot retry request, wire might have been aborted");
}
}
}
}
Expand Down Expand Up @@ -793,10 +796,13 @@ macro_rules! post_like {
if retries == 0 {
return Err("Request failed");
} else {
shards::core::cancel_abort(context);
shlog_debug!("Retrying request, {} tries left", retries);
shards::core::suspend(context, self.rb.backoff as f64); // use backoff instead of hardcoded 1.0
retries -= 1;
if shards::core::cancel_abort(context) {
shlog_debug!("Retrying request, {} tries left", retries);
shards::core::suspend(context, self.rb.backoff as f64); // use backoff instead of hardcoded 1.0
retries -= 1;
} else {
return Err("Cannot retry request, wire might have been aborted");
}
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions shards/rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,15 @@ pub fn abortWire(context: &SHContext, message: &str) {
}
}

// void shards_cancel_abort(SHContext *context)
// SHBool shards_cancel_abort(SHContext *context)
// Not exposed in the C API, but used internally
extern "C" {
fn shards_cancel_abort(context: *mut SHContext);
fn shards_cancel_abort(context: *mut SHContext) -> SHBool;
}

#[inline(always)]
pub fn cancel_abort(context: &SHContext) {
unsafe {
shards_cancel_abort(context as *const SHContext as *mut SHContext);
}
pub fn cancel_abort(context: &SHContext) -> bool {
unsafe { shards_cancel_abort(context as *const SHContext as *mut SHContext) }
}

#[inline(always)]
Expand Down

0 comments on commit 3db1e3f

Please sign in to comment.