mirror of
https://github.com/HeyPuter/puter
synced 2024-11-14 22:06:00 +00:00
dev: add logical rate-limit to new call method
This commit is contained in:
parent
780a993465
commit
8be7a7d219
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 => {
|
||||
|
Loading…
Reference in New Issue
Block a user