Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
pauluswi authored Nov 16, 2024
1 parent 364eadd commit ca721f0
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,51 @@ This approach uses Redis to simulate distributed locking. Each critical section
### **Mock**
- To make a simple ready-to-go process, I utilize mock for redis to simulate redis without run real Redis instance.

### **Code**
- Each transaction is processed in a separate goroutine using the go keyword.
- A ```sync.WaitGroup``` ensures the main function waits for all transactions to complete.
- Redis SET with NX ensures that the operation is atomic, meaning the lock is created and has a TTL in a single operation.

```go
// MockRedis simulates a Redis client with basic lock functionality
type MockRedis struct {
data map[string]string
mu sync.Mutex
}
```
```go
// RedisLock represents a distributed lock

type RedisLock struct {
client *MockRedis
key string
value string
}

// AcquireLock tries to acquire the lock
func (lock *RedisLock) AcquireLock(ttl time.Duration) (bool, error) {
return lock.client.SetNX(lock.key, lock.value, ttl)
}

// ReleaseLock releases the lock
func (lock *RedisLock) ReleaseLock() error {
val, err := lock.client.Get(lock.key)
if err != nil {
return fmt.Errorf("lock not found")
}

// Ensure the lock is released by the process that acquired it
if val == lock.value {
_, err = lock.client.Del(lock.key)
if err != nil {
return err
}
}
return nil
}
```


### Output
```
Distributed Locks Output:
Expand Down

0 comments on commit ca721f0

Please sign in to comment.