Skip to content

Commit

Permalink
add option to not persist natsjs stores
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Dec 13, 2023
1 parent 21bcc88 commit e760e9e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pkg/storage/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/storage/utils/decomposedfs/decomposedfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions pkg/store/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 7 additions & 0 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit e760e9e

Please sign in to comment.