I have a HTTP handler which receives a parameter from query. I don't want to run this handler concurrently for the same query parameter i.e. only one goroutine should be running at a point in time.
This was my idea:
import "sync"
import "fmt"
var safeMap = sync.Map{}
func handler(c) {
_, loaded := safeMap.LoadOrStore(c.param, 1) // loaded is true if value was loaded and false if stored
fmt.Println(loaded)
if loaded {
c.JSON(http.StatusLocked, "locked")
return
}
go doWork(c.param)
safeMap.Delete(c.param)
c.JSON(http.StatusOK, "done")
}
But, whenever I send two concurrent requests loaded
is false for both and both requests are processed. Why?