Goroutines don’t panic if channel is gone

Couple of months ago I wrote an article about error handling in concurrent Go programs. One concern I had about it was if other goroutines would panic if some goroutine produces an error value which causes return from function when the channel is iterated with range construct.

So I made another program that mimics scenario. It spawns couple of goroutines, each taking a second more to complete than previous (for the sake of test being easier to observe). When done, they write to channel. 5th goroutine returns an error (after 5 seconds) via channel which is iterated using range. In case of an error, it’s returned from function.

package main

import (
    "errors"
    "sync"
    "time"
)

type data struct {
    err  error
    data int
}

func someWork(dataChan chan<- *data, collection []int) {
    var wg sync.WaitGroup
    l := len(collection)
    wg.Add(l)
    for i, e := range collection {
        go func(i, e int) {
            time.Sleep(time.Duration(i) * time.Second)
            var err error
            if i == 5 {
                err = errors.New("error")
            }
            dataChan <- &data{err, e}
            defer wg.Done()
        }(i, e)
    }
    wg.Wait()
    close(dataChan)
}

func someFunc() {
    collection := []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
    dataChan := make(chan *data)

    go someWork(dataChan, collection)

    for c := range dataChan {
        if c.err != nil {
            return
        }
    }

    time.Sleep(2 * time.Second)
}

func main() {
    someFunc()
    time.Sleep(20 * time.Second)
}

It surprised me a little other goroutines which still aren't finished don't cause panic because channel is gone even. For more realistic test I've also added some work after someFunc is finished.

Don't know why, it sure has to do with the way garbage collection works in Go (would be a great read if there's detailed explanation of it). I'm glad though I don't have to deal with goroutines panicking myself.

Auto-reloading (safe-write) issues of development servers

I have discovered new module bundler for developing applications in JavaScript. It’s called Parcel.js and my initial impressions are positive. That’s what I tweeted about it:

Parcel is a web application bundler, differentiated by its developer experience. It offers blazing fast performance utilizing multicore processing, and requires zero configuration. Basically a fast Webpack alternative only without tons of configuration. Read more about its features here. I discovered it while looking at some library’s package.json checking which dependencies it uses. I use this technique often whatever the language library is written in because that’s how I find new and exciting libraries which makes me more productive.

The first problem I encountered while using it, has actually nothing to do with Parcel.js itself, but the way most editors save files which makes Parcel.js’ and many other development servers’ reloading capabilities limited to only first file save. This feature is called safe-write which doesn’t save changes in original file, but in a temporary file. Temporary file is than renamed and replaces original. This causes development servers with reload capabilities to lose track of file. Luckily we can turn this off (how-to for popular code editors). But before you do this, please educate yourself about the advantages of using safe-write. To sum up, it guards you against data loss if editor crashes. In my case this almost never happens so I don’t see the point of having safe-write enabled.

TV series and movies I’m watching these days

I like watching TV series and movies from time to time. Not on TV, but on my laptop because advantages are clear to me; no annoying ads and you can watch video on-demand. You can also organize time how you are going to watch it. For example often I don’t watch a movie from start to finish in one evening but in course of several.

So these days I’m watching two TV series (The Good Doctor and This is Us) and if I’m in a mood, a movie which I pick from excellent A good movie to watch website. In the past I’ve been watching more but I’ve found out it’s is too much and I tend not to be an extremist.

I’m really proud of my picks. I’m really enjoying these two series, which are certainly a step up from past TV series I’ve been watching. Both TV series have good, lovely characters that actually make me care about them and don’t make me question a story from scenario point of view which happened a lot in TV series I’ve been watching before.

As for movies, I have been searching for a movie recommendation website for a long time. Preferably the one where blockbusters and highly marketed movies aren’t the center of attention. It’s not that I do not enjoy watching action movies or that they are bad, it’s just that less-known movies often have to offers more. I have settled for A good movie to watch because it lists those kind of movies and because I enjoyed first couple of movies I picked from the site (for example Blue Jay, A Separation, Wild Tales, Incendies, 45 years).

But when I have more time to spare, I can go nuts in watching TV series. This means loading a whole season of some new and highly praised TV series and binge-watch it. If I enjoy it more than one of my current pick, they get swapped without feeling bad for a TV series that gets kicked out.

Total Lunar Eclipse (Super Blue Blood Moon), January 31, 2018

Yesterday sister sent me a request over an e-mail if I could print some document for her. Of course I wanted to grant her wish immediately only to find out after first page being printed, a black cartridge was out. Since it was only 6 pm and the nearest shop that sells cartridges was still open for an hour, I decided to take a walk. It was a nice weather outside, low temperatures (7 °C) felt refreshing, and after a few steps I glanced up in the sky. I noticed bright clouds and at first thought they are just high enough for sun still reaching them. But after some distance, when house rooftops cleared from my view, I saw the reason. It was a full moon, slightly bigger than usual and brighter.

Till than I knew nothing about the phenomena which is quite unusual for me since I spent a lot of time behind computer due to my profession. So I should’ve been able to spot the news by then on news portal and social media. It turned out this was a total lunar eclipse, also know as a full blue blood moon. This means:

  • super: full or new moon that is more or less closest to the shortest distance between Moon and Earth,
  • blue: has nothing to do with the colour. Refers to second full moon in a month or 3rd or 4th in a season.
  • blood: due to its reddish colour. It happens because sunlight reflects from Earth and its atmosphere filters and scatters green and violet lights on spectrum more than red.

Avtomatska rokometna lestvica (WordPress plugin)

Last weekend I developed for an acquintance a WordPress plugin which I’ve titled “Avtomatska rokometna lestvica” (translates to “Automatic handball league table” from Slovene). He’s a contributor to an official website of RK Trimo Trebnje, my local handball club and asked me if it’s possible to have a functionality which replaces the manual work of reading a website with statistics for a league table, selecting only a handful of columns and saving it to a blogpost. I had a lot of experiences with web scraping so I was immediately sure it can be done.

Without knowing where the scraped data will be displayed, I coded plugin’s option page using WordPress’s Settings API (and utilizing my library which simplifies creating option’s page). It’s just a textarea which accepts CSV-like data where each league table is represented in one line and one line contains a selection name and URL to its stats.

I have also coded a logic which actually scrapes the necessary data – table of team position, team name and team points and makes new table which is than saved into the database. For reading and constructing HTML tables I’ve used PHP’s DOMDocument. I’ve enjoyed working with this class.

Web-scraping is done periodically at midnight using wp-cron. They way wp-cron works is it launches when first user visits the site after the time it’s set to repeat.

Now comes the part where I learned how it’s going to be displayed. More than one league table needed to be shown (for selections like members, cadets, juniors,…), they were grouped in tabs. Inputs where they are edited were present on certain WordPress page using cusom fields. The problem for me was I had no idea where this fields are stored in the database. So the content from web-scraping process can be saved there.

I fixed this problem quite elegantly, I must say. I saved the content into wp_options table under a key {$package}_{$selection} and for displaying it I’ve coded a shortcode which queries wp_options for the key {$package}_{$selection} and returns HTML stored under that key – selection is specified as an argument to shortcode.