Solved: Mojolicious Websockets Inactivity timeout

A recent snippet of Mojolicious webapp I recently wrote would not allow browsers to connect and upgrade to websockets. Below you'll find the code and explanation so hopefully it'll save you some debugging time. Bug Symptoms Connecting to the websocket endpoint gave me the following error in the browser console: WebSocket connection to 'ws://localhost:3000/feed/news' failed: Connection closed before receiving a handshake response (anonymous) @ update:18 And at the same time in the server console: [debug] Inactivity timeout So it looks like the

Read more

Hello Elixir World

After spending too long trying to love lisp, I think I'm finally starting to understand the beauty in functional languages - and elixir is to blame. It's very different than most languages I know, but in an almost magical way. Here's my very first elixir program, which is a terminal based tic-tac-toe game. It's probably not that great elixir example regarding best practices, but I hope it'll show some of the magic I find in this language. A Finished Game The tic-tac-toe game should read a

Read more

Top 3 Unit Tests Causes of Death

One challenge in writing unit tests is making them survive the test of time. Sooner or later your code will change and if your tests aren't resilient they'll break. That usually happens right before a deadline so evrybody's too busy to fix the tests, and as life goes those tests could continue to fail for quite some time. Following a list of 3 major unit tests causes of death and some tips to avoiding them. Reason 1: Special Privileges As both code and its unit test

Read more

Still Slow After Fork

A simple paradigm for speeding up code is to use multiple processes created by fork. Unfortunately this doesn't always work as we can see in the following example. Calculating Tons of MD5 A simple case would be brute forcing MD5. Using Math::Cartesian::Product it's easy to generate all the 5 letter combinations and calculate an MD5 sum from each of them. If we were searching for the sources of the following MD5s: 5d41402abc4b2a76b9719d911017c592 3899dcbab79f92af727c2190bbd8abc5 9aeaed51f2b0f6680c4ed4b07fb1a83c 96e25e3c6373262faae824dc5a16cce1 We could use the following perl program to brute

Read more

Fixing Legacy Perl Functions With Decorators

Function decorators give us a way to modify a function's behaviour without changing its source. This is useful in changing the behaviour of complex or legacy functions that you don't want to touch. Although perl does not have a built-in syntax for creating or using decorators, typeglob manipulations are used to easily fill the gap. Reasons To Decorate Take the following implementation for fibonacci calculating function: sub fib { my ($n) = @_; print "DEBUG - calculating fib($n)\n"; $n < 2 ? 1 : fib($n-1) + fib(

Read more