diff --git a/waku/node.go b/waku/node.go index a688ab1b..19fe3533 100644 --- a/waku/node.go +++ b/waku/node.go @@ -144,7 +144,7 @@ func Execute(options Options) { } if options.Store.Enable { - nodeOpts = append(nodeOpts, node.WithWakuStoreAndLimits(options.Store.ShouldResume, options.Store.RetentionMaxDaysDuration(), options.Store.RetentionMaxMessages)) + nodeOpts = append(nodeOpts, node.WithWakuStoreAndRetentionPolicy(options.Store.ShouldResume, options.Store.RetentionMaxDaysDuration(), options.Store.RetentionMaxMessages)) if options.UseDB { dbStore, err := persistence.NewDBStore(persistence.WithDB(db), persistence.WithRetentionPolicy(options.Store.RetentionMaxMessages, options.Store.RetentionMaxDaysDuration())) failOnErr(err, "DBStore") diff --git a/waku/persistence/store.go b/waku/persistence/store.go index 656379fb..da96ea1c 100644 --- a/waku/persistence/store.go +++ b/waku/persistence/store.go @@ -21,7 +21,7 @@ type DBStore struct { db *sql.DB maxMessages int - maxDays time.Duration + maxDuration time.Duration } type StoredMessage struct { @@ -54,16 +54,17 @@ func WithDriver(driverName string, datasourceName string) DBOption { } } -func WithRetentionPolicy(maxMessages int, maxDays time.Duration) DBOption { +func WithRetentionPolicy(maxMessages int, maxDuration time.Duration) DBOption { return func(d *DBStore) error { - d.maxDays = maxDays + d.maxDuration = maxDuration d.maxMessages = maxMessages return nil } } // Creates a new DB store using the db specified via options. -// It will create a messages table if it does not exist +// It will create a messages table if it does not exist and +// clean up records according to the retention policy used func NewDBStore(options ...DBOption) (*DBStore, error) { result := new(DBStore) @@ -105,10 +106,10 @@ func (d *DBStore) createTable() error { } func (d *DBStore) cleanOlderRecords() error { - // Delete messages older than N days - if d.maxDays > 0 { + // Delete older messages + if d.maxDuration > 0 { sqlStmt := `DELETE FROM message WHERE receiverTimestamp < ?` - _, err := d.db.Exec(sqlStmt, utils.GetUnixEpochFrom(func() time.Time { return time.Now().Add(-d.maxDays) })) + _, err := d.db.Exec(sqlStmt, utils.GetUnixEpochFrom(func() time.Time { return time.Now().Add(-d.maxDuration) })) if err != nil { return err } diff --git a/waku/v2/node/wakunode2.go b/waku/v2/node/wakunode2.go index b470b1a9..e09f8500 100644 --- a/waku/v2/node/wakunode2.go +++ b/waku/v2/node/wakunode2.go @@ -128,7 +128,7 @@ func New(ctx context.Context, opts ...WakuNodeOption) (*WakuNode, error) { } func (w *WakuNode) Start() error { - w.store = store.NewWakuStore(w.opts.messageProvider, w.opts.maxMessages, w.opts.maxDays) + w.store = store.NewWakuStore(w.opts.messageProvider, w.opts.maxMessages, w.opts.maxDuration) if w.opts.enableStore { w.startStore() } diff --git a/waku/v2/node/wakuoptions.go b/waku/v2/node/wakuoptions.go index b1548e9e..1afd6e1a 100644 --- a/waku/v2/node/wakuoptions.go +++ b/waku/v2/node/wakuoptions.go @@ -38,7 +38,7 @@ type WakuNodeParameters struct { storeMsgs bool messageProvider store.MessageProvider maxMessages int - maxDays time.Duration + maxDuration time.Duration enableRendezvous bool enableRendezvousServer bool @@ -179,14 +179,14 @@ func WithWakuStore(shouldStoreMessages bool, shouldResume bool) WakuNodeOption { } } -// WithWakuStoreAndLimits enables the Waku V2 Store protocol, storing them in an optional message provider +// WithWakuStoreAndRetentionPolicy enables the Waku V2 Store protocol, storing them in an optional message provider // applying an specific retention policy -func WithWakuStoreAndLimits(shouldResume bool, maxDays time.Duration, maxMessages int) WakuNodeOption { +func WithWakuStoreAndRetentionPolicy(shouldResume bool, maxDuration time.Duration, maxMessages int) WakuNodeOption { return func(params *WakuNodeParameters) error { params.enableStore = true params.storeMsgs = true params.shouldResume = shouldResume - params.maxDays = maxDays + params.maxDuration = maxDuration params.maxMessages = maxMessages return nil }