Skip to content

Commit b0bc8cf

Browse files
authored
Merge pull request #2 from zeriontech/dev-mutex
Added mutex
2 parents 01a1837 + 45f0b77 commit b0bc8cf

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

.github/workflows/cd_manual.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: "CD"
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
TAG:
7+
required: true
8+
description: 'Docker container tag'
9+
default: staging
10+
11+
env:
12+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
13+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
14+
15+
jobs:
16+
build_an_push:
17+
name: Build and Push a docker image
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v2
22+
23+
- name: Set env
24+
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
25+
26+
- name: Configure AWS credentials
27+
uses: aws-actions/configure-aws-credentials@v1
28+
with:
29+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
30+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
31+
aws-region: us-east-1
32+
33+
- name: Login to Amazon ECR
34+
id: login-ecr
35+
uses: aws-actions/amazon-ecr-login@v1
36+
37+
- name: Build, tag, and push image to Amazon ECR
38+
id: build-image
39+
env:
40+
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
41+
ECR_REPOSITORY: sidecache
42+
IMAGE_TAG: ${{ github.event.inputs.TAG || env.RELEASE_VERSION }}
43+
run: |
44+
# Build a docker container and push it to ECR
45+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --build-arg release_version=${IMAGE_TAG} .
46+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
47+
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"

pkg/lock/redis.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package lock
22

33
import (
44
"errors"
5+
"sync"
6+
"time"
7+
58
"github.com/google/uuid"
69
"github.com/zeriontech/sidecache/pkg/cache"
7-
"time"
810
)
911

1012
type RedisLock struct {
11-
redis *cache.RedisRepository
13+
redis *cache.RedisRepository
1214
values map[string]string
15+
mutex sync.Mutex
1316
}
1417

1518
const UnlockScript = `
@@ -25,12 +28,18 @@ func NewRedisLock(redis *cache.RedisRepository) *RedisLock {
2528
}
2629

2730
func (lock *RedisLock) Acquire(key string, ttl time.Duration) error {
31+
lock.mutex.Lock()
32+
defer lock.mutex.Unlock()
33+
2834
val := uuid.NewString()
2935
lock.values[key] = val
3036
return lock.redis.SetNX(key, val, ttl)
3137
}
3238

3339
func (lock *RedisLock) Release(key string) error {
40+
lock.mutex.Lock()
41+
defer lock.mutex.Unlock()
42+
3443
val, ok := lock.values[key]
3544
if !ok {
3645
return errors.New("unknown key")

0 commit comments

Comments
 (0)