lmdb: use an waitgroup so transactions don't get closed before they actually end in the sub goroutines.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/PowerDNS/lmdb-go/lmdb"
|
"github.com/PowerDNS/lmdb-go/lmdb"
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
@@ -38,12 +39,15 @@ func (b *LMDBBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
|
|||||||
go func() {
|
go func() {
|
||||||
err := b.lmdbEnv.View(func(txn *lmdb.Txn) error {
|
err := b.lmdbEnv.View(func(txn *lmdb.Txn) error {
|
||||||
txn.RawRead = true
|
txn.RawRead = true
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(len(queries))
|
||||||
|
|
||||||
// actually iterate
|
// actually iterate
|
||||||
cursorClosers := make([]func(), len(queries))
|
cursorClosers := make([]func(), len(queries))
|
||||||
for i, q := range queries {
|
for i, q := range queries {
|
||||||
go func(i int, q query) {
|
go func(i int, q query) {
|
||||||
defer close(q.results)
|
defer close(q.results)
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
cursor, err := txn.OpenCursor(q.dbi)
|
cursor, err := txn.OpenCursor(q.dbi)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -171,6 +175,7 @@ func (b *LMDBBackend) QueryEvents(ctx context.Context, filter nostr.Filter) (cha
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user