<commit_before>require 'ffi'
require 'nn-core'
require 'aggro/nanomsg_transport/socket_error'

module Aggro
  module NanomsgTransport
    # Private: Base class for nanomsg socket wrappers.
    class Connection
      TIMEOUT_ERRNO = 35

      def initialize(endpoint)
        @endpoint = endpoint
        @rcv_buffer = FFI::MemoryPointer.new(:pointer)

        allocate_socket
        setup_socket
        set_endpoint
      end

      def setup_socket
        set_socket_option NNCore::NN_LINGER, 100
        set_socket_option NNCore::NN_SNDBUF, 131_072
        set_socket_option NNCore::NN_RCVBUF, 131_072
      end

      def send_msg(stringable)
        msg = stringable.to_s
        nbytes = NNCore::LibNanomsg.nn_send(@socket, msg, msg.bytesize, 0)
        assert(nbytes)

        nbytes
      end

      def recv_msg
        nbytes = NNCore::LibNanomsg.nn_recv(@socket, @rcv_buffer,
                                            NNCore::NN_MSG, 0)

        assert(nbytes)

        str = @rcv_buffer.read_pointer
        response = str.read_string(nbytes)
        NNCore::LibNanomsg.nn_freemsg str

        response
      rescue SocketError => e
        raise e unless e.errno == TIMEOUT_ERRNO

        nil
      end

      def terminate
        assert NNCore::LibNanomsg.nn_close(@socket)
      end

      protected

      def assert(rc)
        fail SocketError.new NNCore::LibNanomsg.nn_errno unless rc >= 0
      end<commit_msg>Use nn-core provided constant to match EAGAIN.<commit_after>require 'ffi'
require 'nn-core'
require 'aggro/nanomsg_transport/socket_error'

module Aggro
  module NanomsgTransport
    # Private: Base class for nanomsg socket wrappers.
    class Connection
      def initialize(endpoint)
        @endpoint = endpoint
        @rcv_buffer = FFI::MemoryPointer.new(:pointer)

        allocate_socket
        setup_socket
        set_endpoint
      end

      def setup_socket
        set_socket_option NNCore::NN_LINGER, 100
        set_socket_option NNCore::NN_SNDBUF, 131_072
        set_socket_option NNCore::NN_RCVBUF, 131_072
      end

      def send_msg(stringable)
        msg = stringable.to_s
        nbytes = NNCore::LibNanomsg.nn_send(@socket, msg, msg.bytesize, 0)
        assert(nbytes)

        nbytes
      end

      def recv_msg
        nbytes = NNCore::LibNanomsg.nn_recv(@socket, @rcv_buffer,
                                            NNCore::NN_MSG, 0)

        assert(nbytes)

        str = @rcv_buffer.read_pointer
        response = str.read_string(nbytes)
        NNCore::LibNanomsg.nn_freemsg str

        response
      rescue SocketError => e
        raise e unless e.errno == NNCore::EAGAIN

        nil
      end

      def terminate
        assert NNCore::LibNanomsg.nn_close(@socket)
      end

      protected

      def assert(rc)
        fail SocketError.new NNCore::LibNanomsg.nn_errno unless rc >= 0
      end
{'fraction_non_alphanumeric': 0.06914523974982627, 'fraction_numerical': 0.01320361362056984, 'mean_word_length': 2.1602634467618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2260486', 'n_tokens_mistral': 963, 'n_tokens_neox': 887, 'n_words': 193}