Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
25baa5ff
Commit
25baa5ff
authored
1 year ago
by
Jano Hendriks
Browse files
Options
Downloads
Patches
Plain Diff
Add redis function to set a lock on a specific key
parent
b40025b6
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
redis/redis.go
+58
-0
58 additions, 0 deletions
redis/redis.go
with
58 additions
and
0 deletions
redis/redis.go
+
58
−
0
View file @
25baa5ff
...
...
@@ -27,6 +27,13 @@ type ClientWithHelpers struct {
Available
bool
}
type
SetLockKeyOptions
struct
{
Value
*
string
MaxRetries
*
int
RetryInterval
*
time
.
Duration
FallbackResult
*
bool
}
func
GetRedisClient
(
isDebug
bool
)
*
ClientWithHelpers
{
if
redisClient
!=
nil
&&
redisClient
.
IsConnected
()
{
return
redisClient
...
...
@@ -232,3 +239,54 @@ func (r ClientWithHelpers) RateLimit(key string, limitFn func(int) redis_rate.Li
return
res
.
Allowed
==
1
,
nil
}
}
// SetLockKey attempts to set a lock on the specified key.
func
(
r
ClientWithHelpers
)
SetLockKey
(
key
string
,
expiration
time
.
Duration
,
lockOptions
...
SetLockKeyOptions
)
(
success
bool
,
err
error
)
{
fallbackResult
:=
false
if
len
(
lockOptions
)
>
0
&&
lockOptions
[
0
]
.
FallbackResult
!=
nil
{
fallbackResult
=
*
lockOptions
[
0
]
.
FallbackResult
}
if
!
r
.
IsConnected
()
{
return
fallbackResult
,
errors
.
Error
(
"Redis is not connected"
)
}
value
:=
"1"
retries
:=
0
retryInterval
:=
100
*
time
.
Millisecond
if
len
(
lockOptions
)
>
0
{
// Only use the values that were set
if
lockOptions
[
0
]
.
Value
!=
nil
{
value
=
*
lockOptions
[
0
]
.
Value
}
if
lockOptions
[
0
]
.
MaxRetries
!=
nil
{
retries
=
*
lockOptions
[
0
]
.
MaxRetries
}
if
lockOptions
[
0
]
.
RetryInterval
!=
nil
{
retryInterval
=
*
lockOptions
[
0
]
.
RetryInterval
}
}
for
retries
>=
0
{
success
,
err
=
r
.
Client
.
SetNX
(
ctx
,
key
,
value
,
expiration
)
.
Result
()
if
err
!=
nil
{
logs
.
ErrorWithMsg
(
fmt
.
Sprintf
(
"Error setting lock key %s"
,
key
),
err
)
// Prevent further calls in this execution from trying to connect and also timeout
if
strings
.
HasSuffix
(
err
.
Error
(),
"i/o timeout"
)
{
r
.
Available
=
false
}
return
fallbackResult
,
err
}
if
success
||
retries
==
0
{
break
}
retries
--
time
.
Sleep
(
retryInterval
)
}
return
success
,
nil
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment