<commit_before><?php

class CMService_KissMetrics_Transport_BufferedSocket implements \KISSmetrics\Transport\Transport {

    /** @var CM_File */
    protected $_buffer;

    /** @var string */
    protected $_host;

    /** @var int */
    protected $_port, $_batchSize;

    /**  @var float */
    protected $_timeout;

    /**
     * @param CM_File $buffer
     * @param string  $host
     * @param int     $port
     * @param float   $timeout
     * @param int     $batchSize
     */
    public function __construct(CM_File $buffer, $host, $port, $timeout, $batchSize) {
        $this->_buffer = $buffer;
        $this->_host = (string) $host;
        $this->_port = (int) $port;
        $this->_timeout = (float) $timeout;
        $this->_batchSize = (int) $batchSize;
    }

    public function submitData(array $dataList) {
        foreach ($dataList as $data) {
            $query = http_build_query($data[1], '', '&');
            $query = str_replace(
                array('+', '%7E'),
                array('%20', '~'),
                $query
            );
            $url = '/' . $data[0] . '?' . $query;
            $this->_buffer->appendLine($url);
        }
        // Flush at random to mitigate concurrency issues
        if (1 === mt_rand(1, $this->_batchSize)) {
            $content = $this->_buffer->read();
            $this->_buffer->truncate();
            $socket = null;
            $urlList = explode(PHP_EOL, $content);
            array_pop($urlList);
            $batchList = array_chunk($urlList, $this->_batchSize);
            foreach ($batchList as $batch) {
                $socket = @fsockopen($this->_host, $this->_port, $errno, $errstr, $this->_timeout);
                if (!$socket) {
                    throw new \KISSmetrics\Transport\TransportException('Cannot connect to the KISSmetrics server: ' . $errstr);
                }
                stream_set_blocking($socket, 0);
                $indexLast = count($batch) - 1;
                foreach ($batch as $i => $url) {
                    $connection = ($indexLast === $i) ? 'Close' : 'Keep-Alive';
                    $request = "GET $url HTTP/1.1\r\nHost: {$this->_host}\r\nConnection: $connection\r\n\r\n";
                    fwrite($socket, $request);
                }
                fclose($socket);
            }
        }
    }

    public static function initDefault() {
        return new static(new CM_File('kissmetrics-buffer.log', CM_Service_Manager::getInstance()->getFilesystems()->getTmp()), 'trk.kissmetrics.com', 80, 30, 100);
    }<commit_msg>Added exception if socket write failed<commit_after><?php

class CMService_KissMetrics_Transport_BufferedSocket implements \KISSmetrics\Transport\Transport {

    /** @var CM_File */
    protected $_buffer;

    /** @var string */
    protected $_host;

    /** @var int */
    protected $_port, $_batchSize;

    /**  @var float */
    protected $_timeout;

    /**
     * @param CM_File $buffer
     * @param string  $host
     * @param int     $port
     * @param float   $timeout
     * @param int     $batchSize
     */
    public function __construct(CM_File $buffer, $host, $port, $timeout, $batchSize) {
        $this->_buffer = $buffer;
        $this->_host = (string) $host;
        $this->_port = (int) $port;
        $this->_timeout = (float) $timeout;
        $this->_batchSize = (int) $batchSize;
    }

    public function submitData(array $dataList) {
        foreach ($dataList as $data) {
            $query = http_build_query($data[1], '', '&');
            $query = str_replace(
                array('+', '%7E'),
                array('%20', '~'),
                $query
            );
            $url = '/' . $data[0] . '?' . $query;
            $this->_buffer->appendLine($url);
        }
        // Flush at random to mitigate concurrency issues
        if (1 === mt_rand(1, $this->_batchSize)) {
            $content = $this->_buffer->read();
            $this->_buffer->truncate();
            $socket = null;
            $urlList = explode(PHP_EOL, $content);
            array_pop($urlList);
            $batchList = array_chunk($urlList, $this->_batchSize);
            foreach ($batchList as $batch) {
                $socket = @fsockopen($this->_host, $this->_port, $errno, $errstr, $this->_timeout);
                if (!$socket) {
                    throw new \KISSmetrics\Transport\TransportException('Cannot connect to the KISSmetrics server: ' . $errstr);
                }
                stream_set_blocking($socket, 0);
                $indexLast = count($batch) - 1;
                foreach ($batch as $i => $url) {
                    $connection = ($indexLast === $i) ? 'Close' : 'Keep-Alive';
                    $request = "GET $url HTTP/1.1\r\nHost: {$this->_host}\r\nConnection: $connection\r\n\r\n";
                    if (!fwrite($socket, $request)) {
                        throw new \KISSmetrics\Transport\TransportException('Could not submit the query: ' . $url);
                    }
                }
                fclose($socket);
            }
        }
    }

    public static function initDefault() {
        return new static(new CM_File('kissmetrics-buffer.log', CM_Service_Manager::getInstance()->getFilesystems()->getTmp()), 'trk.kissmetrics.com', 80, 30, 100);
    }
{'fraction_non_alphanumeric': 0.15296114673708033, 'fraction_numerical': 0.0067898906073179935, 'mean_word_length': 1.8618456556934702, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '6741107', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1505, 'n_words': 334}