I finally got around to reading "How does Sidekiq really work?" by Dan Svetlov dansvetlov.me/sidekiq-inte.... Dang, it did not disappoint. What an enjoyable and in-depth read! A few things that stuck out to me 1/11 🧵
Post
1) @mike.contribsys.com is a exceedingly clever! 2) I've used Ruby for 12+ years now, and I learned new Hash methods from this article 3) The design of Ruby thread interrupts seem heavily influenced by linux signals 4) My next article "when good threads go bad" will pair nicely with this! 2/11 🧵
1) @mike.contribsys.com you clever devil, that Signal.trap approach is ingenious! In my post on thread interrupts, I mentioned that you cannot use rails.logger inside of a signal.trap: bit.ly/43c23Ym. Similarly, you cannot use the Sidekiq logger in trap, which is inconvenient. 3/11 🧵
The full reasons for that are explained in docs.ruby-lang.org/en/3.4/signa..., and have to do with a concept called "reentrancy". But mike bypasses all of that by using using IO.pipe. The io writer is safe to write to in the trap, so that's all it does, it writes the signal. 4/11 🧵
Meanwhile, the main loop for sidekiq is always waiting on the io reader, calling a blocking method, `wait_readable` in the while loop. Signal received -> write the signal -> wait_readable returns truthy -> while loop runs and calls `handle_signal` with the signal read 5/11 🧵
0 reposts1 like
You can change the default return value of a hash by using `default=` on the hash. docs.ruby-lang.org/en/3.4/Hash..... Now instead of nil, you'll return whatever `default` you provided! Very handy. 7/11 🧵
Zeitwerk uses it too to have namespaces as hash keys. Classes and modules are unique objects, and you cannot assume they implement the `hash` method as Hash needs it: github.com/fxn/zeitwerk...