mirror of
https://github.com/chubin/wttr.in
synced 2024-11-14 16:17:19 +00:00
Move global consts to config
This commit is contained in:
parent
28f1fd9aae
commit
30c2c85e54
4 changed files with 57 additions and 24 deletions
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue