-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
39 lines (31 loc) · 1.34 KB
/
utils.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
package utils
import (
"context"
"fmt"
"github.com/stellar/stellar-disbursement-platform-backend/db"
sdpUtils "github.com/stellar/stellar-disbursement-platform-backend/internal/utils"
)
// AcquireAdvisoryLock attempt to acquire an advisory lock on the provided lockKey, returns true if acquired, or false
// not.
func AcquireAdvisoryLock(ctx context.Context, dbConnectionPool db.DBConnectionPool, lockKey int) (bool, error) {
tssAdvisoryLockAcquired := false
sqlQuery := "SELECT pg_try_advisory_lock($1)"
err := dbConnectionPool.QueryRowxContext(ctx, sqlQuery, lockKey).Scan(&tssAdvisoryLockAcquired)
if err != nil {
return false, fmt.Errorf("querying pg_try_advisory_lock(%v): %w", lockKey, err)
}
return tssAdvisoryLockAcquired, nil
}
type PrivateKeyEncrypter interface {
Encrypt(message string, passphrase string) (string, error)
Decrypt(message string, passphrase string) (string, error)
}
type DefaultPrivateKeyEncrypter struct{}
func (e *DefaultPrivateKeyEncrypter) Encrypt(message, passphrase string) (string, error) {
return sdpUtils.Encrypt(message, passphrase)
}
func (e *DefaultPrivateKeyEncrypter) Decrypt(message, passphrase string) (string, error) {
return sdpUtils.Decrypt(message, passphrase)
}
// Making sure that DefaultPrivateKeyEncrypter implements PrivateKeyEncrypter
var _ PrivateKeyEncrypter = (*DefaultPrivateKeyEncrypter)(nil)