Comments (4)
This redislock has no retry strategy by default. Your code looks like it will simply return false if the lock was not acquired. I don't quite understand what you said 'they can obtain the lock for all of them and it continues for next calls.' In theory, four threads grab the lock at the same time, and only one thread will succeed in grabbing the lock. I don't know if you are using postman. The four requests sent by postman at the same time are actually executed serially, that is, B will be executed after A is executed.
You can add one row like this after the 'Obtain'.
fmt.Print(lock) time.sleep(10*time.Second)'
If you can see four threads at the same time print the lock acquired successfully. May be a bug of redislock.
from redislock.
Yeah, sorry for the part I've talked about 'pausing'. What I mean is the lock would failed or process if it is obtainable or not. But whenever I call the API, my lock can be obtained. If I want to wait the lock to be obtained, I will use a loop to wait.
About 4 requests simultaneously, I am using a client written in ReactJS that makes multiple request at a time.
from redislock.
I don't know about gin but I just created a tiny HTTP server and it seems to be doing what it says on the tin :)
package main
import (
"io"
"net/http"
"time"
"github.com/bsm/redislock"
"github.com/go-redis/redis/v8"
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: ":6379",
DB: 9,
})
defer client.Close()
locker := redislock.New(client)
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
lock, err := locker.Obtain(ctx, "file", 5*time.Second, nil)
if err == redislock.ErrNotObtained {
http.Error(rw, "Busy", http.StatusTooManyRequests)
return
} else if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
defer lock.Release(ctx)
select {
case <-ctx.Done():
http.Error(rw, "Gone", http.StatusGone)
return
case <-time.After(200 * time.Millisecond):
// assume request takes 200ms to complete
}
io.WriteString(rw, "OK")
})
http.ListenAndServe(":8080", nil)
}
When I run the client code below, I get:
request #02: 429 "Busy"
request #03: 429 "Busy"
request #01: 200 "OK"
request #05: 429 "Busy"
request #06: 429 "Busy"
request #04: 200 "OK"
request #08: 429 "Busy"
request #09: 429 "Busy"
request #07: 200 "OK"
request #10: 200 "OK"
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"sync"
"time"
)
func main() {
wg := sync.WaitGroup{}
defer wg.Wait()
for i := 0; i < 10; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
time.Sleep(time.Duration(n) * 100 * time.Millisecond)
resp, err := http.Get("http://localhost:8080")
if err != nil {
log.Fatalln(err)
}
b, _ := io.ReadAll(resp.Body)
fmt.Printf("request #%02d: %d %q\n", n, resp.StatusCode, bytes.TrimSpace(b))
}(i + 1)
}
}
from redislock.
Ok tks, the problem is in my code :)
from redislock.
Related Issues (20)
- Allow setting/disabling Obtain deadline in Options HOT 4
- Add context param to all functions and methods that make calls to redis HOT 1
- Please Follow Semantic Import Versioning HOT 2
- Can release the lock when ctx deadline exceeded HOT 1
- the Obtain may be have a problem HOT 1
- Global locker object or local HOT 3
- Two questions about the implementation HOT 1
- A question about ttl HOT 5
- No provision for using KeepTTL option of redis server itself
- redis/v8 support HOT 2
- How to get lock with a key HOT 16
- Doesn't work with redis/v9 HOT 3
- Is it still working on goredis v8? HOT 1
- RedisClient error HOT 8
- README Examples Can't run HOT 1
- please ignore HOT 1
- Should here return `context.Cause(ctx)` or the error warp with `context.Cause(ctx)` when `ctx.Done()` ? HOT 2
- lock.Release() unable to release lock. HOT 3
- panic HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redislock.