v1.0.0

Configuration

Last updated


This page lists every field of the Config passed to New, its default, and how invalid values are corrected.

Config Fields


Config


  1. type Config struct {
  2. Redis *Redis `json:"redis"`
  3. Log *Log `json:"log,omitempty"`
  4. Option *Options `json:"option,omitempty"`
  5. Email *EmailConfig `json:"email,omitempty"`
  6. }

FieldRequiredWhen nil
RedisYesNew panics when the ping succeeds (nil pointer at sync.go:51); when the ping fails, the first local file access (a fallback file read or write, Del, or the expiry sweep removing a file) panics
LogNoAll log defaults apply
OptionNoAll option defaults apply
EmailNoNo effect (the current version sends no mail)

When Redis is nil, initRedis fills defaults only into its local copy; the instance keeps a nil Config.Redis, and the next access to Redis.DB panics. Pass &redisFallback.Redis{} even when every default is acceptable.

Redis


FieldDefaultInvalid values
HostlocalhostEmpty string → localhost
Port6379≤ 0 or > 65535 → 6379
Password""—
DB0< 0 or > 15 → 0

DB also names the local file subdirectory {DBPath}/{DB}.

Options


FieldDefaultEffect
DBPath./files/redisFallback/dbRoot of the local JSON files; {DBPath}/{DB} must exist before startup or normal mode is never entered
MaxRetry3Attempts per Redis access in Get / Set; exhausting them switches to fallback
MaxQueue1000Fallback write queue length; when full, Set writes the file synchronously
TimeToWrite3sFallback batch write interval; also the maximum delay from Set to disk
TimeToCheck1mHealth-check ping interval; also the maximum delay before a recovered Redis is noticed

Any numeric field ≤ 0 falls back to its default.

Log


A type alias of Log from github.com/pardnchiu/go-logger v0.2.0:
FieldDefaultDescription
Path./logs/redisFallbackLog directory containing debug.log, output.log, error.log
StdoutfalseAlso write to stdout
MaxSize16 * 1024 * 1024 (16 MiB)Maximum file size in bytes
MaxBackup5Rotated files kept
Typetexttext or json

Email


EmailConfig has Host, Port, Username, Password, From, To, CC, Subject, and Body fields, but the send call is commented out in the source (sync.go:26), so setting it has no effect.

Tuning


GoalAdjustment
Return to normal mode sooner after Redis recoversLower TimeToCheck
Lose less data if the process crashes during fallbackLower TimeToWrite
Avoid false fallback switches when Redis is flakyRaise MaxRetry (attempts have no delay between them, so the effect is limited)

Full Example


  1. rf, err := redisFallback.New(redisFallback.Config{
  2. Redis: &redisFallback.Redis{
  3. Host: "10.0.0.5",
  4. Port: 6379,
  5. Password: os.Getenv("REDIS_PASSWORD"),
  6. DB: 2,
  7. },
  8. Log: &redisFallback.Log{
  9. Path: "/var/log/myapp/redis-fallback",
  10. Stdout: true,
  11. },
  12. Option: &redisFallback.Options{
  13. DBPath: "/var/lib/myapp/redis-fallback",
  14. MaxRetry: 3,
  15. MaxQueue: 5000,
  16. TimeToWrite: 1 * time.Second,
  17. TimeToCheck: 15 * time.Second,
  18. },
  19. })
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. defer rf.Close()
中文