Tuesday, April 27, 2021

Experiment with Dart and Flutter


 

Decided to try out Dart and Flutter for Android development. Some thoughts after re-implementing AndSafe:

  • For simple / standard UI, defining and implementing each page with code is a lot easier than using UI designer
  • If you know JS / node.js, writing Dart code feel like home
  • Although Dart utilizes ahead-of-time compiled code, doing CPU and memory heavy tasks (e.g. scrypt key derivation) is still noticeably slow. Ended up using Dart FFI to call C implementation of scrypt.

I first implemented Android Safe 10 years ago in 2010. This re-write is certainly more enjoyable and easy, thanks to matured development environment.

Wednesday, March 17, 2021

Limit cloudflared upstream connections

Just archived my cloudflared patch repo. My patch was a hack to get around a run-away issue: when there are sudden in-rush of requests or network delay, cloudflared will create lots of connection to upstream DNS-over-HTTPS servers. This will trigger the upstream throttling cloudflared and causing it to create even more connections to upstream. The machine will ended up with high CPU usage and no DNS request being resolved.

That is because cloudflared used golang "http.Transport" for the connection without setting a max limit. My hack hard-coded the max number of connection to 2 to avoid the issue. But it is probably inappropriate if cloudflared is used in an enterprise environment.

Luckily someone worked on a fix by adding a command line parameter to specify the max connection. Just add "--max-upstream-conns num_con" as parameter when starting cloudflared.

Sunday, February 28, 2021

Penney's Game

 Just finished the book Humble Pi. Interesting read, especially if you are interested in math trivia and read about how little math mistakes can cause serious disasters.

In the chapter about probability, it mentioned the Penney's Game and how people's misconception about independent event makes the game interesting.

I ended up writing a program that simulate coin flipping to verify the claims. The result checked out but I did make a mistake when first implementing the logic.

When looping the coin flipping result, I used counter to keep track of the number of matches for each player. When a mismatch happens, I incorrectly reset the counter to zero. Instead, I should have checked if the last one / two coin flipping results match player's pattern.


Tuesday, January 26, 2021

Stable matching

While cleaning my working directory, found a Python function that I wrote a few months ago for solving the stable marriage problem. But couldn't remember why I wrote it...

Anyway, refactored and pushed to github.