From aaa1c70a39f21e9be27a6c14a7be56f519550811 Mon Sep 17 00:00:00 2001 From: syz Date: Fri, 14 Aug 2020 15:37:18 +0800 Subject: [PATCH 1/3] fix: yun api qps limit --- src/client.js | 73 +++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/src/client.js b/src/client.js index 1a7cda7..569a597 100644 --- a/src/client.js +++ b/src/client.js @@ -465,7 +465,8 @@ class SlsMonitor { responses.push(newP50) } - async describeCCMInstanceDatas(id, instances, startTime, endTime) { + async describeCCMInstanceDatas(id, instances, startTime, endTime, i, limit) { + const client = new TencentCloudClient(this.credentials, { host: 'monitor.tencentcloudapi.com', path: '/' @@ -479,7 +480,20 @@ class SlsMonitor { EndTime: endTime, TypeId: 'SCF' } - return client.doCloudApiRequest(req) + + const timeCost = 1000 + let sleep = false + if (!((i + 1) % limit)) { + sleep = true + } + return new Promise(function(resolve, rejecte) { + if (!sleep) { + return resolve(client.doCloudApiRequest(req)) + } + setTimeout(function() { + resolve(client.doCloudApiRequest(req)) + }, timeCost) + }); } async describeAttributes(offset, limit) { @@ -498,7 +512,7 @@ class SlsMonitor { } async getCustomMetrics(region, announceInstance, rangeTime, period) { - const apiQPSLimit = 80 + const apiQPSLimit = 100 const metricsRule = [ /^(GET|POST|DEL|DELETE|PUT|OPTIONS|HEAD)_([a-zA-Z0-9]+)_latency$/i, /^(GET|POST|DEL|DELETE|PUT|OPTIONS|HEAD)_([a-zA-Z0-9]+)_(\d+)$/i, @@ -538,45 +552,36 @@ class SlsMonitor { let results let firstRequestFlag = true const attributes = await this.describeAttributes(0, 200) - for (var i = 0; i < attributes.Response.Data.TotalCount; i++) { - const metricAttribute = attributes.Response.Data.Data[i] + attributes.Response.Data.Data.push(attributes.Response.Data.Data[10]) - if (!filterAttributeName(metricAttribute.AttributeName, metricsRule)) { - continue - } - metricAttributeHash[metricAttribute.AttributeId] = metricAttribute - requestHandlers.push( - this.describeCCMInstanceDatas( + let i = 0 + const _this = this + function run() { + if(attributes.Response.Data.Data.length > 0) { + const metricAttribute = attributes.Response.Data.Data.shift() + metricAttributeHash[metricAttribute.AttributeId] = metricAttribute + + return _this.describeCCMInstanceDatas( metricAttribute.AttributeId, announceInstance, rangeTime.rangeStart, - rangeTime.rangeEnd - ) - ) - - if (!((i + 1) % apiQPSLimit)) { - if (i + 1 != apiQPSLimit) { - sleep.sleep(1) - } - firstRequestFlag = false - results = await getMetricsResponse(requestHandlers) - responses = responses.concat(results) - requestHandlers = [] + rangeTime.rangeEnd, + i++, apiQPSLimit + ).then(res => { + responses.push(res) + return run() + }) } } - if (requestHandlers.length === 0) { - responses = this.cleanEmptyMetric(responses, metricAttributeHash) + + const promiseList = Array(Math.min(apiQPSLimit, attributes.Response.Data.Data.length)) + .fill(Promise.resolve()) + .map(promise => promise.then(run)) + + return Promise.all(promiseList).then(() => { this.aggrCustomDatas(responses, period, metricAttributeHash) return responses - } - if (!firstRequestFlag) { - sleep.sleep(1) - } - results = await getMetricsResponse(requestHandlers) - results = this.cleanEmptyMetric(results, metricAttributeHash) - responses = responses.concat(results) - this.aggrCustomDatas(responses, period, metricAttributeHash) - return responses + }) } cleanEmptyMetric(datas, metricAttributeHash) { From 66fb7c713989255eb56eeffbff515f9ebb380d1f Mon Sep 17 00:00:00 2001 From: syz Date: Fri, 14 Aug 2020 15:47:11 +0800 Subject: [PATCH 2/3] change code style --- package.json | 1 - src/client.js | 55 +++++++++++++++++++++------------------------------ 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 3e07e33..3daefac 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,6 @@ "performance-now": "2.1.0", "qs": "6.5.2", "safe-buffer": "5.1.2", - "sleep": "^6.3.0", "tough-cookie": "2.5.0", "tunnel-agent": "0.6.0", "uuid": "3.3.2", diff --git a/src/client.js b/src/client.js index 569a597..d9b7c0b 100644 --- a/src/client.js +++ b/src/client.js @@ -5,7 +5,6 @@ const request = require('../lib/request/index') const crypto = require('crypto') const cos = require('../lib/cos/cos') const _ = require('lodash') -const sleep = require('sleep') const DEFAULTS = { signatureMethod: 'HmacSHA1', @@ -466,7 +465,6 @@ class SlsMonitor { } async describeCCMInstanceDatas(id, instances, startTime, endTime, i, limit) { - const client = new TencentCloudClient(this.credentials, { host: 'monitor.tencentcloudapi.com', path: '/' @@ -486,14 +484,15 @@ class SlsMonitor { if (!((i + 1) % limit)) { sleep = true } - return new Promise(function(resolve, rejecte) { + + return new Promise(function(resolve) { if (!sleep) { return resolve(client.doCloudApiRequest(req)) } setTimeout(function() { resolve(client.doCloudApiRequest(req)) }, timeCost) - }); + }) } async describeAttributes(offset, limit) { @@ -524,18 +523,6 @@ class SlsMonitor { /^5xx$/i ] - const getMetricsResponse = (promiseHandlers) => { - return new Promise((resolve, reject) => { - Promise.all(promiseHandlers) - .then((results) => { - resolve(results) - }) - .catch((error) => { - reject(error) - }) - }) - } - const filterAttributeName = function(name, mRule) { const len = mRule.length for (var i = 0; i < len; i++) { @@ -547,37 +534,39 @@ class SlsMonitor { } const metricAttributeHash = {} - let requestHandlers = [] - let responses = [] - let results - let firstRequestFlag = true + const responses = [] const attributes = await this.describeAttributes(0, 200) attributes.Response.Data.Data.push(attributes.Response.Data.Data[10]) let i = 0 const _this = this function run() { - if(attributes.Response.Data.Data.length > 0) { + if (attributes.Response.Data.Data.length > 0) { const metricAttribute = attributes.Response.Data.Data.shift() metricAttributeHash[metricAttribute.AttributeId] = metricAttribute - - return _this.describeCCMInstanceDatas( - metricAttribute.AttributeId, - announceInstance, - rangeTime.rangeStart, - rangeTime.rangeEnd, - i++, apiQPSLimit - ).then(res => { - responses.push(res) + if (!filterAttributeName(metricAttribute.AttributeName, metricsRule)) { return run() - }) + } + return _this + .describeCCMInstanceDatas( + metricAttribute.AttributeId, + announceInstance, + rangeTime.rangeStart, + rangeTime.rangeEnd, + i++, + apiQPSLimit + ) + .then((res) => { + responses.push(res) + return run() + }) } } const promiseList = Array(Math.min(apiQPSLimit, attributes.Response.Data.Data.length)) .fill(Promise.resolve()) - .map(promise => promise.then(run)) - + .map((promise) => promise.then(run)) + return Promise.all(promiseList).then(() => { this.aggrCustomDatas(responses, period, metricAttributeHash) return responses From d38f1fba8fefb6294bb8b8be64f9a4d23df3b0a7 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Fri, 14 Aug 2020 07:54:40 +0000 Subject: [PATCH 3/3] chore(release): version 1.0.4 ## [1.0.4](https://github.com/serverless-tencent/tencent-cloud-sdk/compare/v1.0.3...v1.0.4) (2020-08-14) ### Bug Fixes * yun api qps limit ([aaa1c70](https://github.com/serverless-tencent/tencent-cloud-sdk/commit/aaa1c70a39f21e9be27a6c14a7be56f519550811)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcef437..3458549 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [1.0.4](https://github.com/serverless-tencent/tencent-cloud-sdk/compare/v1.0.3...v1.0.4) (2020-08-14) + + +### Bug Fixes + +* yun api qps limit ([aaa1c70](https://github.com/serverless-tencent/tencent-cloud-sdk/commit/aaa1c70a39f21e9be27a6c14a7be56f519550811)) + ## [1.0.3](https://github.com/serverless-tencent/tencent-cloud-sdk/compare/v1.0.2...v1.0.3) (2020-08-13) diff --git a/package.json b/package.json index 3daefac..de83e27 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "tencent-cloud-sdk", "description": "Tencent Cloud SDK For Serverless Framework", - "version": "1.0.3", + "version": "1.0.4", "main": "./index.js", "publishConfig": { "access": "public"