dev: add logical rate-limit to new call method

This commit is contained in:
KernelDeimos 2024-07-25 20:23:05 -04:00 committed by Eric Dubé
parent 780a993465
commit 8be7a7d219
2 changed files with 28 additions and 3 deletions

View File

@ -22,10 +22,15 @@ class SUService extends BaseService {
async get_system_actor () {
return this.sys_actor_;
}
async sudo (callback) {
async sudo (actor, callback) {
if ( ! callback ) {
callback = actor;
actor = await this.sys_actor_;
}
actor = Actor.adapt(actor);
return await Context.get().sub({
user: await this.sys_user_,
actor: await this.sys_actor_,
actor,
user: actor.type.user,
}).arun(callback);
}
}

View File

@ -24,6 +24,7 @@ const BaseService = require("../BaseService");
const { Driver } = require("../../definitions/Driver");
const { PermissionUtil } = require("../auth/PermissionService");
const { Invoker } = require("@heyputer/puter-js-common/src/libs/invoker");
const { get_user } = require("../../helpers");
/**
* DriverService provides the functionality of Puter drivers.
@ -261,6 +262,9 @@ class DriverService extends BaseService {
);
}
const policy_holder = await get_user(
{ username: effective_policy.holder });
// NOT FINAL: this will be handled by 'get_policies_for_option_'
// when cascading monthly usage is implemented.
const svc_systemData = this.services.get('system-data');
@ -276,6 +280,22 @@ class DriverService extends BaseService {
const invoker = Invoker.create({
decorators: [
{
name: 'enforce logical rate-limit',
on_call: async args => {
if ( ! effective_policy['rate-limit'] ) return args;
const svc_su = this.services.get('su');
const svc_rateLimit = this.services.get('rate-limit');
await svc_su.sudo(policy_holder, async () => {
await svc_rateLimit.check_and_increment(
`V1:${service_name}:${iface}:${method}`,
effective_policy['rate-limit'].max,
effective_policy['rate-limit'].period,
);
});
return args;
},
},
{
name: 'add metadata',
on_return: async result => {