mirror of
https://github.com/chubin/wttr.in
synced 2025-01-12 12:08:47 +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
|
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)
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,8 +39,31 @@ type Server struct {
|
||||||
TLSKeyFile string
|
TLSKeyFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conf contains the current configuration.
|
// Uplink configuration.
|
||||||
var Conf = Config{
|
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{
|
Logging{
|
||||||
AccessLog: "/wttr.in/log/access.log",
|
AccessLog: "/wttr.in/log/access.log",
|
||||||
ErrorsLog: "/wttr.in/log/errors.log",
|
ErrorsLog: "/wttr.in/log/errors.log",
|
||||||
|
@ -50,4 +75,13 @@ var Conf = Config{
|
||||||
TLSCertFile: "/wttr.in/etc/fullchain.pem",
|
TLSCertFile: "/wttr.in/etc/fullchain.pem",
|
||||||
TLSKeyFile: "/wttr.in/etc/privkey.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