-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
105 lines (84 loc) · 2.56 KB
/
config.go
File metadata and controls
105 lines (84 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package cachegrid
import (
"fmt"
"time"
)
// Mode defines the cache distribution strategy.
type Mode int
const (
Partitioned Mode = iota
Replicated
NearCache
)
// Config holds configuration for a Cache instance.
type Config struct {
// NumShards is the number of internal shards (memory mode only). Must be a power of 2.
// Default: 256.
NumShards int
// MaxMemoryMB is the maximum memory in megabytes across all shards (memory mode only).
// 0 means unlimited.
MaxMemoryMB int64
// DefaultTTL is the default expiration for entries when no TTL is specified.
// 0 means no expiry by default.
DefaultTTL time.Duration
// SweeperInterval controls how often the background goroutine
// checks for expired entries. Default: 1 second.
SweeperInterval time.Duration
// StorageMode selects the storage backend. Default: Memory.
StorageMode StorageMode
// DiskPath is the directory for disk-based storage (Pebble).
// Required when StorageMode is Disk. Ignored for Memory mode.
DiskPath string
// --- Cluster Configuration ---
// ListenAddr is the gossip protocol listen address (e.g., ":7946").
// If empty, the cache runs in local-only mode.
ListenAddr string
// Peers is the list of seed nodes for cluster discovery.
Peers []string
// Mode is the cache distribution strategy.
Mode Mode
// NodeName is a unique identifier for this node. If empty, hostname is used.
NodeName string
// GRPCPort is the port for inter-node gRPC/RPC communication. Default: 7947.
GRPCPort int
// HTTPPort is the HTTP server port for standalone mode. Default: 6380.
HTTPPort int
// VirtualNodes is the number of virtual nodes per physical node on the hash ring.
// Default: 150.
VirtualNodes int
}
// DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() Config {
return Config{
NumShards: 256,
MaxMemoryMB: 0,
DefaultTTL: 0,
SweeperInterval: time.Second,
Mode: Partitioned,
GRPCPort: 7947,
HTTPPort: 6380,
VirtualNodes: 150,
}
}
func (c *Config) validate() error {
if c.SweeperInterval <= 0 {
c.SweeperInterval = time.Second
}
switch c.StorageMode {
case Disk:
if c.DiskPath == "" {
return fmt.Errorf("cachegrid: DiskPath is required when StorageMode is Disk")
}
default: // Memory
if c.NumShards <= 0 {
c.NumShards = 256
}
if c.NumShards&(c.NumShards-1) != 0 {
return fmt.Errorf("cachegrid: NumShards must be a power of 2, got %d", c.NumShards)
}
if c.MaxMemoryMB < 0 {
return fmt.Errorf("cachegrid: MaxMemoryMB must be >= 0, got %d", c.MaxMemoryMB)
}
}
return nil
}