Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MOAR process ballooning while running Perl6 socket server

I have a socket server using IO::Socket::Async and Redis::Async for message publishing. Whenever there is a message received by the server, the script would translate the message and generate acknowledge message to be sent back to the sender so that the sender would send subsequent messages. Since translating the message is quite expensive, the script would run that portion under a 'start' method. However, I noticed that the Moar process eating my RAM as the script is running. Any thought where should I look to solve this issue? Thanks!

https://pastebin.com/ySsQsMFH

use v6;
use Data::Dump;
use experimental :pack;
use JSON::Tiny;
use Redis::Async;

constant $SOCKET_PORT = 7000;
constant $SOCKET_ADDR = '0.0.0.0';
constant $REDIS_PORT = 6379;
constant $REDIS_ADDR = '127.0.0.1';
constant $REDIS_AUTH = 'xxxxxxxx';


constant $IDLING_PERIOD_MIN = 180 - 2; # 3 minutes - 2 secs
constant $CACHE_EXPIRE_IN = 86400; # 24h hours

# create socket
my $socket = IO::Socket::Async.listen($SOCKET_ADDR, $SOCKET_PORT);

# connnect to Redis ...
my $redis;
try {
    my $error-code = "110";
    $redis = Redis::Async.new("$SOCKET_ADDR:$SOCKET_PORT");
    $redis.auth($REDIS_AUTH);

    CATCH {
        default {
            say "Error $error-code ", .^name, ': Failed to initiate connection to Redis';
            exit;
        }
    }
}

# react whenever there is connection
react {
    whenever $socket -> $conn {

        # do something when the connection wants to talk
        whenever $conn.Supply(:bin) {
            # only process if data length is either 108 or 116
            if $_.decode('utf-8').chars == 108 or $_.decode('utf-8').chars == 116 {
                say "Received --> "~$_.decode('utf-8');
                my $ack = generateAck($_.decode('utf-8'));  # generate ack based on received data
                if $ack {
                    $conn.print: $ack;
                }else{
                    say "No ack. Received data maybe corrupted. Closing connection";
                    $conn.close;
                }

            }
        }
    }
    CATCH {
        default {
            say .^name, ': ', .Str;
            say "handled in $?LINE";
        }
    }
}

### other subroutines down here ###
like image 765
Zarul Zakuan Avatar asked Nov 06 '22 23:11

Zarul Zakuan


1 Answers

The issue was using the Async::Redis. Jonathon Stowe had fixed the Redis module so I'm using Redis module with no issue.

like image 162
Zarul Zakuan Avatar answered Nov 15 '22 11:11

Zarul Zakuan