diff --git a/kv/mdbx/kv_mdbx.go b/kv/mdbx/kv_mdbx.go index 9bee6164c..70279becf 100644 --- a/kv/mdbx/kv_mdbx.go +++ b/kv/mdbx/kv_mdbx.go @@ -27,6 +27,7 @@ import ( "sort" "strings" "sync" + "time" "github.com/c2h5oh/datasize" stack2 "github.com/go-stack/stack" @@ -52,6 +53,7 @@ type MdbxOpts struct { mapSize datasize.ByteSize flags uint log log.Logger + syncPeriod time.Duration augumentLimit uint64 pageSize uint64 roTxsLimiter chan struct{} @@ -124,6 +126,11 @@ func (opts MdbxOpts) Readonly() MdbxOpts { return opts } +func (opts MdbxOpts) SyncPeriod(period time.Duration) MdbxOpts { + opts.syncPeriod = period + return opts +} + func (opts MdbxOpts) DBVerbosity(v kv.DBVerbosityLvl) MdbxOpts { opts.verbosity = v return opts @@ -227,6 +234,15 @@ func (opts MdbxOpts) Open() (kv.RwDB, error) { return nil, fmt.Errorf("%w, label: %s, trace: %s", err, opts.label.String(), stack2.Trace().String()) } + if opts.syncPeriod != 0 { + // the option value is in 1/65536 of second units + optValue := uint64(opts.syncPeriod / (time.Second / 65536)) + if err = env.SetOption(mdbx.OptSyncPeriod, optValue); err != nil { + env.Close() + return nil, err + } + } + if opts.roTxsLimiter == nil { opts.roTxsLimiter = make(chan struct{}, runtime.NumCPU())