fix
All checks were successful
Deploy Dev / Build (pull_request) Successful in 38s
Deploy Dev / Push (pull_request) Successful in 24s
Deploy Dev / Deploy dev (pull_request) Successful in 7s

This commit is contained in:
Egor Matveev 2025-01-06 13:33:28 +03:00
parent 64bee3c59f
commit bc411ea644
2 changed files with 10 additions and 13 deletions

View File

@ -1,11 +1,9 @@
package routers
import (
"fmt"
"net/http"
tasks "queues-go/app/storage/mongo/collections"
"queues-go/app/storage/redis"
"time"
"sync"
)
type TaskResponse struct {
@ -18,17 +16,16 @@ type TakeResponse struct {
Task *TaskResponse `json:"task"`
}
var MutexMap map[string]*sync.Mutex
func Take(r *http.Request) (interface{}, int) {
queue := r.Header.Get("queue")
mutex := redis.Sync.NewMutex(fmt.Sprintf("lock_queues_%s", queue))
for {
err := mutex.Lock()
if err != nil {
time.Sleep(time.Millisecond * 5)
} else {
break
}
mutex, ok := MutexMap[queue]
if !ok {
mutex = &sync.Mutex{}
MutexMap[queue] = mutex
}
mutex.Lock()
task, err := tasks.Take(queue)
mutex.Unlock()
if err != nil {

View File

@ -6,7 +6,7 @@ import (
"net/http"
"queues-go/app/routers"
client "queues-go/app/storage/mongo"
"queues-go/app/storage/redis"
"sync"
"time"
)
@ -31,7 +31,7 @@ func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.Response
func main() {
client.Connect()
redis.Connect()
routers.MutexMap = make(map[string]*sync.Mutex)
http.HandleFunc("/api/v1/take", handlerWrapper(routers.Take))
http.HandleFunc("/api/v1/finish", handlerWrapper(routers.Finish))
http.HandleFunc("/api/v1/put", handlerWrapper(routers.Put))