Skip to content

Commit

Permalink
'sdk-ify' the distribute lock implementations
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Lamarre <alex7285@gmail.com>
  • Loading branch information
alexandreLamarre committed Sep 19, 2024
1 parent 66a223b commit b6b3a8e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: [alexandreLamarre]
28 changes: 28 additions & 0 deletions sdk/dlock/etcd/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package etcd

import (
"log/slog"

"github.com/alexandreLamarre/dlock/internal/lock/backend/etcd"
"github.com/alexandreLamarre/dlock/pkg/lock"
clientv3 "go.etcd.io/etcd/client/v3"
"go.opentelemetry.io/otel/trace"
)

func NewLock(
lg *slog.Logger,
client *clientv3.Client,
prefix, key string,
options *lock.LockOptions,
) lock.Lock {
return etcd.NewEtcdLock(lg, client, prefix, key, options)
}

func NewLockManager(
lg *slog.Logger,
client *clientv3.Client,
prefix string,
tracer trace.Tracer,
) lock.LockManager {
return etcd.NewEtcdLockManager(client, prefix, tracer, lg)
}
30 changes: 30 additions & 0 deletions sdk/dlock/jetstream/jetstream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package jetstream

import (
"context"
"log/slog"

"github.com/alexandreLamarre/dlock/internal/lock/backend/jetstream"
"github.com/alexandreLamarre/dlock/pkg/lock"
"github.com/nats-io/nats.go"
"go.opentelemetry.io/otel/trace"
)

func NewLock(
lg *slog.Logger,
js nats.JetStreamContext,
prefix, key string,
options *lock.LockOptions,
) lock.Lock {
return jetstream.NewLock(js, prefix, key, lg, options)
}

func NewLockManager(
ctx context.Context,
lg *slog.Logger,
js nats.JetStreamContext,
prefix string,
tracer trace.Tracer,
) lock.LockManager {
return jetstream.NewLockManager(ctx, js, prefix, tracer, lg)
}
41 changes: 41 additions & 0 deletions sdk/dlock/redis/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package redis

import (
"context"
"log/slog"

rl "github.com/alexandreLamarre/dlock/internal/lock/backend/redis"
"github.com/alexandreLamarre/dlock/pkg/lock"
"github.com/go-redsync/redsync/v4/redis"
)

func NewLock(
pools []redis.Pool,
quorum int,
prefix, key string,
lg *slog.Logger,
opts *lock.LockOptions,
) lock.Lock {
return rl.NewLock(
pools,
quorum,
prefix,
key,
lg,
opts,
)
}

func NewLockManager(
ctx context.Context,
prefix string,
pools []redis.Pool,
lg *slog.Logger,
) lock.LockManager {
return rl.NewLockManager(
ctx,
prefix,
pools,
lg,
)
}
5 changes: 5 additions & 0 deletions sdk/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Provides an SDK for embedding distributed lock mechanisms without the need to run a dedicated server.

// **Note:** It is recommended to use the server binary for production environments, as it
// is guaranteed to handle synchronization edge cases that may not be fully addressed when using the plain SDK.
package sdk

0 comments on commit b6b3a8e

Please sign in to comment.