Commits

Miki Tebeka committed 32a8ef1

Fixed a bug in backend assignment

Comments (0)

Files changed (2)

+2012-09-03 version 0.1.2
+    * Synchronize backend changes (issue #2)
+
 2012-08-21 version 0.1.1
     * Handle error in http listener
     * Add -version flag
 	"net"
 	"net/http"
 	"os"
+	"sync"
 )
 
 const (
-	Version = "0.1.1"
+	Version = "0.1.2"
 )
 
 // Current backend
 var backend string
 
+// Sync backend changes
+var backendLock sync.RWMutex
+
+// currentBackend returns the current value of the backend in atomic format.
+// (Uses backendLock.RLock)
+func currentBackend() (reply string) {
+	backendLock.RLock()
+	reply = backend
+	backendLock.RUnlock()
+
+	return
+}
+
 func main() {
 	flag.Usage = func() {
 		fmt.Fprintf(os.Stderr, "usage: seamless LISTEN_PORT BACKEND\n")
 		if conn == nil {
 			die("accept failed: %v", err)
 		}
-		go forward(conn, backend)
+		go forward(conn, currentBackend())
 	}
 }
 
 		http.Error(w, msg, http.StatusBadRequest)
 		return
 	}
+
+	backendLock.Lock()
 	backend = newBackend
+	backendLock.Unlock()
 	currentHandler(w, req)
 }