/
main.go
182 lines (143 loc) · 3.58 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"context"
"log"
"os"
"os/signal"
"github.com/7phs/kvs/internal/config"
"github.com/7phs/kvs/internal/server"
"github.com/7phs/kvs/internal/storages"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
errExitCode = 2
)
func logLevel(conf config.Config) zap.AtomicLevel {
var level zapcore.Level
switch conf.LogLevel() {
case config.LogLevelDebug:
level = zapcore.DebugLevel
case config.LogLevelInfo:
level = zapcore.InfoLevel
case config.LogLevelWarning:
level = zapcore.WarnLevel
case config.LogLevelError:
level = zapcore.ErrorLevel
default:
level = zapcore.InfoLevel
}
return zap.NewAtomicLevelAt(level)
}
func buildLogger(conf config.Config) (*zap.Logger, error) {
logConfig := zap.NewProductionConfig()
logConfig.Level = logLevel(conf)
return logConfig.Build()
}
func newMapDictionary(conf config.Config) (storages.DataDictionary, error) {
memoryPool := storages.NewMemoryPool(conf.PreAllocated())
pool, err := storages.NewDataPool(memoryPool)
if err != nil {
return nil, err
}
return storages.NewMapDictionary(pool), nil
}
func newSyncMapDictionary(conf config.Config) (storages.DataDictionary, error) {
memoryPool := storages.NewMemoryPool(conf.PreAllocated())
pool, err := storages.NewDataPool(memoryPool)
if err != nil {
return nil, err
}
return storages.NewSyncMapDictionary(pool), nil
}
func initStorages(conf config.Config) (dictionary storages.DataDictionary, err error) {
switch conf.Mode() {
case config.StorageModeMap:
return newMapDictionary(conf)
case config.StorageModeSyncMap:
return newSyncMapDictionary(conf)
case config.StorageModePartitionedMap:
return storages.NewPartitionedDictionary(
storages.DefaultPartitionNum,
storages.DefaultPartitionMask,
func() (storages.DataDictionary, error) {
return newMapDictionary(conf)
},
)
case config.StorageModePartitionedSyncMap:
return storages.NewPartitionedDictionary(
storages.DefaultPartitionNum,
storages.DefaultPartitionMask,
func() (storages.DataDictionary, error) {
return newSyncMapDictionary(conf)
},
)
}
return
}
func main() {
conf, err := config.NewConfigFromEnv()
if err != nil {
log.Fatal("failed to prepare config: %w", err)
}
logger, err := buildLogger(conf)
if err != nil {
log.Fatal("failed to init logger: %w", err)
}
defer func() {
_ = logger.Sync()
}()
logger.Info("APP RUN")
logger.Info("config",
zap.String(config.LOGLEVEL, string(conf.LogLevel())),
zap.Int(config.PORT, conf.Port()),
zap.Duration(config.EXPIRATION, conf.Expiration()),
zap.Duration(config.MAINTENANCE, conf.Maintenance()),
zap.Int(config.PREALLOCATED, conf.PreAllocated()),
zap.String(config.MODE, string(conf.Mode())),
)
logger.Info("init: data dictionary")
dictionary, err := initStorages(conf)
if err != nil {
logger.Fatal("failed to init data dictionary")
}
logger.Info("init: storages")
storages, err := storages.NewInMemStorages(
conf,
dictionary,
)
if err != nil {
logger.Fatal("failed to init data pool",
zap.Error(err),
)
}
logger.Info("init: server")
srv := server.NewServer(
logger,
conf,
storages,
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
logger.Info("interrupt")
cancel()
}()
go func() {
logger.Info("start: server")
err := srv.Start()
if err != nil {
logger.Fatal("failed to start server",
zap.Error(err),
)
os.Exit(errExitCode)
}
}()
<-ctx.Done()
logger.Info("stop: server")
srv.Stop()
logger.Info("finish")
}