var ConsistentHash = require('consistent-hash');
var hr = new ConsistentHash({distribution: "uniform"});
hr.add('origin_0');
hr.add('origin_1');
hr.add('origin_2');
addEventListener("fetch", event => {
const req = event.request;
const url = new URL(req.url);
const resourceName = url.pathname + url.search;
const backendToUse = hr.get(resourceName);
console.log(resourceName + " ---> " + backendToUse);
const backendResponse = fetch(req, {
backend: backendToUse
});
event.respondWith(backendResponse);
});