Reputation: 523
I write a piece of code to record the num of request.
package main
import (
"log"
"net/http"
"runtime"
"sync/atomic"
)
var count int32 = 0
func test(w http.ResponseWriter, r *http.Request) {
count = atomic.LoadInt32(&count)
atomic.AddInt32(&count, 1)
log.Println("count:", count)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
http.HandleFunc("/", test)
http.ListenAndServe(":8080", nil)
}
I have considered the condition of concurrency, so I use the atomic pacakge. I test the code via the apache ab tool
ab -c 400 -n 1000 http://localhost:8080/
the result is correct: result However, someone said he got the 1004 or other number on his computer, I have tested many times of the code, but the result is correct on my computer, is there wrong with my way? I am new to go, Thanks in advance.
Upvotes: 1
Views: 3483
Reputation:
You are using the sync/atomic
package incorrectly. If you have an atomic variable, ALL reading and writing must be done using the atomic functions.
Here is your code fixed so that the count
variable is not written to or read from non-atomically:
package main
import (
"log"
"net/http"
"runtime"
"sync/atomic"
)
var count int32
func test(w http.ResponseWriter, r *http.Request) {
currentCount := atomic.LoadInt32(&count)
atomic.AddInt32(&count, 1)
log.Println("count:", currentCount)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
http.HandleFunc("/", test)
http.ListenAndServe(":8080", nil)
}
Upvotes: 3