From 30c2c85e54e5e84dd010ded3d7dd4fe473218f4f Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Fri, 2 Dec 2022 20:10:32 +0100 Subject: [PATCH] Move global consts to config --- cmd/peakHandling.go | 2 +- cmd/processRequest.go | 13 +++++---- cmd/srv.go | 6 +--- internal/config/config.go | 60 ++++++++++++++++++++++++++++++--------- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/cmd/peakHandling.go b/cmd/peakHandling.go index 461f7ff..0195c3c 100644 --- a/cmd/peakHandling.go +++ b/cmd/peakHandling.go @@ -62,7 +62,7 @@ func (rp *RequestProcessor) prefetchPeakRequests(peakRequestMap *sync.Map) { return } log.Printf("PREFETCH: Prefetching %d requests\n", peakRequestLen) - sleepBetweenRequests := time.Duration(prefetchInterval*1000/peakRequestLen) * time.Millisecond + sleepBetweenRequests := time.Duration(rp.config.Uplink.PrefetchInterval*1000/peakRequestLen) * time.Millisecond peakRequestMap.Range(func(key interface{}, value interface{}) bool { go func(r http.Request) { rp.prefetchRequest(&r) diff --git a/cmd/processRequest.go b/cmd/processRequest.go index 8080115..a010978 100644 --- a/cmd/processRequest.go +++ b/cmd/processRequest.go @@ -12,6 +12,7 @@ import ( "sync" "time" + "github.com/chubin/wttr.in/internal/config" "github.com/chubin/wttr.in/internal/routing" lru "github.com/hashicorp/golang-lru" @@ -34,24 +35,25 @@ type RequestProcessor struct { stats *Stats router routing.Router upstreamTransport *http.Transport + config *config.Config } // NewRequestProcessor returns new RequestProcessor. -func NewRequestProcessor() (*RequestProcessor, error) { - lruCache, err := lru.New(lruCacheSize) +func NewRequestProcessor(config *config.Config) (*RequestProcessor, error) { + lruCache, err := lru.New(config.Cache.Size) if err != nil { return nil, err } dialer := &net.Dialer{ - Timeout: uplinkTimeout * time.Second, - KeepAlive: uplinkTimeout * time.Second, + Timeout: time.Duration(config.Uplink.Timeout) * time.Second, + KeepAlive: time.Duration(config.Uplink.Timeout) * time.Second, DualStack: true, } transport := &http.Transport{ DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) { - return dialer.DialContext(ctx, network, uplinkSrvAddr) + return dialer.DialContext(ctx, network, config.Uplink.Address) }, } @@ -59,6 +61,7 @@ func NewRequestProcessor() (*RequestProcessor, error) { lruCache: lruCache, stats: NewStats(), upstreamTransport: transport, + config: config, } // Initialize routes. diff --git a/cmd/srv.go b/cmd/srv.go index 28bd2ce..6246eb4 100644 --- a/cmd/srv.go +++ b/cmd/srv.go @@ -11,10 +11,6 @@ import ( "github.com/chubin/wttr.in/internal/config" ) -const uplinkSrvAddr = "127.0.0.1:9002" -const uplinkTimeout = 30 -const prefetchInterval = 300 -const lruCacheSize = 12800 const logLineStart = "LOG_LINE_START " // plainTextAgents contains signatures of the plain-text agents @@ -105,7 +101,7 @@ func main() { err error ) - rp, err = NewRequestProcessor() + rp, err = NewRequestProcessor(config.Conf) if err != nil { log.Fatalln("log processor initialization:", err) } diff --git a/internal/config/config.go b/internal/config/config.go index 8ccd493..8722658 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,8 +2,10 @@ package config // Config of the program. type Config struct { + Cache Logging Server + Uplink } // Logging configuration. @@ -37,17 +39,49 @@ type Server struct { TLSKeyFile string } -// Conf contains the current configuration. -var Conf = Config{ - Logging{ - AccessLog: "/wttr.in/log/access.log", - ErrorsLog: "/wttr.in/log/errors.log", - Interval: 300, - }, - Server{ - PortHTTP: 8083, - PortHTTPS: 8084, - TLSCertFile: "/wttr.in/etc/fullchain.pem", - TLSKeyFile: "/wttr.in/etc/privkey.pem", - }, +// Uplink configuration. +type Uplink struct { + // Address contains address of the uplink server in form IP:PORT. + Address string + + // Timeout for upstream queries. + Timeout int + + // PrefetchInterval contains time (in milliseconds) indicating, + // how long the prefetch procedure should take. + PrefetchInterval int +} + +// Cache configuration. +type Cache struct { + // Size of the main cache. + Size int } + +// Default contains the default configuration. +func Default() *Config { + return &Config{ + Cache{ + Size: 12800, + }, + Logging{ + AccessLog: "/wttr.in/log/access.log", + ErrorsLog: "/wttr.in/log/errors.log", + Interval: 300, + }, + Server{ + PortHTTP: 8083, + PortHTTPS: 8084, + TLSCertFile: "/wttr.in/etc/fullchain.pem", + TLSKeyFile: "/wttr.in/etc/privkey.pem", + }, + Uplink{ + Address: "127.0.0.1:9002", + Timeout: 30, + PrefetchInterval: 300, + }, + } +} + +// Conf contains the current configuration +var Conf = Default()