<commit_before>-module(socket_util).

-export([
    acceptor_start/1,
    acceptor_loop/1
]).

-export([
    receiver_start/1,
    receiver_loop/1
]).

-include("socket_rec.hrl").

acceptor_start(Socket) when is_port(Socket) ->
    acceptor_start(#s_handler{socket = Socket, owner = self()});

acceptor_start(#s_handler{} = Handler) ->
    proc_lib:spawn_link(?MODULE, acceptor_loop, [Handler]).

acceptor_loop(#s_handler{} = Handler) ->
    case gen_tcp:accept(Handler#s_handler.socket) of
        {ok, Socket} ->
            Handler#s_handler.owner ! {acceptor, {socket, Socket}},
            acceptor_loop(Handler);

        {error, Reason} ->
            Handler#s_handler.owner ! {acceptor, {error, Reason}}
    end.

receiver_start(Socket) when is_port(Socket) ->
    receiver_start(#s_handler{socket = Socket, owner = self()});

receiver_start(#s_handler{} = Handler) ->
    proc_lib:spawn_link(?MODULE, receiver_loop, [Handler]).

receiver_loop(#s_handler{} = Handler) ->
    case gen_tcp:recv(Handler#s_handler.socket, 0) of
        {ok, Payload} ->
            Handler#s_handler.owner ! {receiver, {payload, Payload}},
            receiver_loop(Handler);

        {error, Reason} ->
            Handler#s_handler.owner ! {receiver, {error, Reason}}
    end.
<commit_msg>Make socket utility processes tail recursive<commit_after>-module(socket_util).

-export([
    acceptor_start/1,
    acceptor_loop/1
]).

-export([
    receiver_start/1,
    receiver_loop/1
]).

-include("socket_rec.hrl").

acceptor_start(Socket) when is_port(Socket) ->
    acceptor_start(#s_handler{socket = Socket, owner = self()});

acceptor_start(#s_handler{} = Handler) ->
    proc_lib:spawn_link(?MODULE, acceptor_loop, [Handler]).

acceptor_loop(#s_handler{} = Handler) ->
    case gen_tcp:accept(Handler#s_handler.socket) of
        {ok, Socket} ->
            Handler#s_handler.owner ! {acceptor, {socket, Socket}};

        {error, Reason} ->
            Handler#s_handler.owner ! {acceptor, {error, Reason}}
    end,
    acceptor_loop(Handler).

receiver_start(Socket) when is_port(Socket) ->
    receiver_start(#s_handler{socket = Socket, owner = self()});

receiver_start(#s_handler{} = Handler) ->
    proc_lib:spawn_link(?MODULE, receiver_loop, [Handler]).

receiver_loop(#s_handler{} = Handler) ->
    case gen_tcp:recv(Handler#s_handler.socket, 0) of
        {ok, Payload} ->
            Handler#s_handler.owner ! {receiver, {payload, Payload}};

        {error, Reason} ->
            Handler#s_handler.owner ! {receiver, {error, Reason}}
    end,
    receiver_loop(Handler).

{'fraction_non_alphanumeric': 0.1499029126213592, 'fraction_numerical': 0.003883495145631068, 'mean_word_length': 3.5034965034965033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '309973', 'n_tokens_mistral': 905, 'n_tokens_neox': 861, 'n_words': 147}