fix
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
tasks "queues-go/app/storage/mongo/collections"
|
||||
"queues-go/app/storage/redis"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TaskResponse struct {
|
||||
@@ -17,7 +20,17 @@ type TakeResponse struct {
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
task, err := tasks.Take(queue)
|
||||
mutex.Unlock()
|
||||
if err != nil {
|
||||
return nil, http.StatusInternalServerError
|
||||
}
|
||||
|
||||
28
app/storage/redis/locks.go
Normal file
28
app/storage/redis/locks.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"queues-go/app/utils"
|
||||
|
||||
"github.com/go-redsync/redsync/v4"
|
||||
"github.com/go-redsync/redsync/v4/redis/goredis/v9"
|
||||
goredislib "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var Sync redsync.Redsync
|
||||
|
||||
func Connect() {
|
||||
client := goredislib.NewClient(getOptions())
|
||||
pool := goredis.NewPool(client)
|
||||
Sync = *redsync.New(pool)
|
||||
}
|
||||
|
||||
func getOptions() *goredislib.Options {
|
||||
addr := fmt.Sprintf("%s:6379", utils.GetEnv("REDIS_HOST", "localhost"))
|
||||
password := utils.GetEnv("REDIS_PASSWORD", "password")
|
||||
|
||||
return &goredislib.Options{
|
||||
Addr: addr,
|
||||
Password: password,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user