Skip to content

os/gdlock: new distributed lock feature draft #4065

Open
@gqcn

Description

@gqcn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussWe need discuss to make decision.featureplannedThis issue/proposal is planned into our next steps.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions