{"@attributes":{"version":"2.0"},"channel":{"title":"Previnder","link":"https:\/\/previnder.com\/","description":"Essays and notes by Previnder","language":"en","lastBuildDate":"Mon, 20 Oct 2025 13:35:18 +0000","item":[{"title":"How to test local area network speed","link":"https:\/\/previnder.com\/test-local-network-speed","guid":"https:\/\/previnder.com\/test-local-network-speed","pubDate":"Mon, 20 Oct 2025 13:35:18 +0000","description":"<p>To test the link speed between two machines on a local network, a method that is often used is to send a file from one machine to the other using a network file sharing protocol such as <a href=\"https:\/\/wiki.archlinux.org\/title\/Samba\">SMB<\/a>, <a href=\"https:\/\/wiki.archlinux.org\/title\/NFS\">NFS<\/a>, <a href=\"https:\/\/wiki.archlinux.org\/title\/SSHFS\">SSHFS<\/a>, or <a href=\"https:\/\/wiki.archlinux.org\/title\/Rsync\">rsync over SSH<\/a>.<\/p>\n<p>Unless one of those protocols is already set up on the two computers, this is a very cumbersome way to test network speeds between them. And if the network is faster than one of the storage devices of the two computers involved, either the one from which the file is read or the one to which the file is written (or both), this method would not test the maximum network speed possible. This can easily happen if one of those storage devices happens to be an old fashioned hard-disk.<\/p>\n<p>This is where the simple networking tool <code>iperf<\/code> comes in. It\u2019s a small program that makes testing the link speed between two computers exceedingly easy and straightforward.<\/p>\n<p>To test the network speed between two computers, on one computer start <code>iperf<\/code> as a server:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\"><span class=\"go\">iperf -s\n<\/span><\/span><\/span><\/code><\/pre><p>And on the other computer, run <code>iperf<\/code> as a client, giving it the IP address of the computer on which <code>iperf<\/code> is running as a server:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\"><span class=\"go\">iperf -c 10.0.0.20\n<\/span><\/span><\/span><\/code><\/pre><p>This command, by default, will take 10 seconds to complete, and its output should look something like the following:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\"><span class=\"gp\">$<\/span> iperf -c 10.0.0.20\n<\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"go\">------------------------------------------------------------\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"go\">Client connecting to 10.0.0.20, TCP port 5001\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"go\">TCP window size: 16.0 KByte (default)\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"go\">------------------------------------------------------------\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"go\">[  1] local 10.0.0.10 port 53928 connected with 10.0.0.20 port 5001\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"go\">[ ID] Interval       Transfer     Bandwidth\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"go\">[  1] 0.0000-10.0201 sec  1.08 GBytes   924 Mbits\/sec\n<\/span><\/span><\/span><\/code><\/pre><p>In my case, I\u2019m using 1 Gbps ethernet to connect the computers on my local area network, and the network seems to be working fine at full-speed, given that the test gives a network speed of 924 Mbits\/sec.<\/p>\n<p>Of course, <code>iperf<\/code> can be used not only on local area networks but between any two computers that are reachable from one another.<\/p>\n"},{"title":"Ext4 reserves five-percent of the filesystem's available space by default","link":"https:\/\/previnder.com\/ext4-reserved-blocks","guid":"https:\/\/previnder.com\/ext4-reserved-blocks","pubDate":"Tue, 14 Oct 2025 13:34:48 +0000","description":"<p>By default, Ext4 reserves 5% of the filesystem\u2019s block count for the super-user, so that root processes can continue to run without problem even if other non-privileged processes are prevented from writing to the filesystem when it\u2019s full.<\/p>\n<p>This makes perfect sense for the root partition where the operating system is installed, but for most other partitions, reserving 5% of the disk space for the super-user is probably a waste of storage space.<\/p>\n<p>For a 1 TB partition, assuming its size is exactly 1024 GB, 5% is 51.2 GB; and for a 4 TB partition, it\u2019s 204 GB. That is a lot of wasted disk space.<\/p>\n<p>To prevent reserving blocks upon filesystem creation, use the <code>m<\/code> option to set it to 0%:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\"> mkfs.ext4 -m <span class=\"m\">0<\/span> \/dev\/device\n<\/span><\/span><\/code><\/pre><p>To change the reserved block count percentage of an Ext4 filesystem after it\u2019s been created, use:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">tune2fs -m <span class=\"m\">0<\/span> \/dev\/device\n<\/span><\/span><\/code><\/pre><p>To find out the reserved block count of an Ext4 filesystem, use:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">tune2fs -l \/dev\/device <span class=\"p\">|<\/span> grep <span class=\"s1\">&#39;Reserved block count:&#39;<\/span>\n<\/span><\/span><\/code><\/pre><p>To convert the output of the above into bytes, multiply it with the output of the following command:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">tune2fs -l \/dev\/device <span class=\"p\">|<\/span> grep <span class=\"s1\">&#39;Block size:&#39;<\/span>\n<\/span><\/span><\/code><\/pre><p>However, if one repeatedly writes to the filesystem when it\u2019s almost full, setting the reserved block count to zero will hinder performance due to fragmentation. In that case, it\u2019s best to either leave the reserved block count as it is or set it to a lower percentage.<\/p>\n<p>If, on the other hand, the Ext4 partition is used for storing user-files such as videos, images, and so on, in which case the partition is used almost as an archival storage device, then it\u2019s probably a good idea to set the reserved block count of the partition to zero.<\/p>\n"},{"title":"Storing IP addresses in MariaDB","link":"https:\/\/previnder.com\/storing-ips-in-mariadb","guid":"https:\/\/previnder.com\/storing-ips-in-mariadb","pubDate":"Wed, 16 Jul 2025 13:33:28 +0000","description":"<p>What\u2019s the best datatype to store IP addresses as in MariaDB?<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1<\/a><\/sup> It\u2019s 2025 and, according to Google, <a href=\"https:\/\/www.google.com\/intl\/en\/ipv6\/statistics.html\">nearly half the internet now supports IPv6<\/a>. So whichever data type is the best, it will have to support both IPv4 and IPv6 addresses.<\/p>\n<p>There are, in that case, three main options:<\/p>\n<ol>\n<li><code>VARBINARY(16)<\/code>: With this option you could store IPv6 addresses as 16-byte byte-strings and IPv4 addresses as 4-byte byte-strings; or you could store all IP addresses as 16-byte byte-strings by converting all IPv4 addresses to IPv4-mapped IPv6 addresses first.<sup id=\"fnref:2\"><a href=\"#fn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2<\/a><\/sup> If you go with the latter option, then of course the data type could simply be <code>BINARY(16)<\/code> instead of <code>VARBINARY(16)<\/code>, making the type a little bit simpler and saving you a byte per each row.<sup id=\"fnref:3\"><a href=\"#fn:3\" class=\"footnote-ref\" role=\"doc-noteref\">3<\/a><\/sup><\/li>\n<li><code>VARCHAR(45)<\/code>: This is the simplest option. You just store the IP addresses in their textual representation. Like in the previous option, you could convert IPv4 addresses to IPv4-mapped IPv6 addresses first, but what would be the point?<\/li>\n<li><code>INET6<\/code><sup id=\"fnref:4\"><a href=\"#fn:4\" class=\"footnote-ref\" role=\"doc-noteref\">4<\/a><\/sup>: This is of course the data-type that MariaDB provides for storing IPv6 addresses, as well as IPv4 addresses mapped into IPv6 addresses. Internally, all <code>INET6<\/code> values are stored as 16-byte fixed length byte-strings, and clients interacting with MariaDB see them in their textual representation.<\/li>\n<\/ol>\n<p>As you might expect, if there\u2019s nothing special that needs to be taken into consideration, <code>INET6<\/code> is the best option for storing IP addresses in MariaDB, for several reasons.<\/p>\n<p>The first is that, since <code>INET6<\/code> values are stored as fixed-length binary-strings, they can be compared with one another, most importantly to select rows between two IP addresses. This also works with the first option above, but not with the second one.<\/p>\n<p>The second reason is that unlike with the <code>VARBINARY(16)<\/code> option, clients interact with <code>INET6<\/code> as if it\u2019s a string type, which makes the use of <code>INET6<\/code> much simpler. The third is that with <code>INET6<\/code> MariaDB makes sure that you\u2019re always inserting correct IP address values.<\/p>\n<p>Here are some example SQL queries dealing with <code>INET6<\/code>:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\"><span class=\"k\">CREATE<\/span><span class=\"w\"> <\/span><span class=\"k\">TABLE<\/span><span class=\"w\"> <\/span><span class=\"n\">t1<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"n\">a<\/span><span class=\"w\"> <\/span><span class=\"n\">INET6<\/span><span class=\"p\">);<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"k\">INSERT<\/span><span class=\"w\"> <\/span><span class=\"k\">INTO<\/span><span class=\"w\"> <\/span><span class=\"n\">t1<\/span><span class=\"w\"> <\/span><span class=\"k\">VALUES<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"s1\">&#39;2001:db8::ff00:42:8329&#39;<\/span><span class=\"p\">);<\/span><span class=\"w\"> <\/span><span class=\"c1\">-- IPv6 address\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"k\">INSERT<\/span><span class=\"w\"> <\/span><span class=\"k\">INTO<\/span><span class=\"w\"> <\/span><span class=\"n\">t1<\/span><span class=\"w\"> <\/span><span class=\"k\">VALUES<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"s1\">&#39;::ffff:192.0.2.128&#39;<\/span><span class=\"p\">);<\/span><span class=\"w\"> <\/span><span class=\"c1\">-- IPv4-mapped IPv6 address\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"k\">INSERT<\/span><span class=\"w\"> <\/span><span class=\"k\">INTO<\/span><span class=\"w\"> <\/span><span class=\"n\">t1<\/span><span class=\"w\"> <\/span><span class=\"k\">VALUES<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"s1\">&#39;::192.0.2.128&#39;<\/span><span class=\"p\">);<\/span><span class=\"w\"> <\/span><span class=\"c1\">-- IPv4-compatible IPv6 address \n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"k\">INSERT<\/span><span class=\"w\"> <\/span><span class=\"k\">INTO<\/span><span class=\"w\"> <\/span><span class=\"n\">t1<\/span><span class=\"w\"> <\/span><span class=\"k\">VALUES<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"s1\">&#39;192.0.2.128&#39;<\/span><span class=\"p\">);<\/span><span class=\"w\"> <\/span><span class=\"c1\">-- IPv4-mapped IPv6 address \n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"k\">SELECT<\/span><span class=\"w\"> <\/span><span class=\"o\">*<\/span><span class=\"w\"> <\/span><span class=\"k\">FROM<\/span><span class=\"w\"> <\/span><span class=\"n\">t1<\/span><span class=\"p\">;<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">+<\/span><span class=\"c1\">------------------------+\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"n\">a<\/span><span class=\"w\">                      <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">+<\/span><span class=\"c1\">------------------------+\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"mi\">2001<\/span><span class=\"p\">:<\/span><span class=\"n\">db8<\/span><span class=\"p\">::<\/span><span class=\"n\">ff00<\/span><span class=\"p\">:<\/span><span class=\"mi\">42<\/span><span class=\"p\">:<\/span><span class=\"mi\">8329<\/span><span class=\"w\"> <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"mi\">2001<\/span><span class=\"p\">:<\/span><span class=\"n\">db8<\/span><span class=\"p\">::<\/span><span class=\"n\">ff00<\/span><span class=\"p\">:<\/span><span class=\"mi\">42<\/span><span class=\"p\">:<\/span><span class=\"mi\">8329<\/span><span class=\"w\"> <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"mi\">2001<\/span><span class=\"p\">:<\/span><span class=\"n\">db8<\/span><span class=\"p\">::<\/span><span class=\"n\">ff00<\/span><span class=\"p\">:<\/span><span class=\"mi\">42<\/span><span class=\"p\">:<\/span><span class=\"mi\">8329<\/span><span class=\"w\"> <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"mi\">2001<\/span><span class=\"p\">:<\/span><span class=\"n\">db8<\/span><span class=\"p\">::<\/span><span class=\"n\">ff00<\/span><span class=\"p\">:<\/span><span class=\"mi\">42<\/span><span class=\"p\">:<\/span><span class=\"mi\">8329<\/span><span class=\"w\"> <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"p\">::<\/span><span class=\"n\">ffff<\/span><span class=\"p\">:<\/span><span class=\"mi\">192<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">2<\/span><span class=\"p\">.<\/span><span class=\"mi\">128<\/span><span class=\"w\">     <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"p\">::<\/span><span class=\"mi\">192<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">2<\/span><span class=\"p\">.<\/span><span class=\"mi\">128<\/span><span class=\"w\">          <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">+<\/span><span class=\"c1\">------------------------+\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"k\">INSERT<\/span><span class=\"w\"> <\/span><span class=\"k\">INTO<\/span><span class=\"w\"> <\/span><span class=\"n\">t1<\/span><span class=\"w\"> <\/span><span class=\"k\">VALUES<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"s1\">&#39;192.0.2.192&#39;<\/span><span class=\"p\">);<\/span><span class=\"w\"> <\/span><span class=\"c1\">-- IPv4-mapped IPv6 address \n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"k\">INSERT<\/span><span class=\"w\"> <\/span><span class=\"k\">INTO<\/span><span class=\"w\"> <\/span><span class=\"n\">t1<\/span><span class=\"w\"> <\/span><span class=\"k\">VALUES<\/span><span class=\"w\"> <\/span><span class=\"p\">(<\/span><span class=\"s1\">&#39;10.0.2.0&#39;<\/span><span class=\"p\">);<\/span><span class=\"w\"> <\/span><span class=\"c1\">-- IPv4-mapped IPv6 address \n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"c1\">-- Select all rows between two IP addresses\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"k\">SELECT<\/span><span class=\"w\"> <\/span><span class=\"o\">*<\/span><span class=\"w\"> <\/span><span class=\"k\">FROM<\/span><span class=\"w\"> <\/span><span class=\"n\">t1<\/span><span class=\"w\"> <\/span><span class=\"k\">WHERE<\/span><span class=\"w\"> <\/span><span class=\"n\">a<\/span><span class=\"w\"> <\/span><span class=\"o\">&gt;<\/span><span class=\"w\"> <\/span><span class=\"s1\">&#39;192.0.2.0&#39;<\/span><span class=\"w\"> <\/span><span class=\"k\">AND<\/span><span class=\"w\"> <\/span><span class=\"n\">a<\/span><span class=\"w\"> <\/span><span class=\"o\">&lt;<\/span><span class=\"w\"> <\/span><span class=\"s1\">&#39;192.0.2.255&#39;<\/span><span class=\"p\">;<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">+<\/span><span class=\"c1\">--------------------+\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"n\">a<\/span><span class=\"w\">                  <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">+<\/span><span class=\"c1\">--------------------+\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"p\">::<\/span><span class=\"n\">ffff<\/span><span class=\"p\">:<\/span><span class=\"mi\">192<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">2<\/span><span class=\"p\">.<\/span><span class=\"mi\">128<\/span><span class=\"w\"> <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"p\">::<\/span><span class=\"n\">ffff<\/span><span class=\"p\">:<\/span><span class=\"mi\">192<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">2<\/span><span class=\"p\">.<\/span><span class=\"mi\">128<\/span><span class=\"w\"> <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">|<\/span><span class=\"w\"> <\/span><span class=\"p\">::<\/span><span class=\"n\">ffff<\/span><span class=\"p\">:<\/span><span class=\"mi\">192<\/span><span class=\"p\">.<\/span><span class=\"mi\">0<\/span><span class=\"p\">.<\/span><span class=\"mi\">2<\/span><span class=\"p\">.<\/span><span class=\"mi\">192<\/span><span class=\"w\"> <\/span><span class=\"o\">|<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"o\">+<\/span><span class=\"c1\">--------------------+\n<\/span><\/span><\/span><\/code><\/pre><p>In the last insert statement of the this snippet, an IPv4 address is inserted into an <code>INET6<\/code> column as is; MariaDB, since version 11.3<sup id=\"fnref:5\"><a href=\"#fn:5\" class=\"footnote-ref\" role=\"doc-noteref\">5<\/a><\/sup>, automatically converts these into IPv4-mapped IPv6 addresses.<\/p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"fn:1\">\n<p>Or MySQL, if for some reason you prefer a database from Oracle.&#160;<a href=\"#fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;<\/a><\/p>\n<\/li>\n<li id=\"fn:2\">\n<p>Technically, you also have the option of storing IPv4 addresses as IPv4-compatible IPv6 addresses, but those are now deprecated (and apparently they\u2019ve been <a href=\"https:\/\/www.rfc-editor.org\/rfc\/rfc4291.html\">deprecated since 2006<\/a>).&#160;<a href=\"#fnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;<\/a><\/p>\n<\/li>\n<li id=\"fn:3\">\n<p>Because of course <code>VARCHAR<\/code> and <code>VARBINARY<\/code> types need to store the length of the string alongside it.&#160;<a href=\"#fnref:3\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;<\/a><\/p>\n<\/li>\n<li id=\"fn:4\">\n<p>Consult the <a href=\"https:\/\/mariadb.com\/docs\/server\/reference\/data-types\/string-data-types\/inet6\">documentation here.<\/a>&#160;<a href=\"#fnref:4\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;<\/a><\/p>\n<\/li>\n<li id=\"fn:5\">\n<p>I had to learn the hard way, while performing a migration on a production database, that this doesn\u2019t work on older versions of MariaDB.&#160;<a href=\"#fnref:5\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>\n"},{"title":"Make KDE Plasma\u2019s file indexer, Baloo, usable","link":"https:\/\/previnder.com\/make-baloo-usable","guid":"https:\/\/previnder.com\/make-baloo-usable","pubDate":"Mon, 28 Apr 2025 13:32:27 +0000","description":"<p>KDE Plasma is great, but the file indexer that ships with it, Baloo, is not. In fact, it\u2019s terrible. If I leave it as it is, one of its processes, for some reason, inevitably starts using 100% CPU, which I only notice once my laptop starts heating up. You\u2019d think this only happens for a short period of time when a lot of new files are waiting to be indexed at once, but that doesn\u2019t seem to be the case, from what I can tell.<\/p>\n<p>I\u2019ve found that there\u2019s a way to <em>kind-of<\/em> fix this, by making only the most minimal use of the file indexer. But each time I install KDE Plasma, in these many years I\u2019ve been using it, I always forget to configure that, which is why I\u2019m writing this note.<\/p>\n<p>The way to fix Baloo is to add the following lines to the <code>baloofilerc<\/code> file<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1<\/a><\/sup> (located in <code>$HOME\/.config<\/code>).<\/p>\n<pre><code class=\"language-conf\">[General]\nonly basic indexing=true\n<\/code><\/pre>\n<p>This instructs Baloo to only index filenames, which is good enough for me. This way, Baloo indexes all the files it\u2019s got to index extremely quickly, and the high CPU usage problem goes away completely.<\/p>\n<p>After adding those lines to the <code>baloofilerc<\/code> file, run <code>balooctl6 disable<\/code> and then <code>balooctl6 purge<\/code>, which will first disable the file indexer and then delete its database. If any Baloo processes are still running after that (run <code>ps aux | grep baloo<\/code> to check), kill them manually. Then restart Baloo by running <code>balooctl6 enable<\/code>.<\/p>\n<p>Run <code>balooctl6 status<\/code> to check the status of the file indexer. Baloo should trouble you no more now.<\/p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"fn:1\">\n<p>See the <a href=\"https:\/\/community.kde.org\/Baloo\/Configuration\">Baloo documentation page<\/a> for more infomation.&#160;<a href=\"#fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>\n"},{"title":"Migrating a Progressive Web App (PWA) to a new domain without breaking existing installations","link":"https:\/\/previnder.com\/pwa-domain-change","guid":"https:\/\/previnder.com\/pwa-domain-change","pubDate":"Sun, 09 Mar 2025 06:02:21 +0000","description":"<p>Recently, I migrated <a href=\"https:\/\/discuit.org\/\">Discuit<\/a>, which is a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Progressive_web_apps\">Progressive Web App (PWA)<\/a>, from <code>discuit.net<\/code> to <code>discuit.org<\/code>. Everything went smoothly and seamlessly, I\u2019m glad to say, without it messing up the existing user experience in any way. In fact, had I not made an <a href=\"https:\/\/discuit.org\/Discuit\/post\/FZadJa9b\">announcement about it<\/a>, most users wouldn\u2019t have even noticed the change.<\/p>\n<p>Migrating a website to a new domain is usually straightforward, at least for simple websites: a basic HTTP 301 \u201cMoved Permanently\u201d response is often enough to redirect traffic from the old domain to the new. But for PWAs, this approach can break existing installations. This is because a blanket 301 redirect would change the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Origin\">origin<\/a> of both the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Progressive_web_apps\/Manifest\">manifest file<\/a> and the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Service_Worker_API\/Using_Service_Workers\">service worker file<\/a>, both of which are required to be on the same origin they were first installed from.<\/p>\n<p>This means that once a PWA is installed, its domain cannot be changed. However, we have something of a solution if we:<\/p>\n<ol>\n<li>Keep the old domain working as it is for existing PWA users.<\/li>\n<li>Redirect only new users to the new domain.<\/li>\n<\/ol>\n<h2 id=\"identifying-requests-coming-from-a-pwa\">Identifying requests coming from a PWA<\/h2>\n<p>Where it gets tricky is, how do we know which HTTP requests are coming from a PWA and which ones are not? Unfortunately, there\u2019s no official, browser-supported, and fool-proof way of doing that.<\/p>\n<h3 id=\"using-a-custom-http-header-added-by-the-service-worker\">Using a custom HTTP header added by the service worker<\/h3>\n<p>Had I planned for precisely this kind of PWA domain migration from the beginning\u2014that is, from the first release of the PWA\u2014I could\u2019ve added a definitive way of identifying requests coming from a PWA.<\/p>\n<p>Since the service worker that a PWA includes is a proxy that sits between the application and the network, it can modify incoming and outgoing HTTP requests. And if I had written the service worker such that it modified all outgoing requests to include an HTTP header that says that the request is coming from a PWA, I would\u2019ve had a straightforward method for identifying the PWAs.<\/p>\n<p>However, since there is no reliable way of updating the service workers of all the previously installed PWAs at once, this method is useless, and I had to rely on other, less straightforward, methods.<\/p>\n<h3 id=\"using-cookies\">Using cookies<\/h3>\n<p>One of those methods was checking for the existence of cookies. Discuit sets a session cookie, named <code>SID<\/code>, when a user first visits the site, which has an expiration date a year from the time it\u2019s set. Almost all users who have visited the site in recent months would send this cookie along with all the HTTP requests to the server, making it a fairly reliable but still not foolproof way of identifying most of the recent PWA users.<\/p>\n<p>Of course, it will identify not just PWA users but also regular users who\u2019ve visited the site recently as well; however, for my purposes this was not at all an issue.<\/p>\n<h3 id=\"when-cookies-dont-exist\">When cookies don\u2019t exist<\/h3>\n<p>But what about someone who has not opened the PWA in more than a year since the session cookie was set? Or someone who\u2019s cleared the cookies for some reason in the meantime? It would be better if the domain change didn\u2019t break the PWA for those users as well. That could be made to work, firstly, by not redirecting both the service worker and manifest files regardless of any other factors<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1<\/a><\/sup>; and secondly, by allowing web pages at the old domain to access resources at the new domain using <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/CORS\">CORS<\/a>.<\/p>\n<p>For the latter, the server needs to return the following HTTP headers for incoming requests to the new domain, <code>discuit.org<\/code>:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">access-control-allow-credentials: <span class=\"nb\">true<\/span>\n<\/span><\/span><span class=\"line\"><span class=\"cl\">access-control-allow-methods: GET, POST, HEAD, OPTIONS\n<\/span><\/span><span class=\"line\"><span class=\"cl\">access-control-allow-origin: https:\/\/discuit.net\n<\/span><\/span><\/code><\/pre><h3 id=\"navigation-preload-requests\">Navigation preload requests<\/h3>\n<p>Then there is the matter of <a href=\"https:\/\/web.dev\/blog\/navigation-preload\">service worker navigation preloading<\/a>, a technique used in service workers to reduce initial page load time. We don\u2019t want to redirect these requests either. Fortunately for us, all service worker navigation preload requests include an HTTP header named <code>Service-Worker-Navigation-Preload<\/code>.<\/p>\n<h3 id=\"api-requests\">API requests<\/h3>\n<p>There is also no need, in any case, to redirect API requests. Since Discuit uses cookies for authentication, if the <code>SID<\/code> cookie is missing, redirecting these API requests that require authentication would result in an error.<\/p>\n<p>Moreover, if the <code>SID<\/code> cookie is expired, this is one more condition that would allow the re-setting of that cookie. And on Discuit, all API requests have the pathname prefix of <code>\/api\/<\/code>, which makes it trivial to identify them.<\/p>\n<h2 id=\"final-migration-strategy\">Final migration strategy<\/h2>\n<p>Now we have a fairly complete solution to our domain migration problem that doesn\u2019t break any of the existing PWA installations. As a bonus, it also doesn\u2019t require already logged in users to log back in on the new domain either; they could continue to use the old domain as it was.<\/p>\n<p>In summary, the complete solution is:<\/p>\n<ol>\n<li>HTTP 301 redirect all incoming requests for the old domain, <code>discuit.net<\/code>, to the new domain, <code>discuit.org<\/code>, except:\n<ol>\n<li>If the HTTP request has a path that points to either the manifest file or the service worker file.<\/li>\n<li>If the HTTP request includes a <code>Service-Worker-Navigation-Preload<\/code> header.<\/li>\n<li>If the HTTP request includes a cookie named <code>SID<\/code>.<\/li>\n<li>If the HTTP request has a path prefix of <code>\/api\/<\/code>.<\/li>\n<\/ol>\n<\/li>\n<li>Enable cross-origin access to the <code>discuit.org<\/code> domain, for the <code>discuit.net<\/code> domain, using CORS headers.<\/li>\n<\/ol>\n<p>To implement this, I wrote a reverse-proxy in Go that sits between the web-server and the Internet. It\u2019s available <a href=\"https:\/\/github.com\/discuitnet\/rip\">here on Github<\/a>.<\/p>\n<h2 id=\"future-proofing\">Future proofing<\/h2>\n<p>After all this is done, I updated the service worker to modify all outgoing HTTP requests to include an HTTP header, named <code>X-Service-Worker-Version<\/code><sup id=\"fnref:2\"><a href=\"#fn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2<\/a><\/sup>, in any case, which might come in handy in the future, perhaps for a different use case.<\/p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"fn:1\">\n<p>For the manifest file, this is necessary anyway, because requests to the manifest file are performed without credentials\u2014so the <code>SID<\/code> cookie won\u2019t be sent along with it\u2014unless the <code>&lt;link&gt;<\/code> tag that points to the manifest includes the attribute <code>crossorigin=&quot;use-credentials&quot;<\/code>.&#160;<a href=\"#fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;<\/a><\/p>\n<\/li>\n<li id=\"fn:2\">\n<p>You have to make sure to include this header only in same-origin requests. Otherwise, CORS will block them.&#160;<a href=\"#fnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>\n"},{"title":"Setting up a trusted, self-signed SSL\/TLS certificate authority in Linux","link":"https:\/\/previnder.com\/tls-ca-linux","guid":"https:\/\/previnder.com\/tls-ca-linux","pubDate":"Mon, 17 Feb 2025 06:01:10 +0000","description":"<p>With OpenSSL, it\u2019s pretty easy to generate a simple <a href=\"https:\/\/en.wikipedia.org\/wiki\/Self-signed_certificate\">self-signed TLS certificate<\/a>. Just run the following command:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">openssl req -x509 -newkey rsa:4096 -sha256 -days <span class=\"m\">365<\/span> -nodes -keyout cert.key -out cert.crt\n<\/span><\/span><\/code><\/pre><p>The files that this command generates, <code>cert.key<\/code> and <code>cert.crt<\/code>, could be passed to a web server, for example, and it will work fine; that is, all the connections made to that web server will be properly encrypted. The only problem with using this certificate, however, is that our browser doesn\u2019t trust it, because it has not been signed with a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Certificate_authority\">certificate authority<\/a> that the browser trusts.<\/p>\n<p>When you visit a website that uses a self-signed TLS certificate, the browser will block your request and tell you that it\u2019s not safe to proceed. You can, of course, ignore that warning and continue, and for your use-case that might be just fine. After all, self-signed certificates are only useful for private use, such as for testing. But even for private use, if the server is accessed via the open internet, as opposed to from within a trusted private network, it\u2019ll be vulnerable to man-in-the-middle attacks and so it won\u2019t be secure.<\/p>\n<p>The solution to this problem is to create our own certificate authority (CA), sign our certificates using our CA, and add the CA to the operating system\u2019s (or the browser\u2019s) list of trusted certificate authorities.<\/p>\n<p>(If you have absolutely no idea how SSL\/TLS works, you might want to either read <a href=\"https:\/\/www.internetsociety.org\/deploy360\/tls\/basics\/\">this article<\/a> or <a href=\"https:\/\/www.youtube.com\/watch?v=T4Df5_cojAs\">watch this video<\/a> or consult some other source before continuing. This post is only about the mechanics of getting this working on Linux, and so it doesn\u2019t explain any of the concepts behind anything.)<\/p>\n<p>First, we need to generate a private key and a certificate for our certificate authority:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out rootca.key\n<\/span><\/span><span class=\"line\"><span class=\"cl\">openssl req -x509 -key rootca.key -out rootca.crt -subj <span class=\"s2\">&#34;\/CN=localhost-ca\/O=localhost-ca&#34;<\/span>\n<\/span><\/span><\/code><\/pre><p>Note that the certificate of our CA is self-signed, which is necessarily the case with root certificate authorities\u2014and our CA is a root CA. And it\u2019s issued by an organization named \u2018localhost-ca\u2019 (the name, of course, is completely arbitrary).<\/p>\n<p>Next, we need to generate a private key for the signed certificate we\u2019ll be creating. In this example, we\u2019ll be issuing a certificate for <code>localhost<\/code>, so we\u2019ll name the private key <code>localhost.key<\/code>:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out localhost.key\n<\/span><\/span><\/code><\/pre><p>Next, we need to create a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Certificate_signing_request\">certificate signing request<\/a> file, which we\u2019ll use to obtain a signed certificate from our CA. But before we do that, we need to create a config file which we\u2019ll pass to the <code>openssl<\/code> commands that follow<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1<\/a><\/sup>. We\u2019ll name this file <code>csr.conf<\/code>:<\/p>\n<pre><code class=\"language-conf\">[req]\ndistinguished_name = dn\nprompt             = no\nreq_extensions = req_ext\n\n[dn]\nCN=&quot;localhost&quot;\n\n[req_ext]\nsubjectAltName = @alt_names\n\n[alt_names]\nDNS.0 = localhost\n<\/code><\/pre>\n<p>The <code>CN=localhost<\/code> line specifies that we\u2019re using this config file to obtain a certificate for the domain name <code>localhost<\/code>. Additional domain names, which the certificate will be valid for, could be specified in the <code>[alt_names]<\/code> section (with the first domain name named <code>DNS.0<\/code>, the second one named <code>DNS.1<\/code>, etc).<\/p>\n<p>Now we can create the certificate signing request file:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">openssl req -new -key localhost.key -out localhost.csr -config csr.conf\n<\/span><\/span><\/code><\/pre><p>The final step is to obtain a signed certificate from our very own certificate authority:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">openssl x509 -req -days <span class=\"m\">365<\/span> -extensions req_ext -extfile csr.conf -CA rootca.crt -CAkey rootca.key -in localhost.csr -out localhost.crt\n<\/span><\/span><\/code><\/pre><p>The <code>localhost.crt<\/code> file that this command generates is our certificate for the <code>localhost<\/code> domain, and <code>localhost.key<\/code> is its private key. The certificate is now signed with the private key of our CA, so that anyone with our CA\u2019s public certificate, <code>rootca.crt<\/code>, can verify that the certificate is signed by our CA.<\/p>\n<p>All we have to do now is to get our operating-system or the browser to trust our CA. At this point, I would suggest spinning up an HTTPS server at <code>localhost<\/code>, with the certificate and the private key that we just created, to test whether our certificate is working as it should after we make our CA a trusted CA.<\/p>\n<h3 id=\"installing-a-root-ca-certificate-system-wide-on-ubuntu\">Installing a root CA certificate system-wide on Ubuntu<\/h3>\n<p>On Ubuntu, root certificates can be installed with:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">sudo cp rootca.crt \/usr\/local\/share\/ca-certificates\/\n<\/span><\/span><span class=\"line\"><span class=\"cl\">sudo update-ca-certificates\n<\/span><\/span><\/code><\/pre><p>Firefox and Chromium, however, on Ubuntu, use their own certificate stores; so you\u2019ll have to import the root certificate to these browsers individually. Programs like <code>curl<\/code> and <code>wget<\/code> should be able to access <code>https:\/\/localhost<\/code> without a problem now.<\/p>\n<h3 id=\"installing-a-root-ca-certificate-system-wide-on-arch-linux\">Installing a root CA certificate system-wide on Arch Linux<\/h3>\n<p>On Arch Linux, root certificates can be installed using:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\">sudo trust anchor rootca.crt\n<\/span><\/span><\/code><\/pre><p>There\u2019s no need to add root certificates individually to Firefox or Chromium on Arch Linux as the browsers use the system-wide certificates.<\/p>\n<h3 id=\"installing-a-root-ca-certificate-in-firefox\">Installing a root CA certificate in Firefox<\/h3>\n<ol>\n<li>Go to Settings \u2192 Privacy &amp; Security.<\/li>\n<li>Scroll down to the \u2018Certificates\u2019 section and click \u2018View certificates\u2019.<\/li>\n<li>Import the <code>rootca.crt<\/code> file in the \u2018Authorities\u2019 section.<\/li>\n<li>Tick all the checkboxes and hit \u2018OK\u2019.<\/li>\n<li><code>https:\/\/localhost<\/code> should give you no warnings now (you might have to restart the browser).<\/li>\n<\/ol>\n<h3 id=\"installing-a-root-ca-certificate-in-chromium\">Installing a root CA certificate in Chromium<\/h3>\n<ol start=\"6\">\n<li>Go to Settings -&gt; Privacy and Security -&gt; Security -&gt; Manage certificates.<\/li>\n<li>Go to the \u2018Authorities\u2019 tab and import the <code>rootca.crt<\/code> file.<\/li>\n<li>Tick all the checkboxes and hit \u2018OK\u2019.<\/li>\n<li><code>https:\/\/localhost<\/code> should give you no warnings now (you might have to restart the browser).<\/li>\n<\/ol>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"fn:1\">\n<p>Technically, it\u2019s not necessary to create a config file. The information in the config file can be passed as either command-line arguments or via user prompting to <code>openssl<\/code> as well.&#160;<a href=\"#fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;<\/a><\/p>\n<\/li>\n<\/ol>\n<\/div>\n"},{"title":"How to convert TTF fonts to WOFF2","link":"https:\/\/previnder.com\/ttf-to-woff2","guid":"https:\/\/previnder.com\/ttf-to-woff2","pubDate":"Wed, 01 Jan 2025 06:00:02 +0000","description":"<p>When you download a font from a source like <a href=\"https:\/\/fonts.google.com\/\">Google Fonts<\/a>, it\u2019s likely in either <a href=\"https:\/\/en.wikipedia.org\/wiki\/TrueType\">TTF<\/a> or <a href=\"https:\/\/en.wikipedia.org\/wiki\/OpenType\">OTF<\/a> formats, the two most widely used font formats in the world. If you\u2019re going to use these fonts on the web, however, it\u2019s a good idea to convert them to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_Open_Font_Format\">WOFF2<\/a> first, because WOFF2 fonts are significantly smaller in size (WOFF2 fonts are the same as TTF or OTF fonts but compressed).<\/p>\n<p>Of course, in many cases (as with Google Fonts) one can just use a CDN, but it\u2019s generally a good idea to go to the trouble of self-hosting fonts (on the same domain as your site), rather than using a CDN, as then the fonts would be delivered to the end-user slightly faster when they visit your site. That\u2019s because accessing a CDN involves making additional network requests, DNS lookups, fetching additional CSS files, etc.<\/p>\n<p>Fortunately for us, Google has an easy-to-use <a href=\"https:\/\/github.com\/google\/woff2\">open-source program<\/a> for converting between TTF\/OTF and WOFF2 formats. We can just clone the repository, build the program, and use it:<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\"><span class=\"c1\"># Clone and build the program<\/span>\n<\/span><\/span><span class=\"line\"><span class=\"cl\">git clone --recursive https:\/\/github.com\/google\/woff2\n<\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"nb\">cd<\/span> woff2\n<\/span><\/span><span class=\"line\"><span class=\"cl\">make clean all\n<\/span><\/span><span class=\"line\"><span class=\"cl\">\n<\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"c1\"># Use it to convert a TTF\/OTF font to WOFF2<\/span>\n<\/span><\/span><span class=\"line\"><span class=\"cl\">.\/woff2_compress ~\/Download\/font.ttf\n<\/span><\/span><\/code><\/pre><p>To install the program on your computer (assuming of course that it\u2019s Unix-based), just copy the <code>woff2_compress<\/code> and <code>woff2_decompress<\/code> binaries to a directory in your <code>$PATH<\/code> (on Linux, I would just copy them to <code>\/usr\/bin<\/code>).<\/p>\n"},{"title":"How to gracefully shutdown a Go HTTP server","link":"https:\/\/previnder.com\/shutdown-go-http-server","guid":"https:\/\/previnder.com\/shutdown-go-http-server","pubDate":"Tue, 03 Dec 2024 05:58:20 +0000","description":"<p>The following code snippet of Go starts a simple HTTP server that responds with \u201cHello World\u201d to every request it gets.<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\"><span class=\"kd\">func<\/span><span class=\"w\"> <\/span><span class=\"nf\">main<\/span><span class=\"p\">()<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"nx\">server<\/span><span class=\"w\"> <\/span><span class=\"o\">:=<\/span><span class=\"w\"> <\/span><span class=\"o\">&amp;<\/span><span class=\"nx\">http<\/span><span class=\"p\">.<\/span><span class=\"nx\">Server<\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"nx\">Addr<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s\">&#34;:8080&#34;<\/span><span class=\"p\">,<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"nx\">Handler<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"nx\">http<\/span><span class=\"p\">.<\/span><span class=\"nf\">HandlerFunc<\/span><span class=\"p\">(<\/span><span class=\"kd\">func<\/span><span class=\"p\">(<\/span><span class=\"nx\">w<\/span><span class=\"w\"> <\/span><span class=\"nx\">http<\/span><span class=\"p\">.<\/span><span class=\"nx\">ResponseWriter<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nx\">r<\/span><span class=\"w\"> <\/span><span class=\"o\">*<\/span><span class=\"nx\">http<\/span><span class=\"p\">.<\/span><span class=\"nx\">Request<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t\t<\/span><span class=\"nx\">w<\/span><span class=\"p\">.<\/span><span class=\"nf\">Write<\/span><span class=\"p\">([]<\/span><span class=\"nb\">byte<\/span><span class=\"p\">(<\/span><span class=\"s\">&#34;&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello World&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&#34;<\/span><span class=\"p\">))<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"p\">}),<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"k\">if<\/span><span class=\"w\"> <\/span><span class=\"nx\">err<\/span><span class=\"w\"> <\/span><span class=\"o\">:=<\/span><span class=\"w\"> <\/span><span class=\"nx\">server<\/span><span class=\"p\">.<\/span><span class=\"nf\">ListenAndServe<\/span><span class=\"p\">();<\/span><span class=\"w\"> <\/span><span class=\"nx\">err<\/span><span class=\"w\"> <\/span><span class=\"o\">!=<\/span><span class=\"w\"> <\/span><span class=\"kc\">nil<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"nx\">log<\/span><span class=\"p\">.<\/span><span class=\"nf\">Fatal<\/span><span class=\"p\">(<\/span><span class=\"nx\">err<\/span><span class=\"p\">)<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/span><\/span><\/code><\/pre><p>But there\u2019s a small problem: the <code>Server.ListenAndServe<\/code> method blocks until the server terminates\u2014until, to be more specific, one of <code>Server.Close<\/code> or <code>Server.Shutdown<\/code> methods is called, or it encounters a fatal error. In this example, the server never terminates (unless there\u2019s an error), and one would have to terminate the process manually from the outside, by for instance hitting <code>Ctrl+C<\/code> and sending an interrupt <a href=\"https:\/\/en.wikipedia.org\/wiki\/Signal_(IPC)\">signal<\/a> to the process.<\/p>\n<p>But that would also terminate any HTTP requests that are being read or responses being written at that moment, mid-flight. This is not of course, especially for a production service, the optimal way this should work.<\/p>\n<p>The <code>Server.Shutdown<\/code> method, in fact, exists for this particular use case, for shutting down the server gracefully, after waiting until all connections have returned to idle. In the following snippet, the program waits until it receives a <code>SIGINT<\/code> or a <code>SIGKILL<\/code> signal, and when it does, it shuts down the HTTP server gracefully before exiting the program.<\/p>\n<p>(If you\u2019re unfamiliar with <a href=\"https:\/\/go.dev\/tour\/concurrency\/2\">Go\u2019s channels<\/a> or <a href=\"https:\/\/pkg.go.dev\/context\">context.Context<\/a>, you most definitely won\u2019t understand how the following works. But if you\u2019re familiar with those two things, the following should be easy to read and understand.)<\/p>\n<pre class=\"chroma\"><code><span class=\"line\"><span class=\"cl\"><span class=\"kd\">func<\/span><span class=\"w\"> <\/span><span class=\"nf\">main<\/span><span class=\"p\">()<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"nx\">server<\/span><span class=\"w\"> <\/span><span class=\"o\">:=<\/span><span class=\"w\"> <\/span><span class=\"o\">&amp;<\/span><span class=\"nx\">http<\/span><span class=\"p\">.<\/span><span class=\"nx\">Server<\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"nx\">Addr<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"s\">&#34;:8080&#34;<\/span><span class=\"p\">,<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"nx\">Handler<\/span><span class=\"p\">:<\/span><span class=\"w\"> <\/span><span class=\"nx\">http<\/span><span class=\"p\">.<\/span><span class=\"nf\">HandlerFunc<\/span><span class=\"p\">(<\/span><span class=\"kd\">func<\/span><span class=\"p\">(<\/span><span class=\"nx\">w<\/span><span class=\"w\"> <\/span><span class=\"nx\">http<\/span><span class=\"p\">.<\/span><span class=\"nx\">ResponseWriter<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nx\">r<\/span><span class=\"w\"> <\/span><span class=\"o\">*<\/span><span class=\"nx\">http<\/span><span class=\"p\">.<\/span><span class=\"nx\">Request<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t\t<\/span><span class=\"nx\">w<\/span><span class=\"p\">.<\/span><span class=\"nf\">Write<\/span><span class=\"p\">([]<\/span><span class=\"nb\">byte<\/span><span class=\"p\">(<\/span><span class=\"s\">&#34;&lt;html&gt;&lt;body&gt;&lt;h1&gt;Hello World&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&#34;<\/span><span class=\"p\">))<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"p\">}),<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ From the documentation of the signal package:<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ NotifyContext returns a copy of the parent context that is marked done<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ (its Done channel is closed) when one of the listed signals arrives,<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ when the returned stop function is called, or when the parent context&#39;s<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ Done channel is closed, whichever happens first.<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ ....<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"nx\">ctx<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nx\">stop<\/span><span class=\"w\"> <\/span><span class=\"o\">:=<\/span><span class=\"w\"> <\/span><span class=\"nx\">signal<\/span><span class=\"p\">.<\/span><span class=\"nf\">NotifyContext<\/span><span class=\"p\">(<\/span><span class=\"nx\">context<\/span><span class=\"p\">.<\/span><span class=\"nf\">Background<\/span><span class=\"p\">(),<\/span><span class=\"w\"> <\/span><span class=\"nx\">os<\/span><span class=\"p\">.<\/span><span class=\"nx\">Interrupt<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nx\">os<\/span><span class=\"p\">.<\/span><span class=\"nx\">Kill<\/span><span class=\"p\">)<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"k\">defer<\/span><span class=\"w\"> <\/span><span class=\"nf\">stop<\/span><span class=\"p\">()<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"k\">go<\/span><span class=\"w\"> <\/span><span class=\"kd\">func<\/span><span class=\"p\">()<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"c1\">\/\/ ListenAndServe always returns a non-nil error. And if it returns as a<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"c1\">\/\/ result of a call to Server.Close or Server.Shutdown, it returns<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"c1\">\/\/ http.ErrServerClosed.<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"k\">if<\/span><span class=\"w\"> <\/span><span class=\"nx\">err<\/span><span class=\"w\"> <\/span><span class=\"o\">:=<\/span><span class=\"w\"> <\/span><span class=\"nx\">server<\/span><span class=\"p\">.<\/span><span class=\"nf\">ListenAndServe<\/span><span class=\"p\">();<\/span><span class=\"w\"> <\/span><span class=\"nx\">err<\/span><span class=\"w\"> <\/span><span class=\"o\">!=<\/span><span class=\"w\"> <\/span><span class=\"kc\">nil<\/span><span class=\"w\"> <\/span><span class=\"o\">&amp;&amp;<\/span><span class=\"w\"> <\/span><span class=\"p\">!<\/span><span class=\"nx\">errors<\/span><span class=\"p\">.<\/span><span class=\"nf\">Is<\/span><span class=\"p\">(<\/span><span class=\"nx\">err<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nx\">http<\/span><span class=\"p\">.<\/span><span class=\"nx\">ErrServerClosed<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t\t<\/span><span class=\"nx\">log<\/span><span class=\"p\">.<\/span><span class=\"nf\">Printf<\/span><span class=\"p\">(<\/span><span class=\"s\">&#34;ListenAndServe (%s) error: %v\\n&#34;<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nx\">server<\/span><span class=\"p\">.<\/span><span class=\"nx\">Addr<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nx\">err<\/span><span class=\"p\">)<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"p\">}()<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ Blocks until the process receives a SIGINT or SIGKILL signal.<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"o\">&lt;-<\/span><span class=\"nx\">ctx<\/span><span class=\"p\">.<\/span><span class=\"nf\">Done<\/span><span class=\"p\">()<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ From the documentation of the http package:<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ Shutdown gracefully shuts down the server without interrupting any<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ active connections. Shutdown works by first closing all open<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ listeners, then closing all idle connections, and then waiting<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ indefinitely for connections to return to idle and then shut down.<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ If the provided context expires before the shutdown is complete,<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ Shutdown returns the context&#39;s error, otherwise it returns any<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ error returned from closing the [Server]&#39;s underlying Listener(s).<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ When Shutdown is called, [Serve], [ListenAndServe], and<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ [ListenAndServeTLS] immediately return [ErrServerClosed]. Make sure the<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ program doesn&#39;t exit and waits instead for Shutdown to return.<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"c1\">\/\/ ....<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"k\">if<\/span><span class=\"w\"> <\/span><span class=\"nx\">err<\/span><span class=\"w\"> <\/span><span class=\"o\">:=<\/span><span class=\"w\"> <\/span><span class=\"nx\">server<\/span><span class=\"p\">.<\/span><span class=\"nf\">Shutdown<\/span><span class=\"p\">(<\/span><span class=\"nx\">context<\/span><span class=\"p\">.<\/span><span class=\"nf\">Background<\/span><span class=\"p\">());<\/span><span class=\"w\"> <\/span><span class=\"nx\">err<\/span><span class=\"w\"> <\/span><span class=\"o\">!=<\/span><span class=\"w\"> <\/span><span class=\"kc\">nil<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t\t<\/span><span class=\"nx\">log<\/span><span class=\"p\">.<\/span><span class=\"nf\">Fatalf<\/span><span class=\"p\">(<\/span><span class=\"s\">&#34;Error gracefully shutting down server: %v\\n&#34;<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"nx\">err<\/span><span class=\"p\">)<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">\t<\/span><span class=\"nx\">log<\/span><span class=\"p\">.<\/span><span class=\"nf\">Println<\/span><span class=\"p\">(<\/span><span class=\"s\">&#34;Server exited gracefully&#34;<\/span><span class=\"p\">)<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/span><\/span><\/code><\/pre><p>Of course, more complex behavior than this can be implemented, which in some cases might be quite useful. For example, receiving a second interrupt signal, while the program waits until <code>server.Shutdown<\/code> to return, could be made to exit the process immediately; or there could be a time limit for how long the program waits for <code>server.Shutdown<\/code> to return; etc.<\/p>\n"},{"title":"By way of beginning","link":"https:\/\/previnder.com\/beginning","guid":"https:\/\/previnder.com\/beginning","pubDate":"Sat, 16 Nov 2024 05:56:35 +0000","description":"<p>There is perhaps a uniquely human instinct to leave our mark on the world, if nothing else to simply say that we were once here to whomever comes later. People have a tendency to carve their names or initials on rocks, trunks of trees, walls, benches, or some other hard surface they know is not going to be withered away by the elements too soon, especially if they\u2019ve spent a lot of time in that place. The image that best illustrates this instinct to me is the stenciled hands humans have left, thousands of years ago, on the walls of <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cueva_de_las_Manos\">Cueva de las Manos<\/a>, in Santa Cruz, Argentina.<\/p>\n<p>This website, I suppose, is also in part inspired by that same egoic instinct to try and preserve something of oneself. It also strikes me as an excellent idea to make this website the home or the central hub of my digital, online self. It\u2019s a space that I own, and ownership makes one think about a thing differently; which is why I\u2019ve gone to the trouble of designing this website from scratch, to suit my tastes and preferences.<\/p>\n<p>If the writing that is to follow on this site is to have a theme, then it shall be the pursuit of truth and knowledge from the perspective of a single individual, starting somewhat from the beginning. I\u2019m not interested in writing for the sake of writing, and for that reason I suppose I\u2019m not a real writer. Writing for me is always a secondary activity, a means to an end, downstream from exploration, learning, doing, and thinking.<\/p>\n<p>The world we find ourselves in is fundamentally mysterious\u2014and so are we ourselves\u2014but most of us, being the pragmatic and responsible grown ups that we are, go about our days as if this was not the case, as if everything around us was perfectly ordinary and familiar. When Benjamin Franklin once noted that, \u201cMany people die at twenty five and aren't buried until they are seventy five,\u201d this ubiquitous incuriosity of much of the grown up world must have been what he meant.<\/p>\n<p>There\u2019s much that I don\u2019t know but wish to know, and much that I don\u2019t understand but wish to understand. I hope this website would serve as a chronicle, for my future self if for no one else, of my gradually decreasing, but always infinite, ignorance.<\/p>\n"}]}}