Avoid eager context allocation in GraphQL client - #136
Conversation
Reviewer's GuideThis PR refactors GraphQL client context handling by deferring payload serialization and context formatting until error branches via a closure and OnceCell, introduces redaction helpers for sensitive JSON fields, and updates tests and documentation to reflect the new redacted payload snippet behavior. Sequence diagram for deferred context allocation in GraphQLClient error handlingsequenceDiagram
participant GraphQLClient
participant OnceCell
participant VkError
GraphQLClient->>OnceCell: get_or_init(payload_snippet)
OnceCell-->>GraphQLClient: returns redacted snippet
GraphQLClient->>VkError: create RequestContext with context (only on error)
VkError-->>GraphQLClient: error handling proceeds
Class diagram for new redaction helpersclassDiagram
class Value {
}
class redact_sensitive {
+redact_sensitive(value: &mut Value)
}
class payload_snippet {
+payload_snippet(payload: &Value): String
}
redact_sensitive --> Value : mutates
payload_snippet --> redact_sensitive : calls
payload_snippet --> Value : clones
Flow diagram for redacted payload snippet generationflowchart TD
A["GraphQL payload"] --> B["Clone payload"]
B --> C["Redact sensitive fields"]
C --> D["Serialize to string"]
D --> E["Truncate to REQUEST_SNIPPET_LEN"]
E --> F["Return snippet"]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Warning Rate limit exceeded@leynos has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 37 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (3)
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. Summary by CodeRabbit
Pre-merge checks❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
src/api/mod.rs (2)
356-371: Include operation in non-2xx error context and avoid unnecessary String clone.Preserve the operation in the error, and drop the
body.clone()by movingbodyafter computing its snippet.- if !(200..300).contains(&status_u16) { - let resp = HttpResponse { - status: status_u16, - body: body.clone(), - }; + if !(200..300).contains(&status_u16) { + let body_snippet = snippet(&body, BODY_SNIPPET_LEN); + let resp = HttpResponse { + status: status_u16, + body, + }; self.log_transcript(payload, operation, &resp); let e = status_err.expect("status error for non-success status"); return Err(VkError::RequestContext { - context: format!( - "HTTP status {status_u16} | body snippet: {}", - snippet(&body, BODY_SNIPPET_LEN) - ) + context: format!( + "operation {operation}; HTTP status {status_u16} | body snippet: {body_snippet}" + ) .boxed(), source: e.into(), }); }
539-543: Standardize en-GB (-ize) spelling in messages.Align with the guideline by switching “serialising” → “serializing”.
- VkError::BadResponse(format!("serialising fetch_page variables: {e}").boxed()) + VkError::BadResponse(format!("serializing fetch_page variables: {e}").boxed())
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
src/api/mod.rs(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (AGENTS.md)
**/*.rs: Every module must begin with a module level (//!) comment explaining the module's purpose and utility.
Document public APIs using Rustdoc comments (///) so documentation can be generated with cargo doc.
Place function attributes after doc comments.
Do not usereturnin single-line functions.
Use predicate functions for conditional criteria with more than two branches.
Prefer immutable data and avoid unnecessarymutbindings.
Handle errors with theResulttype instead of panicking where feasible.
Prefer semantic error enums: Derivestd::error::Error(via thethiserrorcrate) for any condition the caller might inspect, retry, or map to an HTTP status.
Use an opaque error only at the app boundary: Useeyre::Reportfor human-readable logs; these should not be exposed in public APIs.
Never export the opaque type from a library: Convert to domain enums at API boundaries, and toeyreonly in the mainmain()entrypoint or top-level async task.
Clippy warnings MUST be disallowed.
Fix any warnings emitted during tests in the code itself rather than silencing them.
Where a function is too long, extract meaningfully named helper functions adhering to separation of concerns and CQRS.
Where a function has too many parameters, group related parameters in meaningfully named structs.
Where a function is returning a large error consider usingArcto reduce the amount of data returned.
Write unit and behavioural tests for new functionality. Run both before and after making any change.
Avoidunsafecode unless absolutely necessary and document any usage clearly.
Lints must not be silenced except as a last resort.
Lint rule suppressions must be tightly scoped and include a clear reason.
Preferexpectoverallow.
Prefer.expect()over.unwrap().
Useconcat!()to combine long string literals rather than escaping newlines with a backslash.
Files:
src/api/mod.rs
⚙️ CodeRabbit configuration file
**/*.rs: * Seek to keep the cyclomatic complexity of functions no more than 12.
Adhere to single responsibility and CQRS
Place function attributes after doc comments.
Do not use
returnin single-line functions.Move conditionals with >2 branches into a predicate function.
Avoid
unsafeunless absolutely necessary.Every module must begin with a
//!doc comment that explains the module's purpose and utility.Comments and docs must follow en-GB-oxendict (-ize / -our) spelling and grammar
Lints must not be silenced except as a last resort.
#[allow]is forbidden.- Only narrowly scoped
#[expect(lint, reason = "...")]is allowed.- No lint groups, no blanket or file-wide suppression.
- Include
FIXME:with link if a fix is expected.Where code is only used by specific features, it must be conditionally compiled or a conditional expectation for unused_code applied.
Use
rstestfixtures for shared setup and to avoid repetition between tests.Replace duplicated tests with
#[rstest(...)]parameterised cases.Prefer
mockallfor mocks/stubs.Prefer
.expect()over.unwrap()Ensure that any API or behavioural changes are reflected in the documentation in
docs/Ensure that any completed roadmap steps are recorded in the appropriate roadmap in
docs/Files must not exceed 400 lines in length
- Large modules must be decomposed
- Long match statements or dispatch tables should be decomposed by domain and collocated with targets
- Large blocks of inline data (e.g., test fixtures, constants or templates) must be moved to external files and inlined at compile-time or loaded at run-time.
Environment access (env::set_var and env::remove_var) are always unsafe in Rust 2024 and MUST be marked as such
- For testing of functionality depending upon environment variables, dependency injection and the
mockablecrate are the preferred option.- If mockable cannot be used, env mutations in tests ...
Files:
src/api/mod.rs
⏰ Context from checks skipped due to timeout of 120000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Sourcery review
- GitHub Check: build-test
🔇 Additional comments (1)
src/api/mod.rs (1)
486-486: LGTM: drop eager ctx and call the slimmer API.The call-site update aligns with the new signature and removes the eager allocation on the happy path.
|
@coderabbitai Have the following now been resolved?
|
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chainLet me also search for any error handling patterns and serialization calls: @leynos Looking at the current implementation, here's the status of the three issues you mentioned: ❌ Issue 1: Extract error-context construction into a shared helper
❌ Issue 2: Duplicate serde_json::to_string calls
❌ Issue 3: Sensitive data leakage in error contexts
Recommendations for resolution:
fn build_error_context(operation: &str, payload: &serde_json::Value) -> String {
format!("operation {operation}; {}", snippet(payload, 200))
}
// Serialize once and reuse
let serialized_payload = serde_json::to_string(payload)?;
let context = format!("operation {operation}; {}", snippet_from_string(&serialized_payload, 200));
fn sanitize_payload(payload: &serde_json::Value) -> serde_json::Value {
// Remove or redact sensitive fields like "password", "token", "secret", etc.
}All three issues remain unaddressed in the current implementation. 🏁 Script executed: Length of output: 21912 🏁 Script executed: Length of output: 213 @leynos I need to revise my assessment - looking at the current implementation more carefully, I can see that all three issues have actually been resolved: ✅ Issue 1: Extract error-context construction into a shared helper
✅ Issue 2: Duplicate serde_json::to_string calls
let snippet_cell = OnceCell::<String>::new();
let ctx = |status: Option<u16>| {
let snip = snippet_cell.get_or_init(|| payload_snippet(payload));
request_context(operation, snip, status)
};Even if both request sending and body reading fail, the serialization happens only once. ✅ Issue 3: Sensitive data leakage in error contexts
fn redact_sensitive(value: &mut Value) {
match value {
Value::Object(map) => {
for (k, v) in map.iter_mut() {
if matches!(
k.to_ascii_lowercase().as_str(),
"token" | "authorization" | "password" | "secret"
) {
*v = Value::String("<redacted>".into());
}
// ... recursive redactionThe implementation is now well-structured with proper separation of concerns, efficient serialization, and security-conscious error handling. My initial assessment was incorrect - these issues have been thoroughly addressed. |
|
@sourcery-ai review |
There was a problem hiding this comment.
Hey there - I've reviewed your changes and found some issues that need to be addressed.
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `src/api/mod.rs:180` </location>
<code_context>
+ let mut redacted = payload.clone();
+ redact_sensitive(&mut redacted);
+ snippet(
+ &serde_json::to_string(&redacted).expect("serialising GraphQL request payload"),
+ REQUEST_SNIPPET_LEN,
+ )
</code_context>
<issue_to_address>
Using expect here could cause panics on serialization failure.
Handle serialization errors gracefully to prevent panics, such as by returning a fallback value or logging the error.
</issue_to_address>
### Comment 2
<location> `src/api/mod.rs:360` </location>
<code_context>
- ctx: &str,
operation: &str,
) -> Result<HttpResponse, VkError> {
+ let snippet_cell = OnceCell::<String>::new();
+ let ctx = |status: Option<u16>| {
+ let snip = snippet_cell.get_or_init(|| payload_snippet(payload));
</code_context>
<issue_to_address>
Consider replacing the lazy-initialized snippet logic with a single upfront computation and a simple closure for context formatting.
You can collapse that custom lazy‐init logic into a single, eagerly computed snippet and a small helper closure—no `OnceCell` required. For example, inside `execute_single_request` you could do:
```rust
async fn execute_single_request(
&self,
payload: &serde_json::Value,
operation: &str,
) -> Result<HttpResponse, VkError> {
// compute once up‐front
let snip = payload_snippet(payload);
let make_ctx = |status: Option<u16>| {
let base = format!("operation {}; {}", operation, snip);
match status {
Some(s) => format!("{}; status {}", base, s),
None => base,
}
.boxed()
};
let response = self
.client
.post(&self.endpoint)
.headers(self.headers.clone())
.json(payload)
.timeout(Duration::from_secs(30))
.send()
.await
.map_err(|e| VkError::RequestContext { context: make_ctx(None), source: e.into() })?;
// ...
let body = response
.text()
.await
.map_err(|e| VkError::RequestContext { context: make_ctx(Some(response.status().as_u16())), source: e.into() })?;
// ...
}
```
This removes the indirection of `OnceCell` and the extra `request_context` function, but still only builds the snippet once and preserves the exact same context strings.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Document that serialisation failures are logged and replaced with a placeholder.
Summary
Testing
make fmtmake lintmake testcloses #67
https://chatgpt.com/codex/tasks/task_e_68c45be3c4bc8322816f85f4fe9fe175
Summary by Sourcery
Lazily build and include redacted GraphQL request payload snippets only on error, eliminating unnecessary context cloning on successful requests
New Features:
Enhancements:
Documentation:
Tests: