<commit_before># Copyright (C) 2008-2009, <NAME>.

package Mojo::Server::Daemon;

use strict;
use warnings;

use base 'Mojo::Server';

use Carp 'croak';
use IO::Select;
use IO::Socket;
use Mojo::Pipeline;

__PACKAGE__->attr(keep_alive_timeout => (chained => 1, default => 15));
__PACKAGE__->attr(listen_queue_size  => (chained => 1, default => SOMAXCONN));
__PACKAGE__->attr(max_clients        => (chained => 1, default => 1000));
__PACKAGE__->attr(max_keep_alive_requests => (chained => 1, default => 100));
__PACKAGE__->attr(port                    => (chained => 1, default => 3000));

sub accept_lock { return 1 }

sub accept_unlock { return 1 }

sub listen {
    my $self = shift;

    # Create socket
    my $port = $self->port;
    $self->{listen} ||= IO::Socket::INET->new(
        Listen    => $self->listen_queue_size,
        LocalPort => $port,
        Proto     => 'tcp',
        ReuseAddr => 1,
        Type      => SOCK_STREAM
    ) or croak "Can't create listen socket: $!";

    # Non blocking
    $self->{listen}->blocking(0);

    $self->app->log->info("Server started (http://127.0.0.1:$port)");

    # Friendly message
    print "Server available at http://127.0.0.1:$port.\n";
}

# 40 dollars!? This better be the best damn beer ever..
# *drinks beer* You got lucky.
sub run {
    my $self = shift;

    $SIG{HUP} = $SIG{PIPE} = 'IGNORE';

    # Listen
    $self->listen;

    # Spin
    $self->spin while 1;
}

sub spin {
    my $self = shift;

    $self->_prepare_connections;
    $self->_prepare_transactions;
    my ($reader, $writer) = $self->_prepare_select;

    # Select
    my ($read, $write, undef) =
      IO::Select->select($reader, $writer, undef, 5);
    $read  ||= [];
    $write ||= [];

    # Make a random decision about reading or writing
    my $do = -1;
    $do = 0 if @$read;
    $do = 1 if @$write;
    $do = int(rand(3)) - 1 if @$read && @$write;

    # Read
    if ($do == 0) { $self->_read($read) }

    # Write
    elsif ($do == 1) { $self->_write($write) }

}

sub _drop_connection {
    my ($self, $name) = @_;
    delete $self->{_reverse}->{$self->{_connections}->{$name}};
    delete $self->{_connections}->{$name};
}

sub _prepare_connections {
    my $self = shift;

    $self->{_accepted}    ||= [];
    $self->{_connections} ||= {};

    # Accept
    my @accepted = ();
    for my $accept (@{$self->{_accepted}}) {

        # Not yet connected
        unless ($accept->{socket}->connected) {
            push @accepted, $accept;
            next;
        }

        # Connected
        $accept->{socket}->blocking(0);
        next unless my $name = $self->_socket_name($accept->{socket});
        $self->{_reverse}->{$accept->{socket}} = $name;
        $self->{_connections}->{$name} = $accept;
    }
    $self->{_accepted} = [@accepted];
}

sub _prepare_select {<commit_msg>Really close connections when we drop them<commit_after># Copyright (C) 2008-2009, <NAME>.

package Mojo::Server::Daemon;

use strict;
use warnings;

use base 'Mojo::Server';

use Carp 'croak';
use IO::Select;
use IO::Socket;
use Mojo::Pipeline;

__PACKAGE__->attr(keep_alive_timeout => (chained => 1, default => 15));
__PACKAGE__->attr(listen_queue_size  => (chained => 1, default => SOMAXCONN));
__PACKAGE__->attr(max_clients        => (chained => 1, default => 1000));
__PACKAGE__->attr(max_keep_alive_requests => (chained => 1, default => 100));
__PACKAGE__->attr(port                    => (chained => 1, default => 3000));

sub accept_lock { return 1 }

sub accept_unlock { return 1 }

sub listen {
    my $self = shift;

    # Create socket
    my $port = $self->port;
    $self->{listen} ||= IO::Socket::INET->new(
        Listen    => $self->listen_queue_size,
        LocalPort => $port,
        Proto     => 'tcp',
        ReuseAddr => 1,
        Type      => SOCK_STREAM
    ) or croak "Can't create listen socket: $!";

    # Non blocking
    $self->{listen}->blocking(0);

    $self->app->log->info("Server started (http://127.0.0.1:$port)");

    # Friendly message
    print "Server available at http://127.0.0.1:$port.\n";
}

# 40 dollars!? This better be the best damn beer ever..
# *drinks beer* You got lucky.
sub run {
    my $self = shift;

    $SIG{HUP} = $SIG{PIPE} = 'IGNORE';

    # Listen
    $self->listen;

    # Spin
    $self->spin while 1;
}

sub spin {
    my $self = shift;

    $self->_prepare_connections;
    $self->_prepare_transactions;
    my ($reader, $writer) = $self->_prepare_select;

    # Select
    my ($read, $write, undef) =
      IO::Select->select($reader, $writer, undef, 5);
    $read  ||= [];
    $write ||= [];

    # Make a random decision about reading or writing
    my $do = -1;
    $do = 0 if @$read;
    $do = 1 if @$write;
    $do = int(rand(3)) - 1 if @$read && @$write;

    # Read
    if ($do == 0) { $self->_read($read) }

    # Write
    elsif ($do == 1) { $self->_write($write) }

}

sub _drop_connection {
    my ($self, $name) = @_;
    close $self->{_connections}->{$name}->{socket};
    delete $self->{_reverse}->{$self->{_connections}->{$name}};
    delete $self->{_connections}->{$name};
}

sub _prepare_connections {
    my $self = shift;

    $self->{_accepted}    ||= [];
    $self->{_connections} ||= {};

    # Accept
    my @accepted = ();
    for my $accept (@{$self->{_accepted}}) {

        # Not yet connected
        unless ($accept->{socket}->connected) {
            push @accepted, $accept;
            next;
        }

        # Connected
        $accept->{socket}->blocking(0);
        next unless my $name = $self->_socket_name($accept->{socket});
        $self->{_reverse}->{$accept->{socket}} = $name;
        $self->{_connections}->{$name} = $accept;
    }
    $self->{_accepted} = [@accepted];
}

sub _prepare_select {
{'fraction_non_alphanumeric': 0.18677110530896432, 'fraction_numerical': 0.018798955613577025, 'mean_word_length': 2.7070967741935483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 134, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6479310', 'n_tokens_mistral': 2113, 'n_tokens_neox': 1962, 'n_words': 468}