<commit_before>'use strict';

var net                    = require('net');
var Promise                = require('./bluebird-configured');
var NoKafkaConnectionError = require('./errors').NoKafkaConnectionError;

function Connection(options) {
    // options
    this.host = options.host || '127.0.0.1';
    this.port = options.port || 9092;

    // internal state
    this.connected = false;
    this.buffer = new Buffer(256 * 1024);
    this.offset = 0;

    this.queue = [];
}

module.exports = Connection;

Connection.prototype.equal = function (host, port) {
    return this.host === host && this.port === port;
};

Connection.prototype.server = function () {
    return this.host + ':' + this.port;
};

Connection.prototype.connect = function (timeout) {
    var self = this;

    if (self.connected) {
        return Promise.resolve();
    }

    if (self.connecting) {
        return self.connecting;
    }

    self.connecting = Promise.race([
        new Promise(function (resolve, reject) {
            setTimeout(function () {
                reject(new NoKafkaConnectionError(self.server(), 'Connection timeout'));
            }, timeout || 3000);
        }),
        new Promise(function (resolve, reject) {
            if (self.socket) {
                self.socket.destroy();
            }

            self.socket = new net.Socket();
            self.socket.on('end', function () {
                self._disconnect(new NoKafkaConnectionError(self.server(), 'Kafka server has closed connection'));
            });
            self.socket.on('error', function (err) {
                var _err = new NoKafkaConnectionError(self.server(), err.toString());
                reject(_err);
                self._disconnect(_err);
            });
            self.socket.on('data', self._receive.bind(self));

            self.socket.connect(self.port, self.host, function () {
                self.connected = true;
                resolve();
            });
        })
    ])
    .finally(function () {
        self.connecting = false;
    });

    return self.connecting;
};

// Private disconnect method, this is what the 'end' and 'error'
// events call directly to make sure internal state is maintained
Connection.prototype._disconnect = function (err) {
    if (!this.connected) {
        return;<commit_msg>Handle rapid shutdown of new instances<commit_after>'use strict';

var net                    = require('net');
var Promise                = require('./bluebird-configured');
var NoKafkaConnectionError = require('./errors').NoKafkaConnectionError;

function Connection(options) {
    // options
    this.host = options.host || '127.0.0.1';
    this.port = options.port || 9092;

    // internal state
    this.connected = false;
    this.buffer = new Buffer(256 * 1024);
    this.offset = 0;

    this.queue = [];
}

module.exports = Connection;

Connection.prototype.equal = function (host, port) {
    return this.host === host && this.port === port;
};

Connection.prototype.server = function () {
    return this.host + ':' + this.port;
};

Connection.prototype.connect = function (timeout) {
    var self = this;

    if (self.connected) {
        return Promise.resolve();
    }

    if (self.connecting) {
        return self.connecting;
    }

    self.connecting = Promise.race([
        new Promise(function (resolve, reject) {
            setTimeout(function () {
                reject(new NoKafkaConnectionError(self.server(), 'Connection timeout'));
            }, timeout || 3000);
        }),
        new Promise(function (resolve, reject) {
            if (self.socket) {
                self.socket.destroy();
            }
            // If we have aborted/stopped during startup, don't progress.
            if (self._disconnect_called) {
                reject(new NoKafkaConnectionError(self.server(), 'Connection was aborted before connection was established.'));
                return;
            }

            self.socket = new net.Socket();
            self.socket.on('end', function () {
                self._disconnect(new NoKafkaConnectionError(self.server(), 'Kafka server has closed connection'));
            });
            self.socket.on('error', function (err) {
                var _err = new NoKafkaConnectionError(self.server(), err.toString());
                reject(_err);
                self._disconnect(_err);
            });
            self.socket.on('data', self._receive.bind(self));

            self.socket.connect(self.port, self.host, function () {
                self.connected = true;
                resolve();
            });
        })
    ])
    .finally(function () {
        self.connecting = false;
    });

    return self.connecting;
};

// Private disconnect method, this is what the 'end' and 'error'
// events call directly to make sure internal state is maintained
Connection.prototype._disconnect = function (err) {
    this._disconnect_called = true;

    if (!this.connected) {
        return;
{'fraction_non_alphanumeric': 0.13163694650370666, 'fraction_numerical': 0.008815868563414146, 'mean_word_length': 2.083384805435454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6840974', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1298, 'n_words': 310}