Skip to content

Commit

Permalink
try finally
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Sep 5, 2024
1 parent ca48467 commit b813f75
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion service/pool/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -622,13 +622,17 @@ shared (creator) actor class Self(opt_params : ?Types.InitParams) = this {
throw Error.reject "Only a canister managed by the Motoko Playground can call http_request";
};
let cycles = 250_000_000_000;
try {
if (pool.spendCycles(caller, cycles)) {
Cycles.add<system> cycles;
let new_request = switch (request.transform) {
case null {
{ request with transform = null };
};
case (?transform) {
if (Option.isSome(pool.getTransform(caller))) {
throw Error.reject "No concurrent http_request allowed";
};
pool.rememberTransform(caller, transform);
let fake_actor: actor { __transform: ICType.transform_function } = actor(Principal.toText(Principal.fromActor this));
let new_transform = ?{ function = fake_actor.__transform; context = Principal.toBlob caller };
Expand All @@ -638,11 +642,13 @@ shared (creator) actor class Self(opt_params : ?Types.InitParams) = this {
let res = await IC.http_request(new_request);
let refunded = -Cycles.refunded();
assert(pool.spendCycles(caller, refunded) == true);
pool.removeTransform(caller);
res;
} else {
throw Error.reject "http_request exceeds cycle spend limit";
};
} finally {
pool.removeTransform(caller);
};
};
public shared composite query({ caller }) func __transform({context: Blob; response: ICType.http_request_result}) : async ICType.http_request_result {
if (caller != Principal.fromText("aaaaa-aa") and caller != Principal.fromText("2vxsx-fae")) {
Expand Down

0 comments on commit b813f75

Please sign in to comment.