* 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 (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
@ -54,6 +55,8 @@ type configuration struct {
appSignalChannel chan os.Signal
clients clientsContainer
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
BindPort int `yaml:"bind_port"` // BindPort is the port the HTTP server
@ -169,8 +172,16 @@ var config = configuration{
SchemaVersion: currentSchemaVersion,
}
// init initializes default configuration for the current OS&ARCH
func init() {
// initConfig initializes default configuration for the current OS&ARCH
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" {
// Use plain DNS on MIPS, encryption is too slow
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 transport = &http.Transport{
DialContext: customDialContext,
}
var client = &http.Client{
Timeout: time.Minute * 5,
Transport: transport,
}
// ----------------
// helper functions
// ----------------

View File

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

View File

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