Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaa2e8ff

Browse files
committed
feat: rate limit error chaching
Rate limit error caching to alleviate PATs.
1 parent60fae29 commitaa2e8ff

File tree

8 files changed

+48
-17
lines changed

8 files changed

+48
-17
lines changed

‎api/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default async (req, res) => {
5656
);
5757

5858
constcacheSeconds=clampValue(
59-
parseInt(cache_seconds||CONSTANTS.FOUR_HOURS,10),
59+
parseInt(cache_seconds||CONSTANTS.CARD_CACHE_SECONDS,10),
6060
CONSTANTS.FOUR_HOURS,
6161
CONSTANTS.ONE_DAY,
6262
);
@@ -93,7 +93,12 @@ export default async (req, res) => {
9393
}),
9494
);
9595
}catch(err){
96-
res.setHeader("Cache-Control",`no-cache, no-store, must-revalidate`);// Don't cache error responses.
96+
res.setHeader(
97+
"Cache-Control",
98+
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS/2}, s-maxage=${
99+
CONSTANTS.ERROR_CACHE_SECONDS
100+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
101+
);// Cache the error response less frequently.
97102
returnres.send(renderError(err.message,err.secondaryMessage));
98103
}
99104
};

‎api/pin.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default async (req, res) => {
4040
constrepoData=awaitfetchRepo(username,repo);
4141

4242
letcacheSeconds=clampValue(
43-
parseInt(cache_seconds||CONSTANTS.FOUR_HOURS,10),
43+
parseInt(cache_seconds||CONSTANTS.CARD_CACHE_SECONDS,10),
4444
CONSTANTS.FOUR_HOURS,
4545
CONSTANTS.ONE_DAY,
4646
);
@@ -80,7 +80,12 @@ export default async (req, res) => {
8080
}),
8181
);
8282
}catch(err){
83-
res.setHeader("Cache-Control",`no-cache, no-store, must-revalidate`);// Don't cache error responses.
83+
res.setHeader(
84+
"Cache-Control",
85+
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS/2}, s-maxage=${
86+
CONSTANTS.ERROR_CACHE_SECONDS
87+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
88+
);// Cache the error response less frequently.
8489
returnres.send(renderError(err.message,err.secondaryMessage));
8590
}
8691
};

‎api/top-langs.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default async (req, res) => {
4848
);
4949

5050
constcacheSeconds=clampValue(
51-
parseInt(cache_seconds||CONSTANTS.FOUR_HOURS,10),
51+
parseInt(cache_seconds||CONSTANTS.CARD_CACHE_SECONDS,10),
5252
CONSTANTS.FOUR_HOURS,
5353
CONSTANTS.ONE_DAY,
5454
);
@@ -80,7 +80,12 @@ export default async (req, res) => {
8080
}),
8181
);
8282
}catch(err){
83-
res.setHeader("Cache-Control",`no-cache, no-store, must-revalidate`);// Don't cache error responses.
83+
res.setHeader(
84+
"Cache-Control",
85+
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS/2}, s-maxage=${
86+
CONSTANTS.ERROR_CACHE_SECONDS
87+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
88+
);// Cache the error response less frequently.
8489
returnres.send(renderError(err.message,err.secondaryMessage));
8590
}
8691
};

‎api/wakatime.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,11 @@ export default async (req, res) => {
4343
conststats=awaitfetchWakatimeStats({ username, api_domain, range});
4444

4545
letcacheSeconds=clampValue(
46-
parseInt(cache_seconds||CONSTANTS.FOUR_HOURS,10),
46+
parseInt(cache_seconds||CONSTANTS.CARD_CACHE_SECONDS,10),
4747
CONSTANTS.FOUR_HOURS,
4848
CONSTANTS.ONE_DAY,
4949
);
5050

51-
if(!cache_seconds){
52-
cacheSeconds=CONSTANTS.FOUR_HOURS;
53-
}
54-
5551
res.setHeader(
5652
"Cache-Control",
5753
`max-age=${
@@ -80,7 +76,12 @@ export default async (req, res) => {
8076
}),
8177
);
8278
}catch(err){
83-
res.setHeader("Cache-Control",`no-cache, no-store, must-revalidate`);// Don't cache error responses.
79+
res.setHeader(
80+
"Cache-Control",
81+
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS/2}, s-maxage=${
82+
CONSTANTS.ERROR_CACHE_SECONDS
83+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
84+
);// Cache the error response less frequently.
8485
returnres.send(renderError(err.message,err.secondaryMessage));
8586
}
8687
};

‎src/common/retryer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ const retryer = async (fetcher, variables, retries = 0) => {
6060
}
6161
};
6262

63-
export{retryer};
63+
export{retryer,RETRIES};
6464
exportdefaultretryer;

‎src/common/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,21 @@ const noop = () => {};
293293
constlogger=
294294
process.env.NODE_ENV!=="test" ?console :{log:noop,error:noop};
295295

296+
// Cache settings.
297+
constCARD_CACHE_SECONDS=14400;
298+
constERROR_CACHE_SECONDS=600;
299+
296300
constCONSTANTS={
301+
ONE_MINUTE:60,
302+
FIVE_MINUTES:300,
303+
TEN_MINUTES:600,
304+
FIFTEEN_MINUTES:900,
297305
THIRTY_MINUTES:1800,
298306
TWO_HOURS:7200,
299307
FOUR_HOURS:14400,
300308
ONE_DAY:86400,
309+
CARD_CACHE_SECONDS:CARD_CACHE_SECONDS,
310+
ERROR_CACHE_SECONDS:ERROR_CACHE_SECONDS,
301311
};
302312

303313
constSECONDARY_ERROR_MESSAGES={

‎tests/api.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,18 @@ describe("Test /api/", () => {
171171
]);
172172
});
173173

174-
it("shouldnot store cache when error",async()=>{
174+
it("shouldset shorter cache when error",async()=>{
175175
const{ req, res}=faker({},error);
176176
awaitapi(req,res);
177177

178178
expect(res.setHeader.mock.calls).toEqual([
179179
["Content-Type","image/svg+xml"],
180-
["Cache-Control",`no-cache, no-store, must-revalidate`],
180+
[
181+
"Cache-Control",
182+
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS/2}, s-maxage=${
183+
CONSTANTS.ERROR_CACHE_SECONDS
184+
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
185+
],
181186
]);
182187
});
183188

‎tests/retryer.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import{jest}from"@jest/globals";
22
import"@testing-library/jest-dom";
3-
import{retryer}from"../src/common/retryer.js";
3+
import{retryer,RETRIES}from"../src/common/retryer.js";
44
import{logger}from"../src/common/utils.js";
55

66
constfetcher=jest.fn((variables,token)=>{
@@ -45,7 +45,7 @@ describe("Test Retryer", () => {
4545
try{
4646
res=awaitretryer(fetcherFail,{});
4747
}catch(err){
48-
expect(fetcherFail).toBeCalledTimes(8);
48+
expect(fetcherFail).toBeCalledTimes(RETRIES+1);
4949
expect(err.message).toBe("Maximum retries exceeded");
5050
}
5151
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp