<commit_before>var Cookies = Npm.require('cookies');
var HttpProxy = Npm.require('http-proxy');
var urlParse = Npm.require('url').parse;
var urlResolve = Npm.require('url').resolve;

Balancer = {};
Balancer.proxy = HttpProxy.createProxyServer({
  xfwd: true
});

Balancer._urlToTarget = function _urlToTarget(url) {
  var parsedUrl = urlParse(url);
  var target = {
    host: parsedUrl.hostname,
    port: parsedUrl.port
  };

  return target;
};

Balancer._pickAndSetEndpointHash =
function _pickAndSetEndpointHash(cookies) {
  var endpointHash = Cluster.discovery.pickEndpointHash("web");
  if(endpointHash) {
    cookies.set('cluster-endpoint', endpointHash, {httpOnly: false});
    return endpointHash;
  } else {
    // no endpoint point, simply process the request here
    return false;
  }
};

Balancer._pickEndpoint =
function _pickEndpoint(endpointHash, cookies, retries) {
  retries = retries || 0;
  if(retries > 1) return false;

  if(!endpointHash) {
    endpointHash = Balancer._pickAndSetEndpointHash(cookies);
    if(!endpointHash) {
      // no endpoint, simply process the request here
      return false;
    }
  }
<commit_msg>Expire cluster-endpoint cookie in 1hour This is to make sure better distribution of load even there are a lot of long lasting sessions<commit_after>var Cookies = Npm.require('cookies');
var HttpProxy = Npm.require('http-proxy');
var urlParse = Npm.require('url').parse;
var urlResolve = Npm.require('url').resolve;

Balancer = {};
Balancer.proxy = HttpProxy.createProxyServer({
  xfwd: true
});

Balancer._urlToTarget = function _urlToTarget(url) {
  var parsedUrl = urlParse(url);
  var target = {
    host: parsedUrl.hostname,
    port: parsedUrl.port
  };

  return target;
};

Balancer._pickAndSetEndpointHash =
function _pickAndSetEndpointHash(cookies) {
  var endpointHash = Cluster.discovery.pickEndpointHash("web");
  if(endpointHash) {
    var cookieOptions = {
      httpOnly: true,
      // expire the cookie in one hour
      // then we can expect that long lasting sessions
      // does not affect to the load distribution much
      maxAge: 1000 * 60 * 60
    };
    cookies.set('cluster-endpoint', endpointHash, cookieOptions);
    return endpointHash;
  } else {
    // no endpoint point, simply process the request here
    return false;
  }
};

Balancer._pickEndpoint =
function _pickEndpoint(endpointHash, cookies, retries) {
  retries = retries || 0;
  if(retries > 1) return false;

  if(!endpointHash) {
    endpointHash = Balancer._pickAndSetEndpointHash(cookies);
    if(!endpointHash) {
      // no endpoint, simply process the request here
      return false;
    }
  }

{'fraction_non_alphanumeric': 0.11081694402420575, 'fraction_numerical': 0.004916792738275341, 'mean_word_length': 3.88909426987061, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2845363', 'n_tokens_mistral': 811, 'n_tokens_neox': 789, 'n_words': 211}