sync: a page is stored in one transaction with parallel verification; a sync is cut off by idleness, not a fixed 2 minutes
20,000 listings to a newcomer: 120 s (unfinished) -> 24 s. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
+98
-12
@@ -10,7 +10,9 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
@@ -268,24 +270,111 @@ func (s *Store) Close() error { return s.db.Close() }
|
||||
// - tombstones are stored like any other version, so a late-arriving older
|
||||
// version cannot resurrect a deleted listing.
|
||||
func (s *Store) Put(ctx context.Context, l *listing.Listing) (Outcome, error) {
|
||||
if err := l.Check(s.clock(), s.difficulty); err != nil {
|
||||
return Ignored, fmt.Errorf("%w: %v", ErrInvalid, err)
|
||||
}
|
||||
data, err := l.Bytes()
|
||||
data, hash, err := s.verify(l)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
hash, err := l.Hash()
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
outcome, err := s.putTx(ctx, tx, l, data, hash)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
return outcome, nil
|
||||
}
|
||||
|
||||
// PutResult is what became of one listing of a PutMany.
|
||||
type PutResult struct {
|
||||
Outcome Outcome
|
||||
Err error // ErrInvalid, ErrRevoked, ErrQuota; the others abort the batch
|
||||
}
|
||||
|
||||
// PutMany does what Put does for every listing, in one transaction: a sync page
|
||||
// is stored with one write to disk instead of one per listing. The listings
|
||||
// are applied in order, so a later one sees the quota use of the earlier ones.
|
||||
// A listing that is refused (invalid, revoked, over quota) does not stop the
|
||||
// others; a database error does, and nothing is stored.
|
||||
func (s *Store) PutMany(ctx context.Context, ls []*listing.Listing) ([]PutResult, error) {
|
||||
res := make([]PutResult, len(ls))
|
||||
type prepared struct {
|
||||
data []byte
|
||||
hash string
|
||||
}
|
||||
prep := make([]prepared, len(ls))
|
||||
// Checking signatures is most of the work and needs no database: spread it
|
||||
// over the cores.
|
||||
var wg sync.WaitGroup
|
||||
next := make(chan int)
|
||||
for w := 0; w < min(runtime.NumCPU(), max(1, len(ls)/4)); w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := range next {
|
||||
data, hash, err := s.verify(ls[i])
|
||||
if err != nil {
|
||||
res[i] = PutResult{Ignored, err}
|
||||
continue
|
||||
}
|
||||
prep[i] = prepared{data, hash}
|
||||
}
|
||||
}()
|
||||
}
|
||||
for i := range ls {
|
||||
next <- i
|
||||
}
|
||||
close(next)
|
||||
wg.Wait()
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
for i, l := range ls {
|
||||
if res[i].Err != nil {
|
||||
continue
|
||||
}
|
||||
out, err := s.putTx(ctx, tx, l, prep[i].data, prep[i].hash)
|
||||
switch {
|
||||
case errors.Is(err, ErrRevoked), errors.Is(err, ErrQuota):
|
||||
res[i] = PutResult{Ignored, err}
|
||||
case err != nil:
|
||||
return nil, err
|
||||
default:
|
||||
res[i] = PutResult{out, nil}
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// verify checks l (signature, proof of work, dates) and returns the bytes and
|
||||
// hash to store.
|
||||
func (s *Store) verify(l *listing.Listing) ([]byte, string, error) {
|
||||
if err := l.Check(s.clock(), s.difficulty); err != nil {
|
||||
return nil, "", fmt.Errorf("%w: %v", ErrInvalid, err)
|
||||
}
|
||||
data, err := l.Bytes()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
hash, err := l.Hash()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return data, hash, nil
|
||||
}
|
||||
|
||||
// putTx applies the conflict rules of Put to a verified listing inside tx.
|
||||
func (s *Store) putTx(ctx context.Context, tx *sql.Tx, l *listing.Listing, data []byte, hash string) (Outcome, error) {
|
||||
var err error
|
||||
var revoked int
|
||||
err = tx.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*) FROM revocations
|
||||
@@ -342,9 +431,6 @@ func (s *Store) Put(ctx context.Context, l *listing.Listing) (Outcome, error) {
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
return outcome, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user