-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
84 lines (72 loc) · 2.59 KB
/
config.go
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
package immunitycache
import (
"encoding/json"
"fmt"
"github.com/ThotaGopichandThota/gn-core3/core"
"github.com/ThotaGopichandThota/gn-storage/common"
)
const numChunksLowerBound = 1
const numChunksUpperBound = 128
const maxNumItemsLowerBound = 4
const maxNumBytesLowerBound = maxNumItemsLowerBound * 1
const maxNumBytesUpperBound = 1_073_741_824 // one GB
const numItemsToPreemptivelyEvictLowerBound = 1
// CacheConfig holds cache configuration
type CacheConfig struct {
Name string
NumChunks uint32
MaxNumItems uint32
MaxNumBytes uint32
NumItemsToPreemptivelyEvict uint32
}
// Verify verifies the validity of the configuration
func (config *CacheConfig) Verify() error {
if len(config.Name) == 0 {
return fmt.Errorf("%w: config.Name is invalid", common.ErrInvalidConfig)
}
if config.NumChunks < numChunksLowerBound || config.NumChunks > numChunksUpperBound {
return fmt.Errorf("%w: config.NumChunks is invalid", common.ErrInvalidConfig)
}
if config.MaxNumItems < maxNumItemsLowerBound {
return fmt.Errorf("%w: config.MaxNumItems is invalid", common.ErrInvalidConfig)
}
if config.MaxNumBytes < maxNumBytesLowerBound || config.MaxNumBytes > maxNumBytesUpperBound {
return fmt.Errorf("%w: config.MaxNumBytes is invalid", common.ErrInvalidConfig)
}
if config.NumItemsToPreemptivelyEvict < numItemsToPreemptivelyEvictLowerBound {
return fmt.Errorf("%w: config.NumItemsToPreemptivelyEvict is invalid", common.ErrInvalidConfig)
}
return nil
}
func (config *CacheConfig) getChunkConfig() immunityChunkConfig {
numChunks := core.MaxUint32(config.NumChunks, 1)
return immunityChunkConfig{
cacheName: config.Name,
maxNumItems: config.MaxNumItems / numChunks,
maxNumBytes: config.MaxNumBytes / numChunks,
numItemsToPreemptivelyEvict: config.NumItemsToPreemptivelyEvict / numChunks,
}
}
// String returns a readable representation of the object
func (config *CacheConfig) String() string {
bytes, err := json.Marshal(config)
if err != nil {
log.Error("CacheConfig.String()", "err", err)
}
return string(bytes)
}
type immunityChunkConfig struct {
cacheName string
maxNumItems uint32
maxNumBytes uint32
numItemsToPreemptivelyEvict uint32
}
// String returns a readable representation of the object
func (config *immunityChunkConfig) String() string {
return fmt.Sprintf(
"maxNumItems: %d, maxNumBytes: %d, numItemsToPreemptivelyEvict: %d",
config.maxNumItems,
config.maxNumBytes,
config.numItemsToPreemptivelyEvict,
)
}