Some Bits: Nelson's log

8 posts in December 2016

Progressive web sites and service workers

Awake early I read this interesting blog post about making a very fast modern website. Lots of good stuff in there, like React alternatives that are lighter weight, avoiding polyfill overhead for modern browsers, the value of server side rendering, etc. Also two things that were entirely new to me: “Progressive web apps” and “Service Workers”. Also new-to-me: Google’s web fundamentals instructional material.

Progressive web app is the buzzword for a web page that works like a little application. Like Gmail in the days of yore, or Google Docs, or any modern / complex site like Twitter or Facebook or whatever. Or my own single page apps like WindHistory and LogsOfLag. Web apps are nothing new, but the “progressive” is meant to suggest a site that works fast even on mobile devices with slow and unreliable networking. Key ideas are rendering quickly and handling unreliable networks intelligently. They also have a neat tool called Lighthouse that audits a web app for progressiveness. A lot of what it checks for is mom-and-apple-pie, deferring script loads etc.

But Service Workers are the key magic. They are “a programmable network proxy, allowing you to control how network requests from your page are handled”. Basically it’s a background Javascript thread running asynchronously doing all the network for your web app. The web app page installs a service worker once the first time the site is loaded. Then it lives in the background doing things for your app like loading resources, receiving push notifications, synchronizing data to the server, etc. It communicates asynchronously via messages to the Javascript in the browser page.

It sounds pretty powerful. Basically it gives the developer control over the browser’s network, how stuff is cached and when it is loaded, etc. It also sounds pretty complicated, and I imagine 95% of developers won’t code to this API themselves and instead use a framework that takes advantage of it to optimize performance.

The big problem is service workers aren’t fully supported: on desktop Chrome and Firefox support it, but not MSIE/Edge nor Apple Safari. Mobile support is not good either. And this feature is so complex I don’t think you can polyfill support in, certainly not in a meaningfully performant way. I don’t have enough perspective to see whether service workers have momentum. Chrome has supported it for two years, which in a way is a bad sign that it’s been that long and there’s no clear consensus for implementing it everywhere. OTOH it solves a real problem and Apple and Microsoft are certainly not known for leading the way in web development.

 

Bonus links from reading that blog post. The fetch API is a modern alternative to XMLHttpRequest, etc which is nicer to use. Polyfills exist. And speaking of polyfills, polyfill.io is a nice CDN service that delivers optimized polyfills to browsers based on their User-Agent. Finally freegeoip.net does free geolocation of IP addresses and powers a polyfill for the HTML5 location API. It runs as a free service with usage limits, or you can download the IP database and code and run your own.

Ubuntu 16 NTP notes

I love NTP. What’s it look like in Ubuntu 16? ntpd is pretty much the same, but some change or things I’ve never noticed before.

Debug mode 7 queries are now entirely disabled. The ntpdc program basically doesn’t work; you use ntpq instead. Given the number of times this protocol has been used in a UDP amplification DDOS, it’s a necessary change.

ntpd is started with the -g flag. This flag allows NTP to set the system clock once no matter how far out of sync it is. The default behavior is to never move the clock more than 1000s as a sort of sanity check. In practice it’s usually the client that’s insane, not the server.

servers are now configured like this

pool 0.ubuntu.pool.ntp.org iburst

“iburst” means the ntpd will try 8 times in 16 seconds to reach an unreachable server. The “pool” directive is not in the man pages, but these docs explain it. It tells ntpd to use DNS differently, to look up random new IP addresses every time. I’m not positive but I think ntpd still isn’t smart enough to look up a new pool IP address every few days or when a server goes offline. But at least it’ll get different IP addresses for the same hostname.

In addition to ntpdate/ntpd, Ubuntu also now provides timedatectl and systemd-timesyncd, part of the systemd hydra. It’s not very well documented; see Arch’s docs and this announcement. It’s a lightweight client-only time synchronizer. That seems like a good idea for security / client footprint. But it may be a bit too lightweight. It sounds like it only gets time from one server, for instance, and I wonder if they’re doing the PLL clock syncing that makes ntpd so good. systemd’s stuff gets out of the way if you are running ntpd.

Verizon tethering experiment

I need a backup for Internet in Grass Valley for when my WISP goes down. (Which is frequently, every Sunday morning lately.) Verizon now enables tethering on iPhones at no extra charge, just data used, part of the new simpler service plans. So I tried it briefly.

Quick result is it’s not bad. I have two dots of signal on my iPhone. I get 4Mbps down, 130kbps up on a DSLReports speed test. Latency to somebits.com was 130ms average, compare 90ms on my WISP link. But the jitter was terrible, some 400ms+ pings. At $5-$10/GB I’m not going to want to use this regularly, but it’s fine as a backup.

One oddity. My tethered network connection is genuinely IPv6, and my Mac automatically uses it. Yay! The downside is the first time I tried this I could only connect to IPv6 sites; v4 only sites like my own blog wouldn’t work. I originally tested this by configuring my ethernet to “off” in the Mac network preferences, and maybe somehow it retained the IPv4 route through the wire? Explicitly unplugging the wire worked fine.

I’ve been considering getting a mobile hotspot; Wirecutter recommends Verizon’s Jetpack AC791L for $200 (or $50 with a two year contract). It looks pretty reasonable if I want a dedicated device and not just use my phone. Main reason I see to do that is for travel, so I can keep my US SIM card in the phone. Verizon’s plan seems to allow extra devices on my account for $10 / device and I can turn it off and on. Or just buy the Verizon device with the contract.

                                                        Packets               Pings
 Host                                                  Loss%   Snt   Last   Avg  Best  Wrst StDev
 1. 172.20.10.1                                         0.0%   158    2.7  18.9   1.8 303.4  43.4
 2. 69.sub-66-174-31.myvzw.com                          0.0%   157   37.5  77.4  28.2 319.4  70.9
 3. 20.sub-69-83-164.myvzw.com                          0.6%   157   41.1  81.8  26.2 400.8  75.2
 4. 170.sub-69-83-164.myvzw.com                         4.5%   157   36.7  81.5  25.2 323.5  78.8
 5. 2.sub-69-83-164.myvzw.com                           5.7%   157   35.1  77.9  30.4 382.5  71.4
 6. 12.sub-69-83-163.myvzw.com                          8.3%   157   41.4  72.9  30.1 455.9  69.5
 7. 33.sub-66-174-29.myvzw.com                          2.5%   157   42.1  83.5  29.6 380.4  81.1
 8. et-2-0-0.GW11.SAC1.ALTER.NET                        8.9%   157   28.1  71.8  28.1 349.4  65.8
 9. ???
10. 0.ae1.BR3.SJC7.ALTER.NET                            3.8%   157   46.5  94.3  35.6 394.6  71.2
11. 204.255.168.26                                      8.3%   157   56.1  80.3  32.7 289.6  61.7
12. 100ge12-2.core1.den1.he.net                        10.8%   157   76.8 120.1  76.8 509.8  60.7
13. 100ge14-1.core1.mci3.he.net                        21.7%   157   99.9 124.2  86.2 310.3  45.4
14. 10ge4-1.core1.mci2.he.net                          49.7%   157   93.1 120.8  84.0 454.4  58.9
15. wholesale-internet-inc.10gigabitethernet1-3.core1. 38.9%   157  105.2 125.4  79.4 336.2  61.4
16. lag-to-oak.edge-b.clay1.mci.us.as32097.net         39.7%   157   94.2 130.1  77.5 377.2  64.2
    lag-to-oak.edge-a.clay1.mci.us.as32097.net
17. lag-core-a.dist-1-3.clay1.mci.us.as32097.net       34.4%   157  101.9 122.1  82.0 333.8  54.4
    lag-core-b.dist-1-3.clay1.mci.us.as32097.net
18. wk.somebits.com                                     0.6%   157   97.5 135.5  77.6 436.9  72.9

127 packets transmitted, 127 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 79.470/131.121/471.344/69.791 ms

Ubuntu 14.04 to 16.04 notes

I had a moment of foolish optimism and decided to upgrade my development machine from Ubuntu 14 (Trusty) to Ubuntu 16 (Xenial). “Just run do-release-upgrade”, the motd has been encouraging me for months now. So I did.

And it failed. And then I tried to fix it by hand and I thought I did. And then the machine didn’t reboot. So that’s discouraging. But then the second time I tried to reboot (after hauling the machine over to a monitor) it rebooted fine. And now the machine is fine. No idea what that was about, but yay for Ubuntu robustness! My reboot problem may have nothing to do with Ubuntu, but rather be a problem with my ethernet. Anyway, some notes.

On the upgrade scripts

  • do-release-upgrade runs in a screen session. Some of the upgrades prompt you to compare changes in config files, and will helpfully propose spawning a shell to let you inspect the situation. But your tty is occupied in some weird way with screen and/or scripts running in the background. I was getting all sorts of random crap on my screen and I panicked and aborted the upgrade script. Do not make this mistake.
  • You can’t run do-release-upgrade a second time. The system thinks it was already upgraded and it aborts.
  • Fortunately mostly do-release-upgrade does stuff up front, then relies on apt and dpkg to do the real work. Those tools are robust. I’m not really clear on what all do-release-upgrade does, but here’s a reference. I sure hope there wasn’t anything crucial that didn’t happen because I aborted.
  • I fixed things by running dpkg-configure by hand, then running apt-get upgrade a bunch of times. apt-get dist-upgrade is probably not a bad idea either.
  • I also improved things by removing a bunch of PPAs from /etc/apt/sources.list.d.
  • There are some install logs in /var/log/dist-upgrade, including a full screen recording.

On changes in Ubuntu 16

  • Welcome to systemd! Everything about how the machine boots is different. I mostly noticed this with warning messages printed at boot time about funky scripts in /etc/rc that weren’t up to snuff. These came from me or PPAs, not the core code.
  • “systemctl” seems like an important new command to learn. Useful commands: enable/disable, start/stop, status. Also “journalctl” to see systemd’s logs.
  • some init scripts were broken / old / weird. I have a bunch of old cruft! I fixed these by purging the packages and reinstalling them. Culprits included screen and mongodb.
  • My 2012 lm-sensors install included some kernel modules that no longer exist. Fixed by re-running detect-sensors and removing the old entries from /etc/modules
  • I’m getting an error about mount options in dmesg that I can’t understand. It doesn’t seem to be hurting anything though, so ignoring it.
  • grep thinks my syslog is a binary file, it has some nulls in it (which is terrifying!). So i have to use “grep -a” to see the whole thing.

On user programs

  • I run Crashplan which makes extensive use of inotify. The default Linux settings only allow 8192 files/directories to be watched, not enough. Here’s how to update the limit.
  • I had to manually install python3-venv with apt. Then my existing venv directories were broken and I had to rebuild them.
  • My plex server was broken. Difficulty level: systemd. I finally traced the problem down with help from here; “systemctl status” told me it was having a problem with the group. Turns out the server wants to run as group plex, not just user plex, but no one had set up a group plex. BTW hooray! The Plex team finally has an official apt repository
  • I somehow ended up with Postgres 9.1, 9.3, and 9.5 all on my system. With 9.3 and 9.5 daemons fully running. I don’t have any permanent data on the development machine so I went with a full wipe and reinstall of all Postgres. The stock Ubuntu 16 versions of GDAL and PostGIS seem to work nicely.

Testing Amazon S3 CORS headers

Resources served from AWS S3 can have CORS headers set, useful for JSON data intended for inclusion. But testing it from the command line is not simple.

Plain old “curl” doesn’t work because it doesn’t show headers. “curl --verbose” works, but even if CORS is configured on S3 you won’t get CORS headers. You have to set an Origin: header in the HTTP request to induce Amazon to set a CORS header in the response.

curl -H 'Origin: http://example.com' --verbose http://s3.amazonaws.com/data.openaddresses.io/machine-stats.json > /dev/null

Also doesn’t work: “curl -I”. That’s supposed to be curl’s “print headers” option but it also switches to doing a HEAD, not a GET, and S3 doesn’t set the CORS headers for HEAD requests.

 

Global surface water map

Nathaniel Kelso tweeted about an interesting new map of global surface water, derived from classifying Landsat images. Here’s an article, another article, and the paywalled Nature paper.

Best of all, here’s the data! Very nicely prepared for use. I tossed some of the California occurrence GeoTIFFs into QGIS for a quick plot. This view is centered on Engelbright Dam with Sacramento to the west, where the American and Feather rivers meet. Blue is year-round water; pink is intermittent (and seems to highlight farmland.)

Screen Shot 2016-12-09 at 9.19.03 AM.png

At a scale of 30m per pixel this dataset doesn’t show small rivers or creeks. But what this map does show is the history of water over 30 years of Landsat imagery. In addition to the basic occurrence map there are also several data layers highlighting changes in the surface water. One showing before and after Jan 1 2000, one showing seasonality, one showing yearly recurrence, and some other more technical measures.

It’s a solid piece of work and nicely packaged. And all free!

Python Pyramid framework

Interesting conversation on Reddit about scalable webapp frameworks. Lots of endorsements for Pyramids, the successor to the old Pylons project. After a brief review it looks like it’s similar to Flask. The Reddit discussion says it scales better, that it’s thread safe and there’s a hope of running it async.

I like what Pyramids says about philosophy:

Megaframeworks make decisions for you. But if you don’t fit their viewpoint, you end up fighting their decisions. Microframeworks force no decisions, making it easy to start. But as your application grows, you’re on your own.

In both cases, the focus is on the start: either too much or too little. Either way, finishing and staying finished is hard. You need a finishing-focused framework with an architectural design that scales down to getting started, then up as your application grows.

It seems to mostly be focussed on URL dispatch, with a structure for web views and sessions. And support for logging and testing! Also has some connectivity to various template libraries in Python. No database code at all, but there’s extensive docs on using SQLAlchemy. Also the note “Pyramid has a strong orientation towards support for transactions”.

small Wikipedia text for inlining

Reddit Enhancement Sweet has a neat feature where if someone links to a Wikipedia article you can click on an expando button and a div pops up showing just a small bit of the Wikipedia article right on top of the page, a sort of quick preview. Here’s the code for it.

It works by using the Wikipedia API, specifically the “parse” method. (So named because it parses Wikimedia source data and returns JSON, the article contents). This URL is a sample query for the JSON contents of the Round-robin DNS page. There’s a zillion options for configuring what it returns, section=0 seems like a useful one to only get the first section of an article, typically the summary. And here’s how to get only the first paragraph.

The Wikipedia API has a sandbox for designing queries.

BTW I’m having a hard time with Chrome Developer Tools lately. I can’t find that Reddit page + RES loading this JSON anywhere. Maybe because it’s happening in a Chrome extension, out of site in a background page or something? Dunno.