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

physical: use permitpool from go-secure-stdlib #29331

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ require (
github.com/hashicorp/go-secure-stdlib/nonceutil v0.1.0
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.8
github.com/hashicorp/go-secure-stdlib/password v0.1.1
github.com/hashicorp/go-secure-stdlib/permitpool v1.0.0
github.com/hashicorp/go-secure-stdlib/reloadutil v0.1.1
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2
github.com/hashicorp/go-secure-stdlib/tlsutil v0.1.3
Expand Down Expand Up @@ -204,7 +205,7 @@ require (
github.com/sasha-s/go-deadlock v0.3.5
github.com/sethvargo/go-limiter v0.7.1
github.com/shirou/gopsutil/v3 v3.22.6
github.com/stretchr/testify v1.9.0
github.com/stretchr/testify v1.10.0
github.com/tink-crypto/tink-go/v2 v2.2.0
go.etcd.io/bbolt v1.4.0-beta.0
go.etcd.io/etcd/client/pkg/v3 v3.5.17
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,8 @@ github.com/hashicorp/go-secure-stdlib/parseutil v0.1.8 h1:iBt4Ew4XEGLfh6/bPk4rSY
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.8/go.mod h1:aiJI+PIApBRQG7FZTEBx5GiiX+HbOHilUdNxUZi4eV0=
github.com/hashicorp/go-secure-stdlib/password v0.1.1 h1:6JzmBqXprakgFEHwBgdchsjaA9x3GyjdI568bXKxa60=
github.com/hashicorp/go-secure-stdlib/password v0.1.1/go.mod h1:9hH302QllNwu1o2TGYtSk8I8kTAN0ca1EHpwhm5Mmzo=
github.com/hashicorp/go-secure-stdlib/permitpool v1.0.0 h1:U6y5MXGiDVOOtkWJ6o/tu1TxABnI0yKTQWJr7z6BpNk=
github.com/hashicorp/go-secure-stdlib/permitpool v1.0.0/go.mod h1:ecDb3o+8D4xtP0nTCufJaAVawHavy5M2eZ64Nq/8/LM=
github.com/hashicorp/go-secure-stdlib/plugincontainer v0.4.1 h1:JY+zGg8gOmslwif1fiCqT5Hu1SikLZQcHkmQhCoA9gY=
github.com/hashicorp/go-secure-stdlib/plugincontainer v0.4.1/go.mod h1:jW3KCTvdPyAdVecOUwiiO2XaYgUJ/isigt++ISkszkY=
github.com/hashicorp/go-secure-stdlib/reloadutil v0.1.1 h1:SMGUnbpAcat8rIKHkBPjfv81yC46a8eCNZ2hsR2l1EI=
Expand Down Expand Up @@ -2159,8 +2161,9 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tencentcloud/tencentcloud-sdk-go v1.0.162 h1:8fDzz4GuVg4skjY2B0nMN7h6uN61EDVkuLyI2+qGHhI=
github.com/tencentcloud/tencentcloud-sdk-go v1.0.162/go.mod h1:asUz5BPXxgoPGaRgZaVm1iGcUAuHyYUo1nXqKa83cvI=
github.com/tilinna/clock v1.0.2/go.mod h1:ZsP7BcY7sEEz7ktc0IVy8Us6boDrK8VradlKRUGfOao=
Expand Down
21 changes: 15 additions & 6 deletions physical/alicloudoss/alicloudoss.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/armon/go-metrics"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-secure-stdlib/permitpool"
"github.com/hashicorp/vault/sdk/physical"
)

Expand All @@ -39,7 +40,7 @@ type AliCloudOSSBackend struct {
bucket string
client *oss.Client
logger log.Logger
permitPool *physical.PermitPool
permitPool *permitpool.Pool
}

// NewAliCloudOSSBackend constructs an OSS backend using a pre-existing
Expand Down Expand Up @@ -113,7 +114,7 @@ func NewAliCloudOSSBackend(conf map[string]string, logger log.Logger) (physical.
client: client,
bucket: bucket,
logger: logger,
permitPool: physical.NewPermitPool(maxParInt),
permitPool: permitpool.New(maxParInt),
}
return a, nil
}
Expand All @@ -122,7 +123,9 @@ func NewAliCloudOSSBackend(conf map[string]string, logger log.Logger) (physical.
func (a *AliCloudOSSBackend) Put(ctx context.Context, entry *physical.Entry) error {
defer metrics.MeasureSince([]string{AlibabaMetricKey, "put"}, time.Now())

a.permitPool.Acquire()
if err := a.permitPool.Acquire(ctx); err != nil {
return err
}
Comment on lines +126 to +128
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The permitpool now requires a context argument, so that it can unblock in the case of context cancellations. I have weaved this into the code using existing contexts where possible, otherwise using context.Background().

defer a.permitPool.Release()

bucket, err := a.client.Bucket(a.bucket)
Expand All @@ -137,7 +140,9 @@ func (a *AliCloudOSSBackend) Put(ctx context.Context, entry *physical.Entry) err
func (a *AliCloudOSSBackend) Get(ctx context.Context, key string) (*physical.Entry, error) {
defer metrics.MeasureSince([]string{AlibabaMetricKey, "get"}, time.Now())

a.permitPool.Acquire()
if err := a.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer a.permitPool.Release()

bucket, err := a.client.Bucket(a.bucket)
Expand Down Expand Up @@ -174,7 +179,9 @@ func (a *AliCloudOSSBackend) Get(ctx context.Context, key string) (*physical.Ent
func (a *AliCloudOSSBackend) Delete(ctx context.Context, key string) error {
defer metrics.MeasureSince([]string{AlibabaMetricKey, "delete"}, time.Now())

a.permitPool.Acquire()
if err := a.permitPool.Acquire(ctx); err != nil {
return err
}
defer a.permitPool.Release()

bucket, err := a.client.Bucket(a.bucket)
Expand All @@ -190,7 +197,9 @@ func (a *AliCloudOSSBackend) Delete(ctx context.Context, key string) error {
func (a *AliCloudOSSBackend) List(ctx context.Context, prefix string) ([]string, error) {
defer metrics.MeasureSince([]string{AlibabaMetricKey, "list"}, time.Now())

a.permitPool.Acquire()
if err := a.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer a.permitPool.Release()

keys := []string{}
Expand Down
21 changes: 15 additions & 6 deletions physical/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
"github.com/armon/go-metrics"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-secure-stdlib/permitpool"
"github.com/hashicorp/go-secure-stdlib/strutil"
"github.com/hashicorp/vault/sdk/physical"
)
Expand All @@ -37,7 +38,7 @@ const (
type AzureBackend struct {
container *azblob.ContainerURL
logger log.Logger
permitPool *physical.PermitPool
permitPool *permitpool.Pool
}

// Verify AzureBackend satisfies the correct interfaces
Expand Down Expand Up @@ -191,7 +192,7 @@ func NewAzureBackend(conf map[string]string, logger log.Logger) (physical.Backen
a := &AzureBackend{
container: &containerURL,
logger: logger,
permitPool: physical.NewPermitPool(maxParInt),
permitPool: permitpool.New(maxParInt),
}
return a, nil
}
Expand Down Expand Up @@ -233,7 +234,9 @@ func (a *AzureBackend) Put(ctx context.Context, entry *physical.Entry) error {
return fmt.Errorf("value is bigger than the current supported limit of 4MBytes")
}

a.permitPool.Acquire()
if err := a.permitPool.Acquire(ctx); err != nil {
return err
}
defer a.permitPool.Release()

blobURL := a.container.NewBlockBlobURL(entry.Key)
Expand All @@ -248,7 +251,9 @@ func (a *AzureBackend) Put(ctx context.Context, entry *physical.Entry) error {
func (a *AzureBackend) Get(ctx context.Context, key string) (*physical.Entry, error) {
defer metrics.MeasureSince([]string{"azure", "get"}, time.Now())

a.permitPool.Acquire()
if err := a.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer a.permitPool.Release()

blobURL := a.container.NewBlockBlobURL(key)
Expand Down Expand Up @@ -285,7 +290,9 @@ func (a *AzureBackend) Get(ctx context.Context, key string) (*physical.Entry, er
func (a *AzureBackend) Delete(ctx context.Context, key string) error {
defer metrics.MeasureSince([]string{"azure", "delete"}, time.Now())

a.permitPool.Acquire()
if err := a.permitPool.Acquire(ctx); err != nil {
return err
}
defer a.permitPool.Release()

blobURL := a.container.NewBlockBlobURL(key)
Expand All @@ -310,7 +317,9 @@ func (a *AzureBackend) Delete(ctx context.Context, key string) error {
func (a *AzureBackend) List(ctx context.Context, prefix string) ([]string, error) {
defer metrics.MeasureSince([]string{"azure", "list"}, time.Now())

a.permitPool.Acquire()
if err := a.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer a.permitPool.Release()

var keys []string
Expand Down
25 changes: 18 additions & 7 deletions physical/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/cockroachdb/cockroach-go/v2/crdb"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-secure-stdlib/permitpool"
"github.com/hashicorp/go-secure-stdlib/strutil"
"github.com/hashicorp/vault/sdk/physical"
_ "github.com/jackc/pgx/v4/stdlib"
Expand Down Expand Up @@ -44,7 +45,7 @@ type CockroachDBBackend struct {
rawHAStatements map[string]string
haStatements map[string]*sql.Stmt
logger log.Logger
permitPool *physical.PermitPool
permitPool *permitpool.Pool
haEnabled bool
}

Expand Down Expand Up @@ -142,7 +143,7 @@ func NewCockroachDBBackend(conf map[string]string, logger log.Logger) (physical.
},
haStatements: make(map[string]*sql.Stmt),
logger: logger,
permitPool: physical.NewPermitPool(maxParInt),
permitPool: permitpool.New(maxParInt),
haEnabled: haEnabled,
}

Expand Down Expand Up @@ -176,7 +177,9 @@ func (c *CockroachDBBackend) prepare(statementMap map[string]*sql.Stmt, name, qu
func (c *CockroachDBBackend) Put(ctx context.Context, entry *physical.Entry) error {
defer metrics.MeasureSince([]string{"cockroachdb", "put"}, time.Now())

c.permitPool.Acquire()
if err := c.permitPool.Acquire(ctx); err != nil {
return err
}
defer c.permitPool.Release()

_, err := c.statements["put"].Exec(entry.Key, entry.Value)
Expand All @@ -190,7 +193,9 @@ func (c *CockroachDBBackend) Put(ctx context.Context, entry *physical.Entry) err
func (c *CockroachDBBackend) Get(ctx context.Context, key string) (*physical.Entry, error) {
defer metrics.MeasureSince([]string{"cockroachdb", "get"}, time.Now())

c.permitPool.Acquire()
if err := c.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer c.permitPool.Release()

var result []byte
Expand All @@ -213,7 +218,9 @@ func (c *CockroachDBBackend) Get(ctx context.Context, key string) (*physical.Ent
func (c *CockroachDBBackend) Delete(ctx context.Context, key string) error {
defer metrics.MeasureSince([]string{"cockroachdb", "delete"}, time.Now())

c.permitPool.Acquire()
if err := c.permitPool.Acquire(ctx); err != nil {
return err
}
defer c.permitPool.Release()

_, err := c.statements["delete"].Exec(key)
Expand All @@ -228,7 +235,9 @@ func (c *CockroachDBBackend) Delete(ctx context.Context, key string) error {
func (c *CockroachDBBackend) List(ctx context.Context, prefix string) ([]string, error) {
defer metrics.MeasureSince([]string{"cockroachdb", "list"}, time.Now())

c.permitPool.Acquire()
if err := c.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer c.permitPool.Release()

likePrefix := prefix + "%"
Expand Down Expand Up @@ -267,7 +276,9 @@ func (c *CockroachDBBackend) Transaction(ctx context.Context, txns []*physical.T
return nil
}

c.permitPool.Acquire()
if err := c.permitPool.Acquire(ctx); err != nil {
return err
}
defer c.permitPool.Release()

return crdb.ExecuteTx(context.Background(), c.client, nil, func(tx *sql.Tx) error {
Expand Down
13 changes: 10 additions & 3 deletions physical/cockroachdb/cockroachdb_ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package cockroachdb

import (
"context"
"database/sql"
"fmt"
"sync"
Expand Down Expand Up @@ -106,7 +107,9 @@ func (l *CockroachDBLock) Lock(stopCh <-chan struct{}) (<-chan struct{}, error)
// CockroachDB table.
func (l *CockroachDBLock) Unlock() error {
c := l.backend
c.permitPool.Acquire()
if err := c.permitPool.Acquire(context.Background()); err != nil {
return err
}
defer c.permitPool.Release()

if l.renewTicker != nil {
Expand All @@ -121,7 +124,9 @@ func (l *CockroachDBLock) Unlock() error {
// including this one, and returns the current value.
func (l *CockroachDBLock) Value() (bool, string, error) {
c := l.backend
c.permitPool.Acquire()
if err := c.permitPool.Acquire(context.Background()); err != nil {
return false, "", err
}
defer c.permitPool.Release()
var result string
err := c.haStatements["get"].QueryRow(l.key).Scan(&result)
Expand Down Expand Up @@ -185,7 +190,9 @@ func (l *CockroachDBLock) periodicallyRenewLock(done chan struct{}) {
// else has the lock, whereas non-nil means that something unexpected happened.
func (l *CockroachDBLock) writeItem() (bool, error) {
c := l.backend
c.permitPool.Acquire()
if err := c.permitPool.Acquire(context.Background()); err != nil {
return false, err
}
defer c.permitPool.Release()

sqlResult, err := c.haStatements["upsert"].Exec(l.identity, l.key, l.value, fmt.Sprintf("%d seconds", l.ttlSeconds))
Expand Down
21 changes: 15 additions & 6 deletions physical/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-secure-stdlib/parseutil"
"github.com/hashicorp/go-secure-stdlib/permitpool"
"github.com/hashicorp/go-secure-stdlib/tlsutil"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/physical"
Expand Down Expand Up @@ -60,7 +61,7 @@ type ConsulBackend struct {
path string
kv *api.KV
txn *api.Txn
permitPool *physical.PermitPool
permitPool *permitpool.Pool
consistencyMode string
sessionTTL string
lockWaitTime time.Duration
Expand Down Expand Up @@ -161,7 +162,7 @@ func NewConsulBackend(conf map[string]string, logger log.Logger) (physical.Backe
client: client,
kv: client.KV(),
txn: client.Txn(),
permitPool: physical.NewPermitPool(maxParInt),
permitPool: permitpool.New(maxParInt),
consistencyMode: consistencyMode,
sessionTTL: sessionTTL,
lockWaitTime: lockWaitTime,
Expand Down Expand Up @@ -253,7 +254,9 @@ func (c *ConsulBackend) ExpandedCapabilitiesAvailable(ctx context.Context) bool
}}
}

c.permitPool.Acquire()
if err := c.permitPool.Acquire(ctx); err != nil {
return false
}
defer c.permitPool.Release()

queryOpts := &api.QueryOptions{}
Expand Down Expand Up @@ -332,7 +335,9 @@ func (c *ConsulBackend) txnInternal(ctx context.Context, txns []*physical.TxnEnt
ops = append(ops, o)
}

c.permitPool.Acquire()
if err := c.permitPool.Acquire(ctx); err != nil {
return err
}
defer c.permitPool.Release()

var retErr *multierror.Error
Expand Down Expand Up @@ -455,7 +460,9 @@ func (c *ConsulBackend) Put(ctx context.Context, entry *physical.Entry) error {
func (c *ConsulBackend) Get(ctx context.Context, key string) (*physical.Entry, error) {
defer metrics.MeasureSince([]string{"consul", "get"}, time.Now())

c.permitPool.Acquire()
if err := c.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer c.permitPool.Release()

queryOpts := &api.QueryOptions{}
Expand Down Expand Up @@ -505,7 +512,9 @@ func (c *ConsulBackend) List(ctx context.Context, prefix string) ([]string, erro
scan = scan[:len(scan)-1]
}

c.permitPool.Acquire()
if err := c.permitPool.Acquire(ctx); err != nil {
return nil, err
}
defer c.permitPool.Release()

queryOpts := &api.QueryOptions{}
Expand Down
Loading
Loading