mlock
empowers users to enforce locks on the same critical section based on different types, providing a flexible and effective way to control access and maintain concurrency in diverse scenarios.
- Obtain locks based on different keys.
- Automatically cleans up unused locks at regular intervals.
To use mlock
in your Golang project, you can simply run:
go get -u github.com/kimbbakar/mlock
import "github.com/kimbbakar/mlock"
Before using mlock, it's recommended to set up a cleanup routine to remove unused locks periodically. This can be done using the KeepClean function:
interval := 30 * time.Minute
mlock.KeepClean(&interval)
This sets up a cleanup routine to run every 30 minutes. You can customize the cleanup interval as needed.
To obtain a lock based on a key, use the Lock function:
mlock.Lock("your_key_here")
defer mlock.Unlock("your_key_here")
package main
import (
"fmt"
"time"
"github.com/kimbbakar/mlock"
)
func main() {
// Set up cleanup routine
interval := 30 * time.Minute
mlock.KeepClean(&interval)
// Obtain a lock
mlock.Lock("example_key")
defer mlock.Unlock("example_key")
// Your critical section here
fmt.Println("Lock acquired, performing operations...")
}
Contributions are welcome! If you find any issues or have suggestions for improvement, feel free to open an issue or create a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.