From 547ac03eeb6f087ac0507c8b2d62013092ced017 Mon Sep 17 00:00:00 2001 From: hexoscott <70711990+hexoscott@users.noreply.github.com> Date: Thu, 15 Sep 2022 21:14:10 +0100 Subject: [PATCH] ensure kv semaphore has at least 1 count (#641) --- kv/mdbx/kv_mdbx.go | 6 +++++- kv/remotedb/kv_remote.go | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/kv/mdbx/kv_mdbx.go b/kv/mdbx/kv_mdbx.go index d63f8579a..6c09615bc 100644 --- a/kv/mdbx/kv_mdbx.go +++ b/kv/mdbx/kv_mdbx.go @@ -281,7 +281,11 @@ func (opts MdbxOpts) Open() (kv.RwDB, error) { } if opts.roTxsLimiter == nil { - opts.roTxsLimiter = semaphore.NewWeighted(int64(runtime.GOMAXPROCS(-1)) - 1) // 1 less than max to allow unlocking to happen + targetSemCount := int64(runtime.GOMAXPROCS(-1)) - 1 + if targetSemCount <= 1 { + targetSemCount = 2 + } + opts.roTxsLimiter = semaphore.NewWeighted(targetSemCount) // 1 less than max to allow unlocking to happen } db := &MdbxKV{ opts: opts, diff --git a/kv/remotedb/kv_remote.go b/kv/remotedb/kv_remote.go index 677f29644..d679cbcb8 100644 --- a/kv/remotedb/kv_remote.go +++ b/kv/remotedb/kv_remote.go @@ -69,12 +69,17 @@ func (opts remoteOpts) WithBucketsConfig(f mdbx.TableCfgFunc) remoteOpts { } func (opts remoteOpts) Open() (*RemoteKV, error) { + targetSemCount := int64(runtime.GOMAXPROCS(-1)) - 1 + if targetSemCount <= 1 { + targetSemCount = 2 + } + db := &RemoteKV{ opts: opts, remoteKV: opts.remoteKV, log: log.New("remote_db", opts.DialAddress), buckets: kv.TableCfg{}, - roTxsLimiter: semaphore.NewWeighted(int64(runtime.GOMAXPROCS(-1)) - 1), // 1 less than max to allow unlocking + roTxsLimiter: semaphore.NewWeighted(targetSemCount), // 1 less than max to allow unlocking } customBuckets := opts.bucketsCfg(kv.ChaindataTablesCfg) for name, cfg := range customBuckets { // copy map to avoid changing global variable