emicklei/go-restful — pull request #168 by deads2k (2014-12-01 19:00:57+00:00)
Title: make compilePathExpression threadsafe

When using go-restful, if we start hitting the rest endpoints during our integration tests, we can see data races like this: https://gist.github.com/deads2k/9eb5e74e0341904529a2.

By locking the compiledPathExpression method on a per instance basis, the problem can be avoided. I initially tried a double checked lock, but that still failed with the data race failure. Based on my read of https://golang.org/doc/articles/race_detector.html, it seems that Thread A gains access to the critical section and does a set. Meanwhile, Thread B performs the first check and gets "true". That counts as a read access at the same time as a write access. At any rate, I couldn't make the normal double checked lock work like this:

```
if w.pathExpr == nil {
w.compilationMutex.Lock()
defer w.compilationMutex.Unlock()
if w.pathExpr == nil {
```

So I locked the entire thing. I'm open to any other solution that works.