Tracking Cycle Usage

Hi everyone :wave:

I’m looking for some additional insights on tracking cycle usage for specific endpoints, particularly for outgoing HTTP calls (or other calls where cycles need to be added).

Here’s the process I’m currently following:

  1. Add cycles.
  2. Make an http_request (or any other relevant call).
  3. The call returns.
  4. Check cycles again (some cycles might have been refunded).

However, I’ve encountered a couple of issues with this approach:

  1. Race Conditions: Since the HTTP call is made outside the canister, there’s a potential for race conditions. Another request to the same endpoint could be made before the first one returns, leading to an inaccurate cycle balance. While endpoint mutex locks could address this, I’d prefer to avoid this solution if possible.
  2. Canister Top-ups: If the canister is topped up with cycles during the HTTP call, the cycle usage calculation could become negative, potentially causing further issues.

Given these challenges, I’m considering tracking cycle usage on a per-user basis to ensure accurate accounting for each user. Does anyone have experience with this or suggestions on how to effectively manage cycle tracking under these conditions? Any insights would be greatly appreciated!