Facebook
Twitter
Google+
Kommentare
0

Parallel "mv" with Google Go

The first question, that might come into you mind, could be „why the heck should I do a parallel „mv““? The answer is quite easy: Imagine you have a bunch of folders that you unpack and move to a specific location –  your http document root. Those folders are dependend on each other, using php files via include or require, etc. You need to move them consitently as „the new release“ to their destination. You might ask, why the normal linux „mv“ is not enough, even when combining the call with „&&“? Imagine that the webserver has a normal traffic of over 1000 page impressions per second. How big is the timeframe you have for moving the folders without having an inconsistent state? The answer is heavy to guess or to calculate. With a transfer time of ~1 msec you hit the 1% rate of getting a request for a page with an inconsistent state. The more you get under the 1 msec mark, the better it is.

The parallel version, which uses Goroutines – a threading model that is even more lightweight than posix threads – does a move of two folders in ~0.019 msec. This reduces the chance of failure under 1%, even under 0.2%.

As we use it for our release process, the folders are compiled in the binary. This is not very flexible, but prevents typos or human failure while using it.

As posterous does not like my embedded code, please find it here 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import ("os";"runtime")

func main() {
    var aFromList [2]string= [2]string{"/tmp/checkout/folder1_new","/tmp/checkout/folder2_new"}
    var aToList [2]string = [2]string{"/var/www/folder1","/var/www/folder2"}

    runtime.GOMAXPROCS(2)
    c := make(chan int)

    for k,v := range aFromList {
        go move(v, aToList[k], c)
    }

    <-c
    os.Exit(0)
}

func move(f string, t string, c chan int) {
    os.Rename(f, t)
    c <- 1
}

Über den Autor

Mario Müller

Link erfolgreich vorgeschlagen.

Vielen Dank, dass du einen Link vorgeschlagen hast. Wir werden ihn sobald wie möglich prüfen. Schließen