Преглед на файлове

dev: add logical rate-limit to new call method

KernelDeimos преди 10 месеца
родител
ревизия
8be7a7d219
променени са 2 файла, в които са добавени 28 реда и са изтрити 3 реда
  1. 8 3
      src/backend/src/services/SUService.js
  2. 20 0
      src/backend/src/services/drivers/DriverService.js

+ 8 - 3
src/backend/src/services/SUService.js

@@ -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);
     }
 }

+ 20 - 0
src/backend/src/services/drivers/DriverService.js

@@ -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 => {