Git Product home page Git Product logo

Comments (7)

Vorlias avatar Vorlias commented on May 28, 2024 1

I can add a rate limit system as well 👍

from rbx-net.

Vorlias avatar Vorlias commented on May 28, 2024 1

Had to create a separate class, because I need a wrapper callback to handle rate limitations, and don't want to do that for ALL remotes.

rbx-net/src/index.ts

Lines 313 to 362 in 967befb

export class ThrottledServerFunction<CR extends any = any> extends ServerFunction<CR> {
/** @internal */
public static rates = new Map<string, Array<number>>();
private maxRequestsPerMinute: number = 0;
private clientRequests: RequestCounter;
constructor(name: string, rateLimit: number) {
super(name);
this.maxRequestsPerMinute = rateLimit;
this.clientRequests = throttler.Get(`Function~${name}`);
const clientValue = new Instance("IntValue", this.instance);
clientValue.Name = "RateLimit";
clientValue.Value = rateLimit;
}
public set Callback(callback: Callback) {
this.instance.OnServerInvoke = (player: Player, ...args: Array<any>) => {
const clientRequestCount = this.clientRequests.Get(player);
if (clientRequestCount >= this.maxRequestsPerMinute) {
error(`Rate limit reached (${clientRequestCount})!`);
} else {
this.clientRequests.Increment(player);
return callback(player, ...args);
}
};
}
/**
* The number of requests allowed per minute per user
*/
public set RateLimit(requestsPerMinute: number) {
this.maxRequestsPerMinute = requestsPerMinute;
let clientValue = this.instance.FindFirstChild<IntValue>("RateLimit");
if (clientValue) {
clientValue.Value = requestsPerMinute;
} else {
clientValue = new Instance("IntValue", this.instance);
clientValue.Name = "RateLimit";
clientValue.Value = requestsPerMinute;
}
}
public get RateLimit() {
return this.maxRequestsPerMinute;
}
}

How does this look for you? Works good. I'll work on adding an event equivalent as well.

from rbx-net.

Vorlias avatar Vorlias commented on May 28, 2024

ServerFunction.ClientCache can be used to cache results for RemoteFunctions.

The value is the amount of seconds the value is cached for.

e.g.

const myFunction = new Net.ServerFunction("MyFunction");
myFunction.ClientCache = 10; // cached for 10 seconds

During those 10 seconds, it will use the value from the cache rather than send another request.

from rbx-net.

catgirlinspace avatar catgirlinspace commented on May 28, 2024

Does the cache feature call the server if different parameters are used?

from rbx-net.

Vorlias avatar Vorlias commented on May 28, 2024

It will cache regardless of the parameters provided... unlike ModRemote where you can cache based on the first parameter.

If you want, I can also add that functionality into rbx-net.

from rbx-net.

catgirlinspace avatar catgirlinspace commented on May 28, 2024

A way to cache based on parameters would be similar to this, so that would work. Just my concern is someone uses an exploit to fire GetProfileDetails a lot with a different user id each time causing a data store request.

from rbx-net.

catgirlinspace avatar catgirlinspace commented on May 28, 2024

Skimming through it, it looks good! 👍

from rbx-net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.