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

os/gdlock: new distributed lock feature draft #4065

Open
gqcn opened this issue Dec 19, 2024 · 0 comments
Open

os/gdlock: new distributed lock feature draft #4065

gqcn opened this issue Dec 19, 2024 · 0 comments
Labels
discuss We need discuss to make decision. feature planned This issue/proposal is planned into our next steps.

Comments

@gqcn
Copy link
Member

gqcn commented Dec 19, 2024

Is your feature request related to a problem?

Option Yes

Describe the solution you'd like

We are considering adding distributed lock interface design to the framework's main library, while implementing specific implementations in community components. The community components can provide distributed lock implementations based on Redis and MySQL.

Describe alternatives you've considered

For the distributed lock interface definition, here's an initial proposal:

// Package gdlock provides distributed locking functionality.
package gdlock

import (
    "context"
    "time"
)

// DistributedLock defines the interface for distributed lock operations.
// It provides methods for acquiring and managing distributed locks across multiple processes/servers.
type DistributedLock interface {
    // Lock acquires a lock with the given key and TTL.
    // It blocks until the lock is acquired or the context is canceled.
    Lock(ctx context.Context, key string, ttl time.Duration, opts LockOptions) (LockInstance, error)

    // LockFunc acquires a lock and executes the provided function within the lock's scope.
    // The lock is automatically released after the function execution.
    LockFunc(ctx context.Context, key string, ttl time.Duration, opts LockOptions, f func(ctx context.Context) error) (LockInstance, error)

    // TryLock attempts to acquire a lock without blocking.
    // It returns immediately if the lock is already held by another process.
    TryLock(ctx context.Context, key string, ttl time.Duration, opts LockOptions) (LockInstance, error)

    // TryLockFunc attempts to acquire a lock and execute the provided function if successful.
    // It returns immediately without executing the function if the lock is already held.
    TryLockFunc(ctx context.Context, key string, ttl time.Duration, opts LockOptions, f func(ctx context.Context) error) (LockInstance, error)
}

// LockInstance represents an acquired lock instance.
// It provides methods for managing the lifecycle of an acquired lock.
type LockInstance interface {
    // Unlock releases the acquired lock.
    Unlock(ctx context.Context) error

    // Refresh extends the TTL of the lock.
    Refresh(ctx context.Context, newTTL time.Duration) error

    // GetTTL retrieves the remaining time-to-live for the lock.
    GetTTL(ctx context.Context) (time.Duration, error)
}

// LockOptions defines the configuration options for lock acquisition.
type LockOptions struct {
    // Reason provides a descriptive reason for acquiring the lock.
    // This is useful for debugging and monitoring purposes.
    Reason string
}

Additional

No response

@gqcn gqcn added feature planned This issue/proposal is planned into our next steps. discuss We need discuss to make decision. labels Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss We need discuss to make decision. feature planned This issue/proposal is planned into our next steps.
Projects
None yet
Development

No branches or pull requests

1 participant