@ fastify/ throttle (法语)
调高请求的下载速度 .
安装
npm i @fastify/throttle
使用量
注册插件,并在必要时通过自定义选项.
此插件将添加一个 onSend 钩到快取实例,它会拖动响应的下载速度.
import Fastify from 'fastify'
const fastify = Fastify()
await fastify.register(import('@fastify/throttle'), {
bytesPerSecond: 1024 * 1024, // 1MB/s
streamPayloads: true, // throttle the payload if it is a stream
bufferPayloads: true, // throttle the payload if it is a Buffer
stringPayloads: true // throttle the payload if it is a string
})
fastify.get('/', (request, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen({ port: 3000 }, err => {
if (err) {
throw err
}
console.log('Server listening at http://localhost:3000')
})
选项
在插件注册期间, 您可以通过以下选项 :
await fastify.register(import('@fastify/throttle'), {
bytesPerSecond: 1000, // 1000 bytes per second
streamPayloads: true, // throttle the payload if it is a stream
bufferPayloads: true, // throttle the payload if it is a Buffer
stringPayloads: true // throttle the payload if it is a string
})
您可以将节流全球定义为插件选项或每个路由选项. 每个路由的节流阀选项与插件选项相同.
| 页眉 | 说明 | 默认 |
|---|---|---|
bytesPerSecond | 每秒允许的字节、 数字或函数 | 16384 (英语) |
streamPayloads | 如果有效载荷是溪流的话,就把它压住 | 真实 |
bufferPayloads | 如果有效载荷是缓冲器,则将其吸出 | 虚假 |
stringPayloads | 如果有效载荷是绳子, 则按下 | 虚假 |
async | 如果字节Persecond是返回承诺的函数, 设置为 true | 虚假 |
在全球范围设置节奏的实例:
const fastify = require('fastify')()
await fastify.register(import('@fastify/throttle'), {
bytesPerSecond: 1024 // 1KB/s
})
fastify.get('/', (req, reply) => {
reply.send(createReadStream(resolve(__dirname, __filename)))
})
fastify.listen({ port: 3000 })
设置每条路线节奏的示例:
'use strict'
const fastify = require('fastify')()
await fastify.register(import('@fastify/throttle'))
fastify.get('/', {
config: {
throttle: {
bytesPerSecond: 1000
}
}
}, (req, reply) => {
reply.send(createReadStream(resolve(__dirname, __filename)))
})
fastify.listen({ port: 3000 })
这个 bytesPerSecond 选项可以是数字或函数。 函数用于 bytesPerSecond 有以下类型脚本定义 :
type BytesPerSecond = (request: FastifyRequest) => ((elapsedTime: number, bytes: number) => number) | Promise<((elapsedTime: number, bytes: number) => number)>
request 是快捷请求对象.
elapsedTime 是流起于几秒后的时间.
bytes 已经发送的字节.
您必须确保返回值为整数或 Infinity。 。 。 .
例如,您可以通过定义 :
页:1 bytesPerSecond 像这样:
const fastify = require('fastify')()
await fastify.register(import('@fastify/throttle'))
fastify.get('/', {
config: {
throttle: {
bytesPerSecond: function bytesPerSecondfactory(request) {
// this function runs for every request
const client = request.headers['customer-id']
return function (elapsedTime, bytes) {
return CONFIG[client] * 2 // return a number of xyz
}
}
}
}
}, (req, reply) => {
reply.send(createReadStream(resolve(__dirname, __filename)))
})
fastify.listen({ port: 3000 })
这个 bytesPerSecond 函数可以是同步函数,也可以是同步函数。 如果您提供了同位素函数, 它会被插件检测到 。 如果是一个同步函数返回承诺, 您必须设置 async 选项到 true,以便插件知道它应该等待承诺.
const fastify = require('fastify')()
await fastify.register(import('@fastify/throttle'))
fastify.get('/', {
config: {
throttle: {
async: true,
bytesPerSecond: function bytesPerSecondfactory(request) {
// this function runs for every request
const client = request.headers['customer-id']
return Promise.resolve(function (elapsedTime, bytes) {
return CONFIG[client] * 2 // return a number of xyz
})
}
}
}
}, (req, reply) => {
reply.send(createReadStream(resolve(__dirname, __filename)))
})
fastify.listen({ port: 3000 })