Add graceful stopping to server, and extend middleware and pipeline logic

This commit is contained in:
Pablu23
2025-10-01 14:36:30 +02:00
parent 66f2811fff
commit 572e1177ef
5 changed files with 113 additions and 25 deletions

View File

@@ -17,6 +17,7 @@ type Metrics struct {
endpointMetrics []EndpointMetrics
ticker *time.Ticker
file string
stop chan bool
}
type EndpointMetrics struct {
@@ -44,7 +45,7 @@ func NewMetrics(bufferSize int, flushTimeout time.Duration, file string) *Metric
}
}
func (m *Metrics) RequestMetrics(next http.Handler) http.Handler {
func (m *Metrics) Use(next http.Handler) http.Handler {
log.Info().Msg("Enabling Request Metrics")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
@@ -71,6 +72,8 @@ func (m *Metrics) Manage() {
m.calculateDuration(rm)
case <-m.ticker.C:
m.Flush()
case <-m.stop:
return
}
}
}
@@ -129,3 +132,13 @@ func (m *Metrics) Flush() {
log.Info().Str("file", m.file).Int("count", len(a)).Msg("Completed Metrics flush")
}
func (m *Metrics) Stop() {
log.Info().Msg("Stopping Request Metrics")
for len(m.c) > 0 {
rm := <- m.c
m.calculateDuration(rm)
}
m.Flush()
log.Info().Msg("Stopped Request Metrics")
}