Move global consts to config

This commit is contained in:
Igor Chubin 2022-12-02 20:10:32 +01:00
parent 28f1fd9aae
commit 30c2c85e54
4 changed files with 57 additions and 24 deletions

View file

@ -62,7 +62,7 @@ func (rp *RequestProcessor) prefetchPeakRequests(peakRequestMap *sync.Map) {
return return
} }
log.Printf("PREFETCH: Prefetching %d requests\n", peakRequestLen) 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 { peakRequestMap.Range(func(key interface{}, value interface{}) bool {
go func(r http.Request) { go func(r http.Request) {
rp.prefetchRequest(&r) rp.prefetchRequest(&r)

View file

@ -12,6 +12,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/chubin/wttr.in/internal/config"
"github.com/chubin/wttr.in/internal/routing" "github.com/chubin/wttr.in/internal/routing"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
@ -34,24 +35,25 @@ type RequestProcessor struct {
stats *Stats stats *Stats
router routing.Router router routing.Router
upstreamTransport *http.Transport upstreamTransport *http.Transport
config *config.Config
} }
// NewRequestProcessor returns new RequestProcessor. // NewRequestProcessor returns new RequestProcessor.
func NewRequestProcessor() (*RequestProcessor, error) { func NewRequestProcessor(config *config.Config) (*RequestProcessor, error) {
lruCache, err := lru.New(lruCacheSize) lruCache, err := lru.New(config.Cache.Size)
if err != nil { if err != nil {
return nil, err return nil, err
} }
dialer := &net.Dialer{ dialer := &net.Dialer{
Timeout: uplinkTimeout * time.Second, Timeout: time.Duration(config.Uplink.Timeout) * time.Second,
KeepAlive: uplinkTimeout * time.Second, KeepAlive: time.Duration(config.Uplink.Timeout) * time.Second,
DualStack: true, DualStack: true,
} }
transport := &http.Transport{ transport := &http.Transport{
DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) { 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, lruCache: lruCache,
stats: NewStats(), stats: NewStats(),
upstreamTransport: transport, upstreamTransport: transport,
config: config,
} }
// Initialize routes. // Initialize routes.

View file

@ -11,10 +11,6 @@ import (
"github.com/chubin/wttr.in/internal/config" "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 " const logLineStart = "LOG_LINE_START "
// plainTextAgents contains signatures of the plain-text agents // plainTextAgents contains signatures of the plain-text agents
@ -105,7 +101,7 @@ func main() {
err error err error
) )
rp, err = NewRequestProcessor() rp, err = NewRequestProcessor(config.Conf)
if err != nil { if err != nil {
log.Fatalln("log processor initialization:", err) log.Fatalln("log processor initialization:", err)
} }

View file

@ -2,8 +2,10 @@ package config
// Config of the program. // Config of the program.
type Config struct { type Config struct {
Cache
Logging Logging
Server Server
Uplink
} }
// Logging configuration. // Logging configuration.
@ -37,17 +39,49 @@ type Server struct {
TLSKeyFile string TLSKeyFile string
} }
// Conf contains the current configuration. // Uplink configuration.
var Conf = Config{ type Uplink struct {
Logging{ // Address contains address of the uplink server in form IP:PORT.
AccessLog: "/wttr.in/log/access.log", Address string
ErrorsLog: "/wttr.in/log/errors.log",
Interval: 300, // Timeout for upstream queries.
}, Timeout int
Server{
PortHTTP: 8083, // PrefetchInterval contains time (in milliseconds) indicating,
PortHTTPS: 8084, // how long the prefetch procedure should take.
TLSCertFile: "/wttr.in/etc/fullchain.pem", PrefetchInterval int
TLSKeyFile: "/wttr.in/etc/privkey.pem",
},
} }
// 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()