Skip to content

Commit

Permalink
Clean up and fix minor issues with default handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiddlet2666 committed Sep 13, 2023
1 parent 8c18ced commit 7825d57
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions coherence/coherence.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const (
defaultScopeName = "default-store"
defaultTimeout = time.Duration(30) * time.Second
defaultAddress = "localhost:1408"
)

// Storage represents an implementation of Coherence storage provider.
Expand Down Expand Up @@ -42,52 +43,36 @@ type Config struct {

// DefaultConfig defines default options.
var DefaultConfig = Config{
Address: "localhost:1408",
Timeout: time.Duration(30) * time.Millisecond,
Address: defaultAddress,
Timeout: defaultTimeout,
ScopeName: defaultScopeName,
Reset: false,
}

// New returns a new [Storage] given a [coherence.Session].
// New returns a new [Storage] given a [Config].
func New(config ...Config) (*Storage, error) {
var (
scopeName = defaultScopeName
cfg = DefaultConfig
)
cfg := setupConfig(config...)

if len(config) == 1 {
cfg = config[0]
}
options := make([]func(session *coh.SessionOptions), 0)

// apply any config values
if cfg.Address != "" {
options = append(options, coh.WithAddress(cfg.Address))
} else {
cfg.Address = DefaultConfig.Address
}
// apply any config values as Coherence options
options = append(options, coh.WithAddress(cfg.Address))

if cfg.TLSConfig != nil {
options = append(options, coh.WithTLSConfig(cfg.TLSConfig))
} else {
options = append(options, coh.WithPlainText())
}

if cfg.Timeout != defaultTimeout {
options = append(options, coh.WithReadyTimeout(cfg.Timeout))
}

if cfg.ScopeName != defaultScopeName {
scopeName = cfg.ScopeName
}
options = append(options, coh.WithRequestTimeout(cfg.Timeout))

// create the Coherence session
session, err := coh.NewSession(context.Background(), options...)
if err != nil {
return nil, err
}

store, err := newCoherenceStorage(session, scopeName)
store, err := newCoherenceStorage(session, cfg.ScopeName)
if err != nil {
return nil, err
}
Expand All @@ -100,6 +85,29 @@ func New(config ...Config) (*Storage, error) {
return store, nil
}

// setupConfig sets the default config.
func setupConfig(config ...Config) Config {
// if nothing provided then use the default config values
if len(config) == 0 {
return DefaultConfig
}

cfg := config[0]

// Check for any invalid default values and overwrite them
if cfg.Address == "" {
cfg.Address = DefaultConfig.Address
}
if cfg.ScopeName == "" {
cfg.ScopeName = DefaultConfig.ScopeName
}
if cfg.Timeout == 0 {
cfg.Timeout = DefaultConfig.Timeout
}

return cfg
}

// newCoherenceStorage returns a new Coherence [Storage].
func newCoherenceStorage(session *coh.Session, cacheName string) (*Storage, error) {
nc, err := coh.GetNamedCache[string, []byte](session, "fiber$"+cacheName)
Expand Down

0 comments on commit 7825d57

Please sign in to comment.