Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit

Permalink
perf: add optional sync
Browse files Browse the repository at this point in the history
  • Loading branch information
yudhasubki committed Dec 16, 2023
1 parent d77c7cd commit 404d7d7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cmd/blockqueue/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func (h *Http) Run(ctx context.Context, args []string) error {
}
db := blockqueue.NewDb(sqlite)

etcd, err := etcd.New(cfg.Etcd.Path)
etcd, err := etcd.New(
cfg.Etcd.Path,
etcd.WithSync(cfg.Etcd.Sync),
)
if err != nil {
slog.Error("failed to open etcd database", "error", err)
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/blockqueue/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type SQLiteConfig struct {

type EtcdConfig struct {
Path string `yaml:"path"`
Sync bool `yaml:"sync"`
}

type JobConfig struct {
Expand Down
3 changes: 2 additions & 1 deletion config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ sqlite:
db_name: "blockqueue"
busy_timeout: 5000
etcd:
path: "etcdb"
path: "etcdb"
sync: false
26 changes: 25 additions & 1 deletion pkg/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,35 @@ type Etcd struct {
db *nutsdb.DB
}

func New(dbName string) (*Etcd, error) {
type option struct {
// sync represents if call Sync() function.
// if SyncEnable is false, high write performance but potential data loss likely.
// if SyncEnable is true, slower but persistent.
sync bool
}

type opt func(*option)

func WithSync(sync bool) opt {
return func(o *option) {
o.sync = sync
}
}

func New(dbName string, opts ...opt) (*Etcd, error) {
opt := &option{
sync: true,
}

for _, o := range opts {
o(opt)
}

db, err := nutsdb.Open(
nutsdb.DefaultOptions,
nutsdb.WithDir(dbName),
nutsdb.WithEntryIdxMode(nutsdb.HintKeyAndRAMIdxMode),
nutsdb.WithSyncEnable(opt.sync),
)
if err != nil {
return &Etcd{}, err
Expand Down

0 comments on commit 404d7d7

Please sign in to comment.