Skip to content

Commit 04be298

Browse files
author
James Prendergast
committed
randomise pool choice when no cookie is present
1 parent 5f84434 commit 04be298

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

edge-functions/src/originrequest.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const origins = {
66
};
77
const cookieName = 'pool';
88

9-
function parseCookies(headers) {
9+
const parseCookies = (headers) => {
1010
const parsedCookie = {};
1111
if (headers.cookie) {
1212
headers.cookie[0].value.split(';').forEach((cookie) => {
@@ -19,6 +19,8 @@ function parseCookies(headers) {
1919
return parsedCookie;
2020
}
2121

22+
const rollTheDice = () => Math.floor(Math.random()*3) === 0 ? 'b' : 'a';
23+
2224
exports.handler = (event, context, callback) => {
2325
const request = event.Records[0].cf.request;
2426
const headers = request.headers;
@@ -31,6 +33,8 @@ exports.handler = (event, context, callback) => {
3133

3234
if(parsedCookies && parsedCookies[cookieName]){
3335
targetPool = parsedCookies[cookieName];
36+
}else{
37+
targetPool = rollTheDice();
3438
}
3539

3640
let s3Origin = origins[targetPool];

0 commit comments

Comments
 (0)