@@ -26,7 +26,7 @@ type SQLiteBackend struct {
2626 wg sync.WaitGroup
2727
2828 initialized bool
29- mu sync. RWMutex
29+
3030}
3131
3232// NewSQLiteBackend creates a new SQLite-based backend
@@ -44,8 +44,6 @@ func NewSQLiteBackend(db *sql.DB, logger *zap.Logger, config *SQLiteBackendConfi
4444
4545// Initialize sets up the SQLite backend and starts background workers
4646func (b * SQLiteBackend ) Initialize (ctx context.Context ) error {
47- b .mu .Lock ()
48- defer b .mu .Unlock ()
4947
5048 if b .initialized {
5149 return nil
@@ -216,8 +214,6 @@ func (b *SQLiteBackend) CleanupRange(ctx context.Context, from, to time.Time) er
216214
217215// Close gracefully shuts down the SQLite backend
218216func (b * SQLiteBackend ) Close () error {
219- b .mu .Lock ()
220- defer b .mu .Unlock ()
221217
222218 if ! b .initialized {
223219 return nil
@@ -305,8 +301,16 @@ func (b *SQLiteBackend) pollAllOrganizations() {
305301 }
306302
307303 if len (events ) > 0 {
308- b .deliverEvents (org , events )
304+ if len (events ) > 0 {
305+ if b .deliverEvents (org , events ) != nil {
306+ org .updatePollState (state .VersionID , time .Now ())
307+ }
308+ // If delivery failed (channel full), don't update timestamp
309+ // so events will be retried on next poll
310+ } else {
311+ org .updatePollState (state .VersionID , time .Now ())
309312 }
313+ }
310314
311315 org .updatePollState (state .VersionID , time .Now ())
312316 }
@@ -339,6 +343,7 @@ func (b *SQLiteBackend) getAllStates(ctx context.Context) ([]OrganizationState,
339343
340344// getEventsSince retrieves events for an organization after a given timestamp
341345func (b * SQLiteBackend ) getEventsSince (ctx context.Context , orgID string , since time.Time ) ([]Event , error ) {
346+ // TODO: (VirajSalaka) Implement pagination if large number of events
342347 query := `
343348 SELECT processed_timestamp, originated_timestamp, event_type,
344349 action, entity_id, correlation_id, event_data
@@ -370,17 +375,18 @@ func (b *SQLiteBackend) getEventsSince(ctx context.Context, orgID string, since
370375}
371376
372377// deliverEvents sends events to all subscribers of an organization
373- func (b * SQLiteBackend ) deliverEvents (org * organization , events []Event ) {
378+ func (b * SQLiteBackend ) deliverEvents (org * organization , events []Event ) error {
374379 subscribers := org .getSubscribers ()
375380
376381 if len (subscribers ) == 0 {
377382 b .logger .Debug ("No subscribers for organization" ,
378383 zap .String ("organization" , string (org .id )),
379384 zap .Int ("events" , len (events )),
380385 )
381- return
386+ return nil
382387 }
383388
389+ // TODO: (VirajSalaka) One subscriber is considered here. Handle multiple subscribers properly.
384390 for _ , ch := range subscribers {
385391 select {
386392 case ch <- events :
@@ -389,12 +395,14 @@ func (b *SQLiteBackend) deliverEvents(org *organization, events []Event) {
389395 zap .Int ("events" , len (events )),
390396 )
391397 default :
392- b .logger .Warn ("Subscriber channel full, dropping events" ,
398+ b .logger .Error ("Subscriber channel full, dropping events" ,
393399 zap .String ("organization" , string (org .id )),
394400 zap .Int ("events" , len (events )),
395401 )
402+ return fmt .Errorf ("subscriber channel full" )
396403 }
397404 }
405+ return nil
398406}
399407
400408// cleanupLoop runs periodic cleanup of old events
0 commit comments