From e760e9e7b2d172e0c29c362a88b7d888b5467e2b Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 13 Dec 2023 13:05:31 +0100 Subject: [PATCH] add option to not persist natsjs stores Signed-off-by: jkoberg --- pkg/storage/cache/cache.go | 13 +++++++------ pkg/storage/utils/decomposedfs/decomposedfs.go | 1 + pkg/store/options.go | 14 ++++++++++++++ pkg/store/store.go | 7 +++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/pkg/storage/cache/cache.go b/pkg/storage/cache/cache.go index af4b0ffdce1..516c65c7388 100644 --- a/pkg/storage/cache/cache.go +++ b/pkg/storage/cache/cache.go @@ -44,12 +44,13 @@ var ( // Config contains the configuring for a cache type Config struct { - Store string `mapstructure:"cache_store"` - Nodes []string `mapstructure:"cache_nodes"` - Database string `mapstructure:"cache_database"` - Table string `mapstructure:"cache_table"` - TTL int `mapstructure:"cache_ttl"` - Size int `mapstructure:"cache_size"` + Store string `mapstructure:"cache_store"` + Nodes []string `mapstructure:"cache_nodes"` + Database string `mapstructure:"cache_database"` + Table string `mapstructure:"cache_table"` + TTL int `mapstructure:"cache_ttl"` + Size int `mapstructure:"cache_size"` + DisablePersistence bool `mapstructure:"cache_disable_persistence"` } // Cache handles key value operations on caches diff --git a/pkg/storage/utils/decomposedfs/decomposedfs.go b/pkg/storage/utils/decomposedfs/decomposedfs.go index 5c341c73570..8ac77994736 100644 --- a/pkg/storage/utils/decomposedfs/decomposedfs.go +++ b/pkg/storage/utils/decomposedfs/decomposedfs.go @@ -143,6 +143,7 @@ func NewDefault(m map[string]interface{}, bs tree.Blobstore, es events.Stream) ( microstore.Nodes(o.IDCache.Nodes...), microstore.Database(o.IDCache.Database), microstore.Table(o.IDCache.Table), + store.DisablePersistance(o.IDCache.DisablePersistence), )) permissionsSelector, err := pool.PermissionsSelector(o.PermissionsSVC, pool.WithTLSMode(o.PermTLSMode)) diff --git a/pkg/store/options.go b/pkg/store/options.go index 5866d6f09a2..c6f3c9fee99 100644 --- a/pkg/store/options.go +++ b/pkg/store/options.go @@ -75,3 +75,17 @@ func TTL(val time.Duration) store.Option { o.Context = context.WithValue(o.Context, ttlContextKey{}, val) } } + +type disablePersistanceContextKey struct{} + +// DisablePersistance disables the persistence of the store by instructing it to use memory only. +// Only supported by the `natsjs` and `natsjskv` implementations. +func DisablePersistance(val bool) store.Option { + return func(o *store.Options) { + if o.Context == nil { + o.Context = context.Background() + } + + o.Context = context.WithValue(o.Context, disablePersistanceContextKey{}, val) + } +} diff --git a/pkg/store/store.go b/pkg/store/store.go index c3f66d376f4..67ef00dd2bd 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -119,6 +119,9 @@ func Create(opts ...microstore.Option) microstore.Store { return *ocMemStore case TypeNatsJS: ttl, _ := options.Context.Value(ttlContextKey{}).(time.Duration) + if mem, _ := options.Context.Value(disablePersistanceContextKey{}).(bool); mem { + opts = append(opts, natsjs.DefaultMemory()) + } // TODO nats needs a DefaultTTL option as it does not support per Write TTL ... // FIXME nats has restrictions on the key, we cannot use slashes AFAICT // host, port, clusterid @@ -132,6 +135,10 @@ func Create(opts ...microstore.Option) microstore.Store { case TypeNatsJSKV: // NOTE: nats needs a DefaultTTL option as it does not support per Write TTL ... ttl, _ := options.Context.Value(ttlContextKey{}).(time.Duration) + if mem, _ := options.Context.Value(disablePersistanceContextKey{}).(bool); mem { + opts = append(opts, natsjskv.DefaultMemory()) + } + natsOptions := nats.GetDefaultOptions() natsOptions.Name = "TODO" // we can pass in the service name to allow identifying the client, but that requires adding a custom context option return natsjskv.NewStore(