A lightweight, in-memory resource locking package to prevent concurrent access to resources.
// Lock a user resource
if !gatekeeper.TryLock(gatekeeper.ResourceUser, "user123") {
return errors.New("user is already being processed")
}
defer gatekeeper.ReleaseLock(gatekeeper.ResourceUser, "user123")
// Process the user...// Create a middleware that locks user resources
userLock := gatekeeper.Middleware(gatekeeper.ResourceUser, func(c *fiber.Ctx) string {
return utils.GetUserId(c)
})
// Apply middleware to routes
app.Post("/users", userLock, controllers.CreateUser)// Use default settings (no configuration needed)
gatekeeper.Setup()
// Or customize specific options
gatekeeper.Setup(
gatekeeper.WithLockTimeout(10 * time.Second),
gatekeeper.WithErrorMessage("Resource busy"),
)Available options:
WithLockTimeout(duration)- How long locks remain activeWithEnabled(bool)- Toggle locking on/offWithErrorStatus(int)- HTTP status for locked resourcesWithErrorMessage(string)- Error messageWithErrorCode(string)- Error code
// Define your own resource types
const ResourceCart = "cart"
// Use with gatekeeper
if !gatekeeper.TryLock(ResourceCart, cartId) {
return errors.New("cart is locked")
}This is an in-memory locking mechanism for single-instance applications. For distributed locking, consider solutions like Redis locks.