Skip to content

Commit

Permalink
fix: increase wait time for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gitcommitshow committed Dec 20, 2023
1 parent ab57057 commit b09f3cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion ENV_SAMPLE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
REPO_OWNER="Git-Commit-Show"
GITHUB_PERSONAL_TOKEN="ghp_adsfdsf32sdfasdfcdcsdfsdf23sfasdf1"
APERTURE_SERVICE_ADDRESS="my.app.fluxninja.com:443"
APERTURE_API_KEY="4dsfs323db7dsfsde310ca6dsdf12sfr4"
APERTURE_API_KEY="4dsfs323db7dsfsde310ca6dsdf12sfr4"
MAX_RATE_LIMIT_WAIT_DURATION_MS=60000
14 changes: 9 additions & 5 deletions network.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as https from 'https';
import { ApertureClient } from "@fluxninja/aperture-js";
import { ApertureClient, FlowStatus } from "@fluxninja/aperture-js";

const MAX_RATE_LIMIT_WAIT_DURATION_MS = process.env.MAX_RATE_LIMIT_WAIT_DURATION_MS || 60000; // 10 mins

var apertureClient;

Expand Down Expand Up @@ -47,7 +48,9 @@ export async function makeRequest(method, url, requestOptions) {
// Handle HTTP response stream
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve({ res, data }));
res.on('end', function(){
resolve({ res, data })
});
});

req.on('error', error => {
Expand Down Expand Up @@ -92,17 +95,18 @@ export async function makeRequest(method, url, requestOptions) {
* }
* */
export async function makeRequestWithRateLimit(method, url, options){
if(!method) method = "GET";
let flow;
try {
flow = await getApertureClient().startFlow("external-api", {
labels: {
url: url,
priority: 1,
tokens: 1,
tokens: ["POST", "PUT", "DELETE", "PATCH"].includes(method.toUpperCase()) ? 5 : 1,
workload: 'api.github'
},
grpcCallOptions: {
deadline: Date.now() + 300, // ms
deadline: Date.now() + MAX_RATE_LIMIT_WAIT_DURATION_MS, // ms
},
});
} catch(err){
Expand All @@ -116,7 +120,7 @@ export async function makeRequestWithRateLimit(method, url, options){
// Add business logic to process incoming request
console.log("Request accepted. Processing...");
// Wait for 500ms
await new Promise(resolve => setTimeout(resolve, 2000));
await new Promise(resolve => setTimeout(resolve, 500));
const {res, data} = await makeRequest(...arguments)
return { res, data}
} else {
Expand Down

0 comments on commit b09f3cd

Please sign in to comment.