From 7c7117d8d6e9b7c8bf6c0ebffa7063d3a5696dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Mon, 12 Apr 2021 08:51:50 +0200 Subject: [PATCH] Tidy webserver --- version.go | 2 +- web/server.go | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/version.go b/version.go index f2335b4..24d443e 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,4 @@ package apiary -var Version = "v0.1.0" +var Version = "v0.1.1" var Build string diff --git a/web/server.go b/web/server.go index 812e720..3854f86 100644 --- a/web/server.go +++ b/web/server.go @@ -22,23 +22,23 @@ import ( "golang.org/x/crypto/acme/autocert" ) +const streamKeepAliveDuration = 30 * time.Second + type Server struct { http.Server + httpRedirectServer http.Server cfg config.FrontendConfig - store store.LoginAttemptStore + honeypotServer *honeypot.HoneypotServer + store store.LoginAttemptStore ServerLogger *zap.SugaredLogger AccessLogger *zap.SugaredLogger - honeypotServer *honeypot.HoneypotServer - attemptListenersLock sync.RWMutex attemptListeners map[string]chan models.LoginAttempt - - streamContext context.Context - httpRedirectServer http.Server + streamContext context.Context } func NewServer(cfg config.FrontendConfig, hs *honeypot.HoneypotServer, store store.LoginAttemptStore) *Server { @@ -155,7 +155,7 @@ func (s *Server) HandlerAttemptStream(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) flusher := w.(http.Flusher) - ticker := time.NewTicker(30 * time.Second) + ticker := time.NewTicker(streamKeepAliveDuration) defer ticker.Stop() for { @@ -170,6 +170,7 @@ func (s *Server) HandlerAttemptStream(w http.ResponseWriter, r *http.Request) { s.ServerLogger.Warnw("Error writing event", "error", err) } flusher.Flush() + ticker.Reset(streamKeepAliveDuration) case <-s.streamContext.Done(): return case <-r.Context().Done():