fix
This commit is contained in:
parent
64bee3c59f
commit
bc411ea644
@ -1,11 +1,9 @@
|
|||||||
package routers
|
package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
tasks "queues-go/app/storage/mongo/collections"
|
tasks "queues-go/app/storage/mongo/collections"
|
||||||
"queues-go/app/storage/redis"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TaskResponse struct {
|
type TaskResponse struct {
|
||||||
@ -18,17 +16,16 @@ type TakeResponse struct {
|
|||||||
Task *TaskResponse `json:"task"`
|
Task *TaskResponse `json:"task"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var MutexMap map[string]*sync.Mutex
|
||||||
|
|
||||||
func Take(r *http.Request) (interface{}, int) {
|
func Take(r *http.Request) (interface{}, int) {
|
||||||
queue := r.Header.Get("queue")
|
queue := r.Header.Get("queue")
|
||||||
mutex := redis.Sync.NewMutex(fmt.Sprintf("lock_queues_%s", queue))
|
mutex, ok := MutexMap[queue]
|
||||||
for {
|
if !ok {
|
||||||
err := mutex.Lock()
|
mutex = &sync.Mutex{}
|
||||||
if err != nil {
|
MutexMap[queue] = mutex
|
||||||
time.Sleep(time.Millisecond * 5)
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
mutex.Lock()
|
||||||
task, err := tasks.Take(queue)
|
task, err := tasks.Take(queue)
|
||||||
mutex.Unlock()
|
mutex.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
4
main.go
4
main.go
@ -6,7 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"queues-go/app/routers"
|
"queues-go/app/routers"
|
||||||
client "queues-go/app/storage/mongo"
|
client "queues-go/app/storage/mongo"
|
||||||
"queues-go/app/storage/redis"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.Response
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
client.Connect()
|
client.Connect()
|
||||||
redis.Connect()
|
routers.MutexMap = make(map[string]*sync.Mutex)
|
||||||
http.HandleFunc("/api/v1/take", handlerWrapper(routers.Take))
|
http.HandleFunc("/api/v1/take", handlerWrapper(routers.Take))
|
||||||
http.HandleFunc("/api/v1/finish", handlerWrapper(routers.Finish))
|
http.HandleFunc("/api/v1/finish", handlerWrapper(routers.Finish))
|
||||||
http.HandleFunc("/api/v1/put", handlerWrapper(routers.Put))
|
http.HandleFunc("/api/v1/put", handlerWrapper(routers.Put))
|
||||||
|
Loading…
Reference in New Issue
Block a user