Skip to content

Commit

Permalink
use prepared statement for update
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed May 7, 2024
1 parent ec9ea39 commit 4dd7537
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions ipfs/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ func (r *Reprovider) Run(ctx context.Context, interval time.Duration) {
}

for {
cids, err := r.store.ProvideCIDs(1000)
start := time.Now()
cids, err := r.store.ProvideCIDs(5000)
if err != nil {
r.log.Error("failed to fetch CIDs to provide", zap.Error(err))
break
}

if len(cids) == 0 {
r.log.Debug("no CIDs to provide")
reprovideSleep = 15 * time.Minute // set a minimum sleep time
reprovideSleep = 15 * time.Minute // set a minimum sleep time even
break
}

Expand Down Expand Up @@ -115,8 +116,7 @@ func (r *Reprovider) Run(ctx context.Context, interval time.Duration) {
break
}

r.log.Debug("announced CIDs", zap.Int("count", len(announced)), zap.Stringers("cids", announced))
time.Sleep(100 * time.Millisecond)
r.log.Debug("announced CIDs", zap.Int("count", len(announced)), zap.Duration("elapsed", time.Since(start)))
}
}
})
Expand Down
11 changes: 8 additions & 3 deletions persist/sqlite/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ func (s *Store) Pin(b renterd.PinnedBlock) error {
// SetLastAnnouncement updates the last announcement time of a block.
func (s *Store) SetLastAnnouncement(cids []cid.Cid, t time.Time) error {
return s.transaction(func(tx *txn) error {
stmt, err := tx.Prepare(`UPDATE pinned_blocks SET last_announcement=$1 WHERE block_id IN (SELECT id FROM blocks WHERE cid=$2)`)
if err != nil {
return fmt.Errorf("failed to prepare update statement: %w", err)
}
defer stmt.Close()

for _, c := range cids {
c = normalizeCid(c)
_, err := tx.Exec(`UPDATE pinned_blocks SET last_announcement=$1 WHERE block_id IN (SELECT id FROM blocks WHERE cid=$2)`, dbEncode(t), dbEncode(c))
if err != nil {
return fmt.Errorf("failed to update last announcement: %w", err)
if _, err := stmt.Exec(dbEncode(t), dbEncode(c)); err != nil {
return fmt.Errorf("failed to update last announcement %q: %w", c, err)
}
}
return nil
Expand Down

0 comments on commit 4dd7537

Please sign in to comment.