<commit_before>var net = require('net');
var events = require('events');
var util = require('util');
var writers = require('./writers.js');
var streams = require('./streams.js');
var utils = require('./utils.js');
var types = require('./types.js');

var optionsDefault = {
  port:  9042,
  version: '3.0.0',
  //max simultaneous requests (before waiting for a response) (max=128)
  maxRequests: 32,
  //When the simultaneous requests has been reached, it determines the amount of milliseconds before retrying to get an available streamId
  maxRequestsRetry: 100,
  //Connect timeout: time to wait when trying to connect to a host,
  connectTimeout: 2000,
  //Default page size, it can be overridden per execution
  pageSize: 5000
};
function Connection(options) {
  events.EventEmitter.call(this);

  this.streamHandlers = {};
  this.options = utils.extend({}, optionsDefault, options);

  this.latencyHistory = [];
}

util.inherits(Connection, events.EventEmitter);

Connection.prototype.createSocket = function() {
  var self = this;
  if (this.netClient) {
    this.netClient.destroy();
  }
  this.netClient = new net.Socket();
  this.writeQueue = new writers.WriteQueue(this.netClient);
  var protocol = new streams.Protocol({objectMode: true});
  this.parser = new streams.Parser({objectMode: true});
  var resultEmitter = new streams.ResultEmitter({objectMode: true});
  this.netClient
    .pipe(protocol)
    .pipe(this.parser)
    .pipe(resultEmitter);

  resultEmitter.on('result', this.handleResult.bind(this));
  resultEmitter.on('row', this.handleRow.bind(this));
  resultEmitter.on('frameEnded', this.freeStreamId.bind(this));
  
  this.netClient.on('close', function() {
    self.emit('log', 'info', 'Socket disconnected');
    self.connected = false;
    self.connecting = false;
    self.invokePendingCallbacks();
  });
};

/** 
 * Connects a socket and sends the startup protocol messages, including authentication and the keyspace used. 
 */
Connection.prototype.open = function (callback) {
  var self = this;
  self.emit('log', 'info', 'Connecting to ' + this.options.host + ':' + this.options.port);
  self.createSocket();
  self.connecting = true;
  function errorConnecting (err, destroy) {
    self.connecting = false;
    if (destroy) {
      //there is a TCP connection that should be killed.
      self.netClient.destroy();
    }
    callback(err);
  }
  this.netClient.once('error', errorConnecting);
  this.netClient.once('timeout', function connectTimeout() {
    var err = new types.DriverError('Connection timeout');
    errorConnecting(err, true);
  });
  this.netClient.setTimeout(this.options.connectTimeout);
  
  this.netClient.connect(this.options.port, this.options.host, function connectCallback() {
    self.emit('log', 'info', 'Socket connected to ' + self.options.host + ':' + self.options.port);
    self.netClient.removeListener('error', errorConnecting);
    self.netClient.removeAllListeners('connect');
    self.netClient.removeAllListeners('timeout');
    
    self.sendStream(new writers.StartupWriter(self.options.version), null, function (err, response) {
      if (response && response.mustAuthenticate) {
        return self.authenticate(startupCallback);<commit_msg>Log host:port on connection timeout<commit_after>var net = require('net');
var events = require('events');
var util = require('util');
var writers = require('./writers.js');
var streams = require('./streams.js');
var utils = require('./utils.js');
var types = require('./types.js');

var optionsDefault = {
  port:  9042,
  version: '3.0.0',
  //max simultaneous requests (before waiting for a response) (max=128)
  maxRequests: 32,
  //When the simultaneous requests has been reached, it determines the amount of milliseconds before retrying to get an available streamId
  maxRequestsRetry: 100,
  //Connect timeout: time to wait when trying to connect to a host,
  connectTimeout: 2000,
  //Default page size, it can be overridden per execution
  pageSize: 5000
};
function Connection(options) {
  events.EventEmitter.call(this);

  this.streamHandlers = {};
  this.options = utils.extend({}, optionsDefault, options);

  this.latencyHistory = [];
}

util.inherits(Connection, events.EventEmitter);

Connection.prototype.createSocket = function() {
  var self = this;
  if (this.netClient) {
    this.netClient.destroy();
  }
  this.netClient = new net.Socket();
  this.writeQueue = new writers.WriteQueue(this.netClient);
  var protocol = new streams.Protocol({objectMode: true});
  this.parser = new streams.Parser({objectMode: true});
  var resultEmitter = new streams.ResultEmitter({objectMode: true});
  this.netClient
    .pipe(protocol)
    .pipe(this.parser)
    .pipe(resultEmitter);

  resultEmitter.on('result', this.handleResult.bind(this));
  resultEmitter.on('row', this.handleRow.bind(this));
  resultEmitter.on('frameEnded', this.freeStreamId.bind(this));
  
  this.netClient.on('close', function() {
    self.emit('log', 'info', 'Socket disconnected');
    self.connected = false;
    self.connecting = false;
    self.invokePendingCallbacks();
  });
};

/** 
 * Connects a socket and sends the startup protocol messages, including authentication and the keyspace used. 
 */
Connection.prototype.open = function (callback) {
  var self = this;
  self.emit('log', 'info', 'Connecting to ' + this.options.host + ':' + this.options.port);
  self.createSocket();
  self.connecting = true;
  function errorConnecting (err, destroy) {
    self.connecting = false;
    if (destroy) {
      //there is a TCP connection that should be killed.
      self.netClient.destroy();
    }
    callback(err);
  }
  this.netClient.once('error', errorConnecting);
  this.netClient.once('timeout', function connectTimeout() {
    var err = new types.DriverError('Connection timeout to host ' + self.options.host + ':' + self.options.port);
    errorConnecting(err, true);
  });
  this.netClient.setTimeout(this.options.connectTimeout);
  
  this.netClient.connect(this.options.port, this.options.host, function connectCallback() {
    self.emit('log', 'info', 'Socket connected to ' + self.options.host + ':' + self.options.port);
    self.netClient.removeListener('error', errorConnecting);
    self.netClient.removeAllListeners('connect');
    self.netClient.removeAllListeners('timeout');
    
    self.sendStream(new writers.StartupWriter(self.options.version), null, function (err, response) {
      if (response && response.mustAuthenticate) {
        return self.authenticate(startupCallback);
{'fraction_non_alphanumeric': 0.13974535971774812, 'fraction_numerical': 0.00705629697806412, 'mean_word_length': 4.266558966074314, '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': '6371180', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1912, 'n_words': 473}