Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,28 @@ func (m *Mutex) lockContext(ctx context.Context, tries int) error {
}

start := time.Now()
adjustedTTL := time.Duration(float64(m.expiry) * m.timeoutFactor)

n, err := func() (int, error) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(int64(float64(m.expiry)*m.timeoutFactor)))
ctx, cancel := context.WithTimeout(ctx, adjustedTTL)
defer cancel()
return m.actOnPoolsAsync(func(pool redis.Pool) (bool, error) {
return m.acquire(ctx, pool, value)
})
}()

now := time.Now()
until := now.Add(m.expiry - now.Sub(start) - time.Duration(int64(float64(m.expiry)*m.driftFactor)))
until := now.Add(m.expiry - now.Sub(start) - adjustedTTL)
if n >= m.quorum && now.Before(until) {
m.value = value
m.until = until
return nil
}
_, _ = func() (int, error) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(int64(float64(m.expiry)*m.timeoutFactor)))

func() {
ctx, cancel := context.WithTimeout(ctx, adjustedTTL)
defer cancel()
return m.actOnPoolsAsync(func(pool redis.Pool) (bool, error) {
m.actOnPoolsAsync(func(pool redis.Pool) (bool, error) {
return m.release(ctx, pool, value)
})
}()
Expand Down Expand Up @@ -304,11 +306,11 @@ func (m *Mutex) actOnPoolsAsync(actFn func(redis.Pool) (bool, error)) (int, erro

ch := make(chan result, len(m.pools))
for node, pool := range m.pools {
go func(node int, pool redis.Pool) {
go func() {
r := result{node: node}
r.statusOK, r.err = actFn(pool)
ch <- r
}(node, pool)
}()
}

var (
Expand Down