generated from Yandex-Practicum/go-musthave-metrics-tpl
/
app.go
428 lines (374 loc) · 11.5 KB
/
app.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Package app provides functions to work with main server - metric collector
package app
import (
"context"
"errors"
"net/http"
"net/http/pprof"
"os"
"runtime"
rpprof "runtime/pprof"
"sync"
"time"
pb "github.com/kvvPro/metric-collector/proto"
"google.golang.org/grpc"
"github.com/go-chi/chi/v5"
"github.com/kvvPro/metric-collector/cmd/server/config"
"github.com/kvvPro/metric-collector/internal/metrics"
"github.com/kvvPro/metric-collector/internal/retry"
"github.com/kvvPro/metric-collector/internal/storage"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
"github.com/kvvPro/metric-collector/internal/storage/memstorage"
"github.com/kvvPro/metric-collector/internal/storage/postgres"
)
type Server struct {
// Main storage for metrics, can be memstorage or postgresql type
storage storage.Storage
// Address of web server, where app woul be deployed
// Format - Host[:Port]
Address string
// Interval in seconds for backup storage on disk
StoreInterval int
// Path to save storage with interval StoreInterval
FileStoragePath string
// True if server would restore metrics from backup file
Restore bool
// String connection to postgres DB
// Format - "user=<user> password=<pass> host=<host> port=<port> dbname=<db> sslmode=<true/false>"
DBConnection string
// "db" or "memory"
StorageType string
// Key for decrypt and encrypt body of requests
HashKey string
// True if server would validate hash of all incoming requests
CheckHash bool
// True if server accepts encrypted messages from agent
UseEncryption bool
// Path to private key RSA
PrivateKeyPath string
// Trusted subnet to check clients ip addresses
TrustedSubnet string
// http server
HTTPServer *http.Server
// Path to file where mem stats will be saved
MemProfile string
// Exchange mode
ExchangeMode string
// wait group for async saving
wg *sync.WaitGroup
// func to cancel ctx in asunc saving
cancelSaving context.CancelFunc
// implement GRPC server
pb.UnimplementedMetricServerServer
// GRPC server
grpcServer *grpc.Server
}
const (
DatabaseStorageType = "db"
MemStorageType = "memory"
)
// NewServer creates app instance
func NewServer(settings *config.ServerFlags) (*Server, error) {
var t string
var st storage.Storage
if settings.DBConnection != "" {
t = DatabaseStorageType
newdb, err := postgres.NewPSQLStr(context.Background(), settings.DBConnection)
if err != nil {
return nil, err
}
st = newdb
} else {
t = MemStorageType
newmem := memstorage.NewMemStorage()
st = &newmem
}
if st == nil {
return nil, errors.New("cannot create storage for server")
}
return &Server{
storage: st,
Address: settings.Address,
StoreInterval: settings.StoreInterval,
FileStoragePath: settings.FileStoragePath,
Restore: settings.Restore,
DBConnection: settings.DBConnection,
StorageType: t,
HashKey: settings.HashKey,
CheckHash: settings.HashKey != "",
PrivateKeyPath: settings.CryptoKey,
UseEncryption: settings.CryptoKey != "",
MemProfile: settings.MemProfile,
TrustedSubnet: settings.TrustedSubnet,
ExchangeMode: settings.ExchangeMode,
}, nil
}
func (srv *Server) StartServer(ctx context.Context, srvFlags *config.ServerFlags) {
Sugar.Infoln("before restoring values")
srv.RestoreValues(ctx)
Sugar.Infoln("after restoring values")
// записываем в лог, что сервер запускается
Sugar.Infow(
"Starting server",
"srvFlags", srvFlags,
)
if srv.ExchangeMode == "http" {
srv.startHTTPServer()
} else if srv.ExchangeMode == "grpc" {
srv.startGRPCServer(ctx)
} else {
Sugar.Fatalf("uknown exchange mode: %v", srv.ExchangeMode)
}
asyncCtx, cancel := context.WithCancel(ctx)
srv.cancelSaving = cancel
srv.AsyncSaving(asyncCtx)
}
func (srv *Server) startHTTPServer() {
r := chi.NewMux()
r.Use(srv.ValidateIP,
srv.DecryptMiddleware,
srv.CheckHashMiddleware,
GzipMiddleware,
WithLogging)
// r.Use(app.WithLogging)
r.Handle("/ping", http.HandlerFunc(srv.PingHandle))
r.Handle("/updates/", http.HandlerFunc(srv.UpdateBatchJSONHandle))
r.Handle("/update/", http.HandlerFunc(srv.UpdateJSONHandle))
r.Handle("/update/*", http.HandlerFunc(srv.UpdateHandle))
r.Handle("/value/*", http.HandlerFunc(srv.GetValueHandle))
r.Handle("/value/", http.HandlerFunc(srv.GetValueJSONHandle))
r.Handle("/", http.HandlerFunc(srv.AllMetricsHandle))
r.Handle("/debug/pprof", http.HandlerFunc(pprof.Index))
r.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
r.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
r.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
// Manually add support for paths linked to by index page at /debug/pprof/
r.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
r.Handle("/debug/pprof/heap", pprof.Handler("heap"))
r.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
r.Handle("/debug/pprof/block", pprof.Handler("block"))
srv.HTTPServer = &http.Server{
Addr: srv.Address,
Handler: r,
}
go func() {
if err := srv.HTTPServer.ListenAndServe(); err != http.ErrServerClosed {
// записываем в лог ошибку, если сервер не запустился
Sugar.Fatalw(err.Error(), "event", "start server")
}
}()
}
func (srv *Server) StopServer(ctx context.Context) {
// создаём файл журнала профилирования памяти
fmem, err := os.Create(srv.MemProfile)
if err != nil {
panic(err)
}
defer fmem.Close()
runtime.GC() // получаем статистику по использованию памяти
if err := rpprof.WriteHeapProfile(fmem); err != nil {
Sugar.Fatalw("Error to make heap profile:", err.Error())
}
Sugar.Infoln("Try to save metrics...")
err = srv.SaveToFile(ctx)
if err != nil {
Sugar.Infoln("Save to file failed: ", err.Error())
}
Sugar.Infoln("Metrics saved")
if srv.ExchangeMode == "http" {
srv.stopHTTPServer(ctx)
} else if srv.ExchangeMode == "grpc" {
srv.stopGRPCServer(ctx)
} else {
Sugar.Fatalf("uknown exchange mode: %v", srv.ExchangeMode)
}
srv.StopAsyncSaving()
}
func (srv *Server) stopHTTPServer(ctx context.Context) {
timeout, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
Sugar.Infoln("Попытка мягко завершить сервер")
if err := srv.HTTPServer.Shutdown(timeout); err != nil {
Sugar.Errorf("Ошибка при попытке мягко завершить http-сервер: %v", err)
// handle err
if err = srv.HTTPServer.Close(); err != nil {
Sugar.Errorf("Ошибка при попытке завершить http-сервер: %v", err)
}
}
}
// Ping tests connection to db
func (srv *Server) Ping(ctx context.Context) error {
return srv.storage.Ping(ctx)
}
// AddMetric adds new metric if it doesn't exist, or update existing metric with name metricName
//
// Deprecated: use AddMetricNew
func (srv *Server) AddMetric(ctx context.Context, metricType string, metricName string, metricValue string) error {
err := srv.storage.Update(ctx, metricType, metricName, metricValue)
if err != nil {
return err
}
return nil
}
// AddMetricNew adds new metric if it doesn't exist, or update existing metric with name metricName
func (srv *Server) AddMetricNew(ctx context.Context, m metrics.Metric) error {
var err error
err = retry.Do(func() error {
return srv.storage.UpdateNew(context.Background(), m.MType, m.ID, m.Delta, m.Value)
},
retry.RetryIf(func(errAttempt error) bool {
var pgErr *pgconn.PgError
if errors.As(errAttempt, &pgErr) && pgerrcode.IsConnectionException(pgErr.Code) {
return true
}
return false
}),
retry.Attempts(3),
retry.InitDelay(1000*time.Millisecond),
retry.Step(2000*time.Millisecond),
retry.Context(ctx),
)
if err != nil {
Sugar.Errorln(err)
return err
}
if srv.StoreInterval == 0 {
err = srv.SaveToFile(ctx)
if err != nil {
Sugar.Infoln("Save to file failed: ", err.Error())
}
}
return nil
}
// AddMetricsBatch adds array of new metrics.
// Behavior is the same as in AddMetricNew
func (srv *Server) AddMetricsBatch(ctx context.Context, m []metrics.Metric) error {
var err error
err = retry.Do(func() error {
return srv.storage.UpdateBatch(context.Background(), m)
},
retry.RetryIf(func(errAttempt error) bool {
var pgErr *pgconn.PgError
if errors.As(errAttempt, &pgErr) && pgerrcode.IsConnectionException(pgErr.Code) {
return true
}
return false
}),
retry.Attempts(3),
retry.InitDelay(1000*time.Millisecond),
retry.Step(2000*time.Millisecond),
retry.Context(ctx),
)
if err != nil {
Sugar.Errorln(err)
return err
}
if srv.StoreInterval == 0 {
err = srv.SaveToFile(ctx)
if err != nil {
Sugar.Infoln("Save to file failed: ", err.Error())
}
}
return nil
}
// GetMetricValue returns current value of metric
func (srv *Server) GetMetricValue(ctx context.Context, metricType string, metricName string) (any, error) {
val, err := srv.storage.GetValue(ctx, metricType, metricName)
return val, err
}
// GetRequestedValues returns current values of requested metrics.
// return only already existed metrics
func (srv *Server) GetRequestedValues(ctx context.Context, m []metrics.Metric) ([]metrics.Metric, error) {
slice, err := srv.GetAllMetricsNew(ctx)
if err != nil {
return nil, err
}
hash := make(map[string]*metrics.Metric, 0)
for _, el := range slice {
hash[el.ID] = el
}
result := make([]metrics.Metric, 0)
for _, el := range m {
metricID := el.ID
if _, isExist := hash[metricID]; isExist && el.MType == hash[metricID].MType {
// add element with updated value
result = append(result, *hash[metricID])
} else {
// keep requested value
if el.MType == metrics.MetricTypeCounter {
el.Delta = new(int64)
} else {
el.Value = new(float64)
}
result = append(result, el)
}
}
return result, nil
}
// GetAllMetricsNew returns all existed metrics with current values
func (srv *Server) GetAllMetricsNew(ctx context.Context) ([]*metrics.Metric, error) {
var val []*metrics.Metric
var err error
err = retry.Do(func() error {
val, err = srv.storage.GetAllMetricsNew(context.Background())
return err
},
retry.RetryIf(func(errAttempt error) bool {
var pgErr *pgconn.PgError
if errors.As(errAttempt, &pgErr) && pgerrcode.IsConnectionException(pgErr.Code) {
return true
}
return false
}),
retry.Attempts(3),
retry.InitDelay(1000*time.Millisecond),
retry.Step(2000*time.Millisecond),
retry.Context(ctx),
)
if err != nil {
Sugar.Errorln(err)
return nil, err
}
return val, nil
}
// AsyncSaving backups database to file
func (srv *Server) AsyncSaving(ctx context.Context) {
srv.wg = &sync.WaitGroup{}
srv.wg.Add(1)
go func() {
defer srv.wg.Done()
// run if only StoreInterval > 0, if StoreInterval = 0 => sync writing after each update
// and FileStoragePath != ""
if srv.StoreInterval > 0 && srv.FileStoragePath != "" {
for {
select {
case <-time.After(time.Duration(srv.StoreInterval) * time.Second):
case <-ctx.Done():
Sugar.Infoln("остановка асинхронного сохранения")
return
}
err := srv.SaveToFile(ctx)
if err != nil {
Sugar.Infoln("Save to file failed: ", err.Error())
}
}
}
}()
}
func (srv *Server) StopAsyncSaving() {
srv.cancelSaving()
srv.wg.Wait()
}
// RestoreValues restore metrics from file
func (srv *Server) RestoreValues(ctx context.Context) {
if srv.Restore && srv.StorageType == "memory" {
m, err := srv.ReadFromFile()
if err != nil {
Sugar.Infoln("Read values failed: ", err.Error())
}
for _, el := range m {
srv.AddMetricNew(ctx, el)
}
}
}