パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.1.1

rate_limiter

通常の関数に有用なレート制限戦略を適用するための純粋なDartパッケージです。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
15.1万
いいね数
102
Pub ポイント
160

パッケージをインストール

flutter pub add rate_limiter

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

通常の関数に効果的なレート制限戦略を実装するための純粋なDartパッケージです。関数の実行頻度を制御することで、システムの過負荷を防ぎ、API呼び出し、検索クエリ、その他の高頻度操作の管理に最適です。シンプルさとパフォーマンスを重視して設計されており、さまざまなレート制限アルゴリズムをサポートし、複数のプラットフォームでシームレスに統合できます。外部依存なしにトータルまたはバースト制御が必要な状況において特に有用です。

用途

  • APIリクエストの制限
  • 検索入力の頻度を制限する
  • ユーザー入力におけるスパムの防止
  • バースト型ワークロードの管理
  • バックグラウンドタスクの実行を制御する

主な機能

  • 複数のレート制限戦略をサポートしています
  • 純粋なDart実装
  • クロスプラットフォーム互換性
  • シンプルな関数ラッピング
  • 設定可能な制限とインターバル
  • 外部の依存関係なし

適している対象

  • 利用制限付きのAPIを開発する開発者
  • 入力のデバウンスが必要なアプリ
  • 負荷保護を必要とするシステム
  • 高通話数のリアルタイムアプリケーション
  • 複数のプラットフォームでFlutterを使用するチーム

注意事項

  • 非同期関数との手動統合が必要です
  • 関数レベルのレート制限に限定
  • 状態の組み込み永続化なし
  • 軽量で同期的な使用ケースに最適
  • 複雑な並行処理パターンに対して追加の処理が必要になる可能性があります

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Rate Limiter

Open Source Love License Dart CI CodeCov Version

[ Built with ♥ at Stream ]

Introduction

Rate limiting is a strategy for limiting an action. It puts a cap on how often someone can repeat an action within a certain timeframe. Using rate_limiter we made it easier than ever to apply these strategies on regular dart functions.

( Inspired from lodash )

Index

Installation

Add the following to your pubspec.yaml and replace [version] with the latest version:

dependencies:
  rate_limiter: ^[version]

Strategies

Debounce

A debounced function will ignore all calls to it until the calls have stopped for a specified time period. Only then it will call the original function. For instance, if we specify the time as two seconds, and the debounced function is called 10 times with an interval of one second between each call, the function will not call the original function until two seconds after the last (tenth) call.

Usage

It's fairly simple to create debounced function with rate_limiter

  1. Creating from scratch
final debouncedFunction = debounce((String value) {  
  print('Got value : $value');  
  return value;  
}, const Duration(seconds: 2));
  1. Converting an existing function into debounced function
String regularFunction(String value) {  
  print('Got value : $value');  
  return value;  
}  
  
final debouncedFunction = regularFunction.debounced(  
  const Duration(seconds: 2),  
);
Example

Often times, search boxes offer dropdowns that provide autocomplete options for the user’s current input. Sometimes the items suggested are fetched from the backend via API (for instance, on Google Maps). The autocomplete API gets called whenever the search query changes. Without debounce, an API call would be made for every letter you type, even if you’re typing very fast. Debouncing by one second will ensure that the autocomplete function does nothing until one second after the user is done typing.

final debouncedAutocompleteSearch = debounce(
  (String searchQuery) async {
    // fetches results from the api
    final results = await searchApi.get(searchQuery);
    // updates suggestion list
    updateSearchSuggestions(results);
  },
  const Duration(seconds: 1),
);

TextField(
  onChanged: (query) {
    debouncedAutocompleteSearch([query]);
  },
);
Throttle

To throttle a function means to ensure that the function is called at most once in a specified time period (for instance, once every 10 seconds). This means throttling will prevent a function from running if it has run “recently”. Throttling also ensures a function is run regularly at a fixed rate.

Usage

Creating throttled function is similar to debounce function

  1. Creating from scratch
final throttledFunction = throttle((String value) {  
  print('Got value : $value');  
  return value;  
}, const Duration(seconds: 2));
  1. Converting an existing function into throttled function
String regularFunction(String value) {  
  print('Got value : $value');  
  return value;  
}  
  
final throttledFunction = regularFunction.throttled(  
  const Duration(seconds: 2),  
);
Example

In action games, the user often performs a key action by pushing a button (example: shooting, punching). But, as any gamer knows, users often press the buttons much more than is necessary, probably due to the excitement and intensity of the action. So the user might hit “Punch” 10 times in 5 seconds, but the game character can only throw one punch in one second. In such a situation, it makes sense to throttle the action. In this case, throttling the “Punch” action to one second would ignore the second button press each second.

final throttledPerformPunch = throttle(
  () {
    print('Performed one punch to the opponent');
  },
  const Duration(seconds: 1),
);

RaisedButton(
  onPressed: (){
    throttledPerformPunch();
  }
  child: Text('Punch')
);
BackOff

BackOff is a strategy that allows you to retry a function call multiple times with a delay between each call. It is useful when you want to retry a function call multiple times in case of failure.

Usage

Creating backoff function is similar to debounce and throttle function.

  1. Creating from scratch
final response = backOff(
  // Make a GET request
  () => http.get('https://google.com').timeout(Duration(seconds: 5)),
  maxAttempts: 5,
  maxDelay: Duration(seconds: 5),
  // Retry on SocketException or TimeoutException
  retryIf: (e, _) => e is SocketException || e is TimeoutException,
);
  1. Converting an existing function into backoff function
Future<String> regularFunction() async {  
  // Make a GET request
  final response = await http.get('https://google.com').timeout(Duration(seconds: 5));
  return response.body;
}

final response = regularFunction.backOff(
  maxAttempts: 5,
  maxDelay: Duration(seconds: 5),
  // Retry on SocketException or TimeoutException
  retryIf: (e, _) => e is SocketException || e is TimeoutException,
);
Example

While making a network request, it is possible that the request fails due to network issues. In such cases, it is useful to retry the request multiple times with a delay between each call. This is where backoff strategy comes in handy.

final response = backOff(
  // Make a GET request
  () => http.get('https://google.com').timeout(Duration(seconds: 5)),
  maxAttempts: 5,
  maxDelay: Duration(seconds: 5),
  // Retry on SocketException or TimeoutException
  retryIf: (e, _) => e is SocketException || e is TimeoutException,
);
Pending

Used to check if the there are functions still remaining to get invoked.

final pending = debouncedFunction.isPending;
final pending = throttledFunction.isPending;
Flush

Used to immediately invoke all the remaining delayed functions.

final result = debouncedFunction.flush();
final result = throttledFunction.flush();
Cancellation

Used to cancel all the remaining delayed functions.

debouncedFunction.cancel();  
throttledFunction.cancel();

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

ランキング