Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function IsLockedTTLWithLimit checks if the key has been incremen… #53

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions candiutils/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ func (r *RedisLocker) IsLockedTTL(key string, TTL time.Duration) bool {
return incr > 1
}

// IsLockedTTLWithLimit checks if the key has been incremented more than the specified limit
// within the given TTL. If the key is being created for the first time, it sets the TTL.
// Example usage: check if a key has been incremented more than 10 times within 1 minute.
func (r *RedisLocker) IsLockedTTLWithLimit(key string, limit int, TTL time.Duration) bool {
Copy link
Member

@agungdwiprasetyo agungdwiprasetyo Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo, better create general method IsLockedWithOption instead add new specific method for new feature.
enhance LockerOptions add new field Limit and set default value to 1 in constructor.

example:

Suggested change
func (r *RedisLocker) IsLockedTTLWithLimit(key string, limit int, TTL time.Duration) bool {
func (r *RedisLocker) IsLockedWithOption(key string, opts ...LockerOption) bool {
conn := r.pool.Get()
defer conn.Close()
lockOpt := r.lockeroptions
for _, opt := range opts {
opt(&lockOpt)
}
lockKey := fmt.Sprintf("%s:%s", r.lockeroptions.Prefix, key)
incr, err := redis.Int64(conn.Do("INCR", lockKey))
if err != nil {
return false
}
withLimit := lockOpt.Limit > 1
if lockOpt.TTL > 0 && !(withLimit && incr == 1) {
conn.Do("EXPIRE", lockKey, int(lockOpt.TTL.Seconds()))
}
return incr > int64(lockOpt.Limit)
}

also applies to IsLockedTTL method

conn := r.pool.Get()
defer conn.Close()

lockKey := fmt.Sprintf("%s:%s", r.lockeroptions.Prefix, key)
incr, err := redis.Int64(conn.Do("INCR", lockKey))
if err != nil {
return false
}

var expireTime time.Duration
if TTL > 0 {
expireTime = TTL
} else {
expireTime = r.lockeroptions.TTL
}

if expireTime > 0 && incr == 1 {
conn.Do("EXPIRE", lockKey, int(expireTime.Seconds()))
}

return incr > int64(limit)
}

func (r *RedisLocker) HasBeenLocked(key string) bool {
conn := r.pool.Get()
defer conn.Close()
Expand Down Expand Up @@ -234,3 +261,6 @@ func (NoopLocker) GetPrefixLocker() string { return "" }

// GetTTLLocker method
func (NoopLocker) GetTTLLocker() time.Duration { return 0 }

// IsLockedTTLWithLimit method
func (NoopLocker) IsLockedTTLWithLimit(string, int, time.Duration) bool { return false }
1 change: 1 addition & 0 deletions codebase/interfaces/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type (
Lock(key string, timeout time.Duration) (unlockFunc func(), err error)
GetPrefixLocker() string
GetTTLLocker() time.Duration
IsLockedTTLWithLimit(key string, limit int, TTL time.Duration) bool
Closer
}
)
32 changes: 32 additions & 0 deletions mocks/candiutils/LockerOption.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion mocks/codebase/interfaces/Locker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.