Skip to content

Commit

Permalink
Updated db.go
Browse files Browse the repository at this point in the history
Signed-off-by: Priyank Makwana <[email protected]>
  • Loading branch information
RuneRogue authored and derrix060 committed Aug 30, 2024
1 parent c000b74 commit 5af6ca0
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions db/remote/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ func New(rawURL string, ctx context.Context, log utils.SimpleLogger, opts ...grp
return nil, err
}

listener := &db.SelectiveListener{
OnIOCb: func(write bool, duration time.Duration) {},
OnCommitCb: func(duration time.Duration) {},
}

return &DB{
ctx: ctx,
grpcClient: grpcClient,
kvClient: gen.NewKVClient(grpcClient),
log: log,
listener: listener,
}, nil
}

Expand All @@ -42,10 +48,8 @@ func (d *DB) NewTransaction(write bool) (db.Transaction, error) {
if err != nil {
return nil, err
}

if d.listener != nil {
d.listener.OnIO(write, time.Since(start))
}

d.listener.OnIO(write, time.Since(start))

return &transaction{client: txClient, log: d.log}, nil
}
Expand All @@ -57,13 +61,11 @@ func (d *DB) View(fn func(txn db.Transaction) error) error {
func (d *DB) Update(fn func(txn db.Transaction) error) error {
start := time.Now()

err := db.Update(d, fn)

if d.listener != nil {
defer func() {
d.listener.OnCommit(time.Since(start))
}
}()

return err
return db.Update(d, fn)
}

func (d *DB) WithListener(listener db.EventListener) db.DB {
Expand Down

0 comments on commit 5af6ca0

Please sign in to comment.