GithubHelp home page GithubHelp logo

transformer-throttler's Introduction

grammY Throttler (Transformer)

Throttling transformer for grammY bot framework, written in Typescript and built with Bottleneck.

About

This throttler aims to limit and queue outgoing Telegram API calls to conform to the official Telegram API rate limits.

Configuration

The throttler accepts a single optional argument of the following form:

type ThrottlerOptions = {
  global?: Bottleneck.ConstructorOptions;     // For throttling all api calls
  group?: Bottleneck.ConstructorOptions;      // For throttling outgoing group messages
  out?: Bottleneck.ConstructorOptions;        // For throttling outgoing private messages
};

The full list of object properties available for Bottleneck.ConstructorOptions can be found at Bottleneck.

If no argument is passed, the throttler created will use the default configuration settings which should be appropriate for most use cases. The default configuration are as follows:

// Outgoing Global Throttler
const globalConfig = {
  reservoir: 30,                  // Number of new jobs that throttler will accept at start
  reservoirRefreshAmount: 30,     // Number of jobs that throttler will accept after refresh
  reservoirRefreshInterval: 1000, // Interval in milliseconds where reservoir will refresh
};

// Outgoing Group Throttler
const groupConfig = {
  maxConcurrent: 1,                // Only 1 job at a time
  minTime: 1000,                   // Wait this many milliseconds to be ready, after a job
  reservoir: 20,                   // Number of new jobs that throttler will accept at start
  reservoirRefreshAmount: 20,      // Number of jobs that throttler will accept after refresh
  reservoirRefreshInterval: 60000, // Interval in milliseconds where reservoir will refresh
};

// Outgoing Private Throttler
const outConfig = {
  maxConcurrent: 1,                // Only 1 job at a time
  minTime: 1000,                   // Wait this many milliseconds to be ready, after a job
};

Usage

import { Bot } from 'https://lib.deno.dev/x/grammy@1/mod.ts';
import { run } from 'https://lib.deno.dev/x/grammy_runner@1/mod.ts';
import { apiThrottler, bypassThrottler } from 'https://lib.deno.dev/x/grammy_transformer_throttler@1/mod.ts';

const botToken = Deno.env.get('BOT_TOKEN');
if (!botToken) {
  throw Error('BOT_TOKEN is required');
}
const bot = new Bot(botToken);

const throttler = apiThrottler();
bot.api.config.use(throttler);

// Experimental: Do not throttle update-initiated first response
bot.use(bypassThrottler);
bot.command('/example', ctx => ctx.reply('I am throttled'));

// If you are using throttler, you most likely want to use a runner to handle updates concurrently
run(bot);

transformer-throttler's People

Contributors

dependabot[bot] avatar knightniwrem avatar knorpelsenf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

roamiiing

transformer-throttler's Issues

Looks like v1.2.1 is incompatible with [email protected]

import { Bot } from "https://deno.land/x/[email protected]/mod.ts"
import { apiThrottler } from "https://deno.land/x/[email protected]/mod.ts"

const bot = new Bot("");
const throttler = apiThrottler();
bot.api.config.use(throttler); // error
Argument of type 'Transformer' is not assignable to parameter of type 'Transformer<RawApi>'.
  Types of parameters 'prev' and 'prev' are incompatible.
    Types of parameters 'payload' and 'payload' are incompatible.
      Type 'import("https://deno.land/x/[email protected]/core/client").Payload<M, import("https://deno.land/x/[email protected]/core/client").RawApi>' is not assignable to type 'import("https://deno.land/x/[email protected]/core/client").Payload<M, import("https://deno.land/x/[email protected]/core/client").RawApi>'.
        Type 'RawApi[M] extends (signal?: AbortSignal | undefined) => unknown ? {} : RawApi[M] extends (args: any, signal?: AbortSignal | undefined) => unknown ? Parameters<...>[0] : never' is not assignable to type 'Payload<M, RawApi>'.
          Type '{} | (RawApi[M] extends (args: any, signal?: AbortSignal | undefined) => unknown ? Parameters<RawApi[M]>[0] : never)' is not assignable to type 'Payload<M, RawApi>'.
            Type '{}' is not assignable to type 'Payload<M, RawApi>'.
              Type 'import("https://deno.land/x/[email protected]/core/client").RawApi[M] extends (signal?: AbortSignal | undefined) => unknown ? {} : import("https://deno.land/x/[email protected]/core/client").RawApi[M] extends (args: any, signal?: AbortSignal | undefined) => unknown ? Parameters<...>[0] : never' is not assignable to type 'import("https://deno.land/x/[email protected]/core/client").RawApi[M] extends (signal?: AbortSignal | undefined) => unknown ? {} : import("https://deno.land/x/[email protected]/core/client").RawApi[M] extends (args: any, signal?: AbortSignal | undefined) => unknown ? Parameters<...>[0] : never'.
                Type '{} | (RawApi[M] extends (args: any, signal?: AbortSignal | undefined) => unknown ? Parameters<RawApi[M]>[0] : never)' is not assignable to type 'RawApi[M] extends (signal?: AbortSignal | undefined) => unknown ? {} : RawApi[M] extends (args: any, signal?: AbortSignal | undefined) => unknown ? Parameters<...>[0] : never'.
                  Type '{}' is not assignable to type 'RawApi[M] extends (signal?: AbortSignal | undefined) => unknown ? {} : RawApi[M] extends (args: any, signal?: AbortSignal | undefined) => unknown ? Parameters<...>[0] : never'

Stable 1.0 release

This plugin seems to work well already.

What is necessary in order for you to release a stable version 1.0?

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.