Optimize database batch operations with prepared statements
- Add prepared statements to all batch operations for better performance - Fix database lock contention by properly batching operations - Update SQLite settings for extreme performance (8GB cache, sync OFF) - Add proper error handling for statement closing - Update tests to properly track batch operations
This commit is contained in:
@@ -135,18 +135,22 @@ func (h *PeerHandler) flushBatchLocked() {
|
||||
}
|
||||
}
|
||||
|
||||
// Apply updates
|
||||
for _, update := range peerMap {
|
||||
if err := h.db.UpdatePeer(update.peerIP, update.peerASN, update.messageType, update.timestamp); err != nil {
|
||||
h.logger.Error("Failed to update peer",
|
||||
"peer", update.peerIP,
|
||||
"peer_asn", update.peerASN,
|
||||
"message_type", update.messageType,
|
||||
"error", err,
|
||||
)
|
||||
// Convert to database format
|
||||
dbPeerMap := make(map[string]database.PeerUpdate)
|
||||
for peerIP, update := range peerMap {
|
||||
dbPeerMap[peerIP] = database.PeerUpdate{
|
||||
PeerIP: update.peerIP,
|
||||
PeerASN: update.peerASN,
|
||||
MessageType: update.messageType,
|
||||
Timestamp: update.timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
// Process all peers in a single batch transaction
|
||||
if err := h.db.UpdatePeerBatch(dbPeerMap); err != nil {
|
||||
h.logger.Error("Failed to process peer batch", "error", err, "count", len(dbPeerMap))
|
||||
}
|
||||
|
||||
// Clear batch
|
||||
h.peerBatch = h.peerBatch[:0]
|
||||
h.lastFlush = time.Now()
|
||||
|
||||
Reference in New Issue
Block a user