Skip to content

Commit 596b512

Browse files
authored
Merge pull request #118 from dashjim/master
feat: throtlling feature added.
2 parents b95fcf9 + cb0f9ff commit 596b512

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

source/new-image-handler/src/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Koa from 'koa'; // http://koajs.cn
55
import * as bodyParser from 'koa-bodyparser';
66
import * as logger from 'koa-logger';
77
import * as Router from 'koa-router';
8+
import * as sharp from 'sharp';
89
import config from './config';
910
import debug from './debug';
1011
import { bufferStore, getProcessor, parseRequest } from './default';
@@ -14,6 +15,8 @@ const DefaultBufferStore = bufferStore();
1415
const app = new Koa();
1516
const router = new Router();
1617

18+
sharp.cache({ items: 1000, files: 200, memory: 2000 });
19+
1720
app.use(logger());
1821
app.use(errorHandler());
1922
app.use(bodyParser());
@@ -51,6 +54,12 @@ router.get(['/debug', '/_debug'], async (ctx) => {
5154
});
5255

5356
router.get('/(.*)', async (ctx) => {
57+
const queue = sharp.counters().queue;
58+
if (queue > 1) {
59+
ctx.body = { message: 'Too many requests, please try again later.' };
60+
ctx.status = 429;
61+
return;
62+
}
5463
const { data, type, headers } = await ossprocess(ctx, bypass);
5564
ctx.body = data;
5665
ctx.type = type;

0 commit comments

Comments
 (0)