Skip to content

Commit

Permalink
fix(redis): fix handling unable to acquire redis lock
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Mar 15, 2022
1 parent 7edcfc6 commit 7afdf6f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
30 changes: 30 additions & 0 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM ghcr.io/aldor007/mort-base

ENV GOLANG_VERSION 1.16.6
ARG TARGETARCH amd64
ARG TAG 'dev'
ARG COMMIT "master"
ARG DATE "now"

ENV GOLANG_DOWNLOAD_URL https://golang.org/dl/go$GOLANG_VERSION.linux-$TARGETARCH.tar.gz

RUN curl -fsSL --insecure "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \
&& tar -C /usr/local -xzf golang.tar.gz \
&& rm golang.tar.gz

ENV WORKDIR /workspace
ENV PATH /usr/local/go/bin:$PATH


WORKDIR $WORKDIR
COPY go.mod ./
COPY go.sum ./
RUN go mod download

COPY cmd/ $WORKDIR/cmd
COPY .godir ${WORKDIR}/.godir
COPY configuration/ ${WORKDIR}/configuration
COPY etc/ ${WORKDIR}/etc
COPY pkg/ ${WORKDIR}/pkg
COPY scripts/ ${WORKDIR}/scripts
COPY Makefile ${WORKDIR}/Makefile
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
tests:
build:
context: .
dockerfile: Dockerfile.base
dockerfile: Dockerfile.test
args:
- TAG=dev
- COMMIT=master
Expand Down
7 changes: 6 additions & 1 deletion pkg/lock/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ func (m *RedisLock) Lock(ctx context.Context, key string) (result LockResult, ok
lock.Refresh(ctx, time.Millisecond*500, nil)
} else {
lock, err := m.client.Obtain(ctx, key, 60*time.Second, nil)
if err != nil {
if err == redislock.ErrNotObtained {
result, ok = m.memoryLock.Lock(ctx, key)
ok = false
return

} else if err != nil {
result.Error = err
return
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/lock/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func TestRedisLock_Lock(t *testing.T) {
assert.Nil(t, c.ResponseChan, "shouldn't return channel")
assert.Nil(t, c.Error, "error should be nil")

mock.ClearExpect()
mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(false)
resChan, lock := l.Lock(ctx, key)

assert.False(t, lock, "Shouldn't acquire lock")
Expand Down Expand Up @@ -61,7 +63,7 @@ func TestRedisLock_NotifyAndReleaseWhenError(t *testing.T) {
assert.Nil(t, c.Error, "error should be nil")

mock.ClearExpect()
mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(true)
mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(false)
result, lock := l.Lock(ctx, key)

assert.False(t, lock, "Shouldn't acquire lock")
Expand Down Expand Up @@ -96,7 +98,7 @@ func TestRedisLock_NotifyAndRelease(t *testing.T) {
assert.Nil(t, c.ResponseChan, "shouldn't return channel")

mock.ClearExpect()
mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(true)
mock.Regexp().ExpectSetNX(key, `[a-zA-Z0-9]+`, 60*time.Second).SetVal(false)
result, lock := l.Lock(ctx, key)

assert.False(t, lock, "Shouldn't acquire lock")
Expand Down

0 comments on commit 7afdf6f

Please sign in to comment.