Skip to content

Commit

Permalink
Emit events on remote db (#2088)
Browse files Browse the repository at this point in the history
  • Loading branch information
RuneRogue authored Aug 30, 2024
1 parent daf1cde commit 5e6e86c
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions db/remote/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package remote
import (
"context"
"math"
"time"

"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/grpc/gen"
Expand All @@ -13,11 +14,11 @@ import (
var _ db.DB = (*DB)(nil)

type DB struct {
ctx context.Context

ctx context.Context
grpcClient *grpc.ClientConn
kvClient gen.KVClient
log utils.SimpleLogger
listener db.EventListener
}

func New(rawURL string, ctx context.Context, log utils.SimpleLogger, opts ...grpc.DialOption) (*DB, error) {
Expand All @@ -26,20 +27,30 @@ 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
}

func (d *DB) NewTransaction(write bool) (db.Transaction, error) {
start := time.Now()

txClient, err := d.kvClient.Tx(d.ctx, grpc.MaxCallSendMsgSize(math.MaxInt), grpc.MaxCallRecvMsgSize(math.MaxInt))
if err != nil {
return nil, err
}

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

return &transaction{client: txClient, log: d.log}, nil
}

Expand All @@ -48,10 +59,17 @@ 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()

defer func() {
d.listener.OnCommit(time.Since(start))
}()

return db.Update(d, fn)
}

func (d *DB) WithListener(listener db.EventListener) db.DB {
d.listener = listener
return d
}

Expand Down

0 comments on commit 5e6e86c

Please sign in to comment.