瀏覽代碼

修复一些问题

zhusiqing 4 年之前
父節點
當前提交
71a75cdade
共有 2 個文件被更改,包括 6 次插入6 次删除
  1. 1 0
      src/middlewares/limit.ts
  2. 5 6
      src/middlewares/redis.ts

+ 1 - 0
src/middlewares/limit.ts

@@ -13,6 +13,7 @@ export default () => {
     const limit: number = Number(await ctx.$redis.get(key)) || 0;
     if (limit > config.limit.times) {
       ctx.throw(403, '请求频次过高');
+      return
     };
     ctx.$redis.setex(key, config.limit.time, String(limit + 1));
     await next();

+ 5 - 6
src/middlewares/redis.ts

@@ -31,13 +31,12 @@ const client = redis.createClient({
     }
     if (options.attempt > 10) {
       // End reconnecting with built in error
-      return undefined;
+      return;
     }
     // reconnect after
-    return Math.min(options.attempt * 100, 3000);
+    return Math.min(options.attempt * 1000, 3000);
   }
 });
-
 client.on('error', err => {
   consola.error(err);
 });
@@ -57,9 +56,9 @@ client.on('end', () => {
 
 // redis查询和设置await/async化
 const promisify = util.promisify;
-const getClient = (key) => promisify(client.get).bind(client, key);
-const setClient = (key, value) => promisify(client.set).bind(client, key, value);
-const setexClient = (key, ttl, value) => promisify(client.setex).bind(client, key, ttl, value);
+const getClient = (key) => promisify(client.get).bind(client)(key);
+const setClient = (key, value) => promisify(client.set).bind(client)(key, value);
+const setexClient = (key, ttl, value) => promisify(client.setex).bind(client)(key, ttl, value);
 
 // 内存存储promise化,为了和redis使用方法保持一致
 const getCache = (key: string): Promise<string|undefined> => {