<commit_before>var dnode  = require('dnode'),
    config = require('./config.js');

function Client(parent, remote) {
  this.parent = parent;
  this.remote = remote;
  this.pending_search = null;
  this.last_search = null;
}

Client.prototype.new_search = function (str) {
  if (str === this.last_search)
    return;
  this.pending_search = str;
  this.dispatch_search();
}

Client.prototype.dispatch_search = function() {
  if (this.pending_search !== null && this.parent.codesearch) {
    var start = new Date();
    this.last_search = this.pending_search;
    console.log('dispatching: %s...', this.pending_search)
    var search = this.pending_search;
    this.pending_search = null;
    var self   = this;
    var remote = this.remote;
    var cbs = {
      not_ready: function() {
        if (self.pending_search === null)
          self.pending_search = search;
      },
      error: function (err) {
        if (remote.error)
          remote.error(search, err)
      },
      match: function (match) {
        if (remote.match)
          remote.match(search, match);
      },
      done: function () {
        if (remote.search_done)
          remote.search_done(search, (new Date()) - start);
      }
    }
    this.parent.codesearch.try_search(search, cbs);
  }
}

function SearchServer(repo, ref, args) {
  var parent = this;
  this.codesearch = null;
  this.clients = {};

  dnode({
          ready: function() {
            console.log('ready')
            Object.keys(parent.clients).forEach(
              function (id) {
                parent.clients[id].dispatch_search();
              })
          }
        }).connect(
          'localhost', config.DNODE_PORT,
          function (remote) {
            parent.codesearch = remote;
          }, {
            reconnect: 200
          });

  this.Server = function (remote, conn) {
    parent.clients[conn.id] = new Client(parent, remote);
    this.new_search = function(str) {
      parent.clients[conn.id].new_search(str);
    }
    conn.on('end', function() {
              delete parent.clients[conn.id];
            });
  }
}

module.exports = SearchServer;<commit_msg>Better handle reconnecting to the codesearch server.<commit_after>var dnode  = require('dnode'),
    config = require('./config.js');

function Client(parent, remote) {
  this.parent = parent;
  this.remote = remote;
  this.pending_search = null;
  this.last_search = null;
}

Client.prototype.new_search = function (str) {
  if (str === this.last_search)
    return;
  this.pending_search = str;
  this.dispatch_search();
}

Client.prototype.dispatch_search = function() {
  if (this.pending_search !== null &&
      this.parent.codesearch &&
      this.parent.ready) {
    var start = new Date();
    this.last_search = this.pending_search;
    console.log('dispatching: %s...', this.pending_search)
    var search = this.pending_search;
    this.pending_search = null;
    var self   = this;
    var remote = this.remote;
    var cbs = {
      not_ready: function() {
        if (self.pending_search === null)
          self.pending_search = search;
      },
      error: function (err) {
        if (remote.error)
          remote.error(search, err)
      },
      match: function (match) {
        if (remote.match)
          remote.match(search, match);
      },
      done: function () {
        if (remote.search_done)
          remote.search_done(search, (new Date()) - start);
      }
    }
    this.parent.codesearch.try_search(search, cbs);
    this.parent.ready = false;
  }
}

function SearchServer(repo, ref, args) {
  var parent = this;
  this.codesearch = null;
  this.clients = {};
  this.ready   = false;

  function ready() {
    parent.ready = true;
    Object.keys(parent.clients).forEach(
      function (id) {
        parent.clients[id].dispatch_search();
      })
  }

  dnode({
          ready: function() {
            ready();
          }
        }).connect(
          'localhost', config.DNODE_PORT,
          function (remote, conn) {
            parent.codesearch = remote;
            conn.on('ready', ready);
            conn.on('reconnect', ready);
          }, {
            reconnect: 200
          });

  this.Server = function (remote, conn) {
    parent.clients[conn.id] = new Client(parent, remote);
    this.new_search = function(str) {
      parent.clients[conn.id].new_search(str);
    }
    conn.on('end', function() {
              delete parent.clients[conn.id];
            });
  }
}

module.exports = SearchServer;
{'fraction_non_alphanumeric': 0.13715046604527298, 'fraction_numerical': 0.0013315579227696406, 'mean_word_length': 2.2238912732474962, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '183422', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1373, 'n_words': 277}