* move "client", "transport" to "config"

This commit is contained in:
Simon Zolin 2019-07-09 18:37:24 +03:00
parent 2682adca39
commit c426ee0108
5 changed files with 17 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package home
import ( import (
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -54,6 +55,8 @@ type configuration struct {
appSignalChannel chan os.Signal appSignalChannel chan os.Signal
clients clientsContainer clients clientsContainer
controlLock sync.Mutex controlLock sync.Mutex
transport *http.Transport
client *http.Client
BindHost string `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to BindHost string `yaml:"bind_host"` // BindHost is the IP address of the HTTP server to bind to
BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server
@ -169,8 +172,16 @@ var config = configuration{
SchemaVersion: currentSchemaVersion, SchemaVersion: currentSchemaVersion,
} }
// init initializes default configuration for the current OS&ARCH // initConfig initializes default configuration for the current OS&ARCH
func init() { func initConfig() {
config.transport = &http.Transport{
DialContext: customDialContext,
}
config.client = &http.Client{
Timeout: time.Minute * 5,
Transport: config.transport,
}
if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" { if runtime.GOARCH == "mips" || runtime.GOARCH == "mipsle" {
// Use plain DNS on MIPS, encryption is too slow // Use plain DNS on MIPS, encryption is too slow
defaultDNS = []string{"1.1.1.1", "1.0.0.1"} defaultDNS = []string{"1.1.1.1", "1.0.0.1"}

View File

@ -30,15 +30,6 @@ var versionCheckLastTime time.Time
var protocols = []string{"tls://", "https://", "tcp://", "sdns://"} var protocols = []string{"tls://", "https://", "tcp://", "sdns://"}
var transport = &http.Transport{
DialContext: customDialContext,
}
var client = &http.Client{
Timeout: time.Minute * 5,
Transport: transport,
}
// ---------------- // ----------------
// helper functions // helper functions
// ---------------- // ----------------

View File

@ -87,7 +87,7 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
} }
log.Tracef("Downloading data from %s", versionCheckURL) log.Tracef("Downloading data from %s", versionCheckURL)
resp, err := client.Get(versionCheckURL) resp, err := config.client.Get(versionCheckURL)
if err != nil { if err != nil {
httpError(w, http.StatusBadGateway, "Couldn't get version check json from %s: %T %s\n", versionCheckURL, err, err) httpError(w, http.StatusBadGateway, "Couldn't get version check json from %s: %T %s\n", versionCheckURL, err, err)
return return
@ -349,7 +349,7 @@ func copySupportingFiles(files []string, srcdir, dstdir string, useSrcNameOnly,
// Download package file and save it to disk // Download package file and save it to disk
func getPackageFile(u *updateInfo) error { func getPackageFile(u *updateInfo) error {
resp, err := client.Get(u.pkgURL) resp, err := config.client.Get(u.pkgURL)
if err != nil { if err != nil {
return fmt.Errorf("HTTP request failed: %s", err) return fmt.Errorf("HTTP request failed: %s", err)
} }

View File

@ -308,7 +308,7 @@ func parseFilterContents(contents []byte) (int, string) {
func (filter *filter) update() (bool, error) { func (filter *filter) update() (bool, error) {
log.Tracef("Downloading update for filter %d from %s", filter.ID, filter.URL) log.Tracef("Downloading update for filter %d from %s", filter.ID, filter.URL)
resp, err := client.Get(filter.URL) resp, err := config.client.Get(filter.URL)
if resp != nil && resp.Body != nil { if resp != nil && resp.Body != nil {
defer resp.Body.Close() defer resp.Body.Close()
} }

View File

@ -108,6 +108,7 @@ func run(args options) {
os.Exit(0) os.Exit(0)
}() }()
initConfig()
config.clients.Init() config.clients.Init()
if !config.firstRun { if !config.firstRun {