-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
50 lines (43 loc) · 1.02 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package pgxmutex
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
)
// Option is a functional option type for configuring Mutex.
type Option func(*Mutex) error
// WithConnStr creates new PGX connection from a connection string.
func WithConnStr(connStr string) Option {
return func(m *Mutex) error {
conn, err := pgx.Connect(context.Background(), connStr)
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
m.conn = conn
return nil
}
}
// WithConn sets the custom DB connection.
func WithConn(conn conn) Option {
return func(m *Mutex) error {
m.conn = conn
return nil
}
}
// WithResourceID sets the lock ID for advisory locking.
func WithResourceID(id int64) Option {
return func(m *Mutex) error {
if id == 0 {
return fmt.Errorf("resource ID must be provided")
}
m.so = getSingleton(id)
return nil
}
}
// WithContext sets a custom context for the Mutex operations.
func WithContext(ctx context.Context) Option {
return func(m *Mutex) error {
m.ctx = ctx
return nil
}
}