preface

In daily development, we encounter many scenarios like this:

  1. In the panic buying activity, users often refresh the interface frequently, so as to protect the interface and call stop response frequently.
  2. In a weak network environment, the retry function is usually implemented. If the number of failures is too many, the frequent retry must be stopped.
  3. In the stock market, the suspension of trading for a period of time when a price fluctuation reaches a limited circuit breaker during trading hours.
  4. .

The essence of this problem is: “overheated calls”

In physical circuits, there is a common electronic component for “overheated calls” : fuses

The fuse will cut off the current and protect the safe operation of the circuit when the current abnormally rises to a certain height and heat.

We can mimic this idea to solve the problem of “overheated calls” in programming:

  1. Set a threshold, and if the function reaches this threshold in a short period of time, the circuit will be shut down for a period of time.
  2. The function has not been called for a while, so that the function heat down.

Function fuse function realization

Based on the above ideas, I implemented an NPM library: Method Fuse

The usage is as follows:

Step1: installation

npm install @jerryc/method-fuse

Step2: Quick use

import { MethodFuse } from '@jerryc/method-fuse';

// An asynchronous function that requests a remote resource
const getAssets = async () => API.requestAssets();

// Create a MethodFuse instance
const fuse = new MethodFuse({
  // Name it for log output
  name: 'TestFuse'.// Maximum load, default: 3
  maxLoad: 3.// Each fusing time. After each fuse break, recasting interval N milliseconds later, default: 5000ms
  breakingTime: 5000.// Automatic cooling time. Automatic recasting after the last call interval N milliseconds, default: 1000ms
  coolDownTime: 1000});// proxy function
const getAssetsProxy = fuse.proxy(getAssets);

// call getAssetsProxy frequently concurrently.
getAssetsProxy();
getAssetsProxy();
getAssetsProxy();
getAssetsProxy(); // This call will fuse
setTimeout(() = > getAssetsProxy(), 5000); // Wait until the fuse is recast before calling again.

// This will print a log:
// [method-fuse:info] TestFuse- through fuse (1/3)
// [method-fuse:info] TestFuse- through fuse (2/3)
// [method-fuse:info] TestFuse- through fuse (3/3)
// [method-fuse:error] TestFuse- Fuse is blown and recast after 5000ms
// [method-fuse:info] TestFuse- reset
// [method-fuse:info] TestFuse- through fuse (1/3)
Copy the code

Step3: Use a decorator

If your project supports TS or ES decorators, MethodFuse provides a Decorator that can be used quickly.

import { decorator as methodFuse } from '@jerryc/method-fuse';

@methodFuse({ name: 'TestFuse' })
async function getAsset() {
  return API.requestAssets();
};
Copy the code

Step4: change the log level

MethodFuse has a built-in mini-Logger (Power by @Jerryc/Mini-Logger) for internal log printing and external access to logger instances for log level control.

import { LoggerLevel } from '@jerryc/mini-logger';
import { logger, MethodFuse } from '@jerryc/method-fuse';

// Create a MethodFuse instance
const MethodFuse = new MethodFuse({ name: 'TestFuse' });

// Lower the Log level
logger.level = LoggerLevel.ERROR;
Copy the code