frontend: add connection timeout
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -14,6 +15,23 @@ type channelData struct {
|
|||||||
data string
|
data string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createConnectionTimeoutRoundTripper(timeout int) http.RoundTripper {
|
||||||
|
context := net.Dialer{
|
||||||
|
Timeout: time.Duration(timeout) * time.Second,
|
||||||
|
}
|
||||||
|
return &http.Transport{
|
||||||
|
DialContext: context.DialContext,
|
||||||
|
|
||||||
|
// Default options from transport.go
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
|
ForceAttemptHTTP2: true,
|
||||||
|
MaxIdleConns: 100,
|
||||||
|
IdleConnTimeout: 90 * time.Second,
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Send commands to lgproxy instances in parallel, and retrieve their responses
|
// Send commands to lgproxy instances in parallel, and retrieve their responses
|
||||||
func batchRequest(servers []string, endpoint string, command string) []string {
|
func batchRequest(servers []string, endpoint string, command string) []string {
|
||||||
// Channel and array for storing responses
|
// Channel and array for storing responses
|
||||||
@@ -47,7 +65,9 @@ func batchRequest(servers []string, endpoint string, command string) []string {
|
|||||||
}
|
}
|
||||||
url := "http://" + hostname + ":" + strconv.Itoa(setting.proxyPort) + "/" + url.PathEscape(endpoint) + "?q=" + url.QueryEscape(command)
|
url := "http://" + hostname + ":" + strconv.Itoa(setting.proxyPort) + "/" + url.PathEscape(endpoint) + "?q=" + url.QueryEscape(command)
|
||||||
go func(url string, i int) {
|
go func(url string, i int) {
|
||||||
client := http.Client{Timeout: time.Duration(setting.timeOut) * time.Second}
|
client := http.Client{
|
||||||
|
Transport: createConnectionTimeoutRoundTripper(setting.connectionTimeOut),
|
||||||
|
Timeout: time.Duration(setting.timeOut) * time.Second}
|
||||||
response, err := client.Get(url)
|
response, err := client.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ch <- channelData{i, "request failed: " + err.Error() + "\n"}
|
ch <- channelData{i, "request failed: " + err.Error() + "\n"}
|
||||||
|
|||||||
@@ -7,24 +7,25 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type settingType struct {
|
type settingType struct {
|
||||||
servers []string
|
servers []string
|
||||||
serversDisplay []string
|
serversDisplay []string
|
||||||
domain string
|
domain string
|
||||||
proxyPort int
|
proxyPort int
|
||||||
whoisServer string
|
whoisServer string
|
||||||
listen string
|
listen string
|
||||||
dnsInterface string
|
dnsInterface string
|
||||||
netSpecificMode string
|
netSpecificMode string
|
||||||
titleBrand string
|
titleBrand string
|
||||||
navBarBrand string
|
navBarBrand string
|
||||||
navBarBrandURL string
|
navBarBrandURL string
|
||||||
navBarAllServer string
|
navBarAllServer string
|
||||||
navBarAllURL string
|
navBarAllURL string
|
||||||
bgpmapInfo string
|
bgpmapInfo string
|
||||||
telegramBotName string
|
telegramBotName string
|
||||||
protocolFilter []string
|
protocolFilter []string
|
||||||
nameFilter string
|
nameFilter string
|
||||||
timeOut int
|
timeOut int
|
||||||
|
connectionTimeOut int
|
||||||
}
|
}
|
||||||
|
|
||||||
var setting settingType
|
var setting settingType
|
||||||
|
|||||||
@@ -9,23 +9,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type viperSettingType struct {
|
type viperSettingType struct {
|
||||||
Servers string `mapstructure:"servers"`
|
Servers string `mapstructure:"servers"`
|
||||||
Domain string `mapstructure:"domain"`
|
Domain string `mapstructure:"domain"`
|
||||||
ProxyPort int `mapstructure:"proxy_port"`
|
ProxyPort int `mapstructure:"proxy_port"`
|
||||||
WhoisServer string `mapstructure:"whois"`
|
WhoisServer string `mapstructure:"whois"`
|
||||||
Listen string `mapstructure:"listen"`
|
Listen string `mapstructure:"listen"`
|
||||||
DNSInterface string `mapstructure:"dns_interface"`
|
DNSInterface string `mapstructure:"dns_interface"`
|
||||||
NetSpecificMode string `mapstructure:"net_specific_mode"`
|
NetSpecificMode string `mapstructure:"net_specific_mode"`
|
||||||
TitleBrand string `mapstructure:"title_brand"`
|
TitleBrand string `mapstructure:"title_brand"`
|
||||||
NavBarBrand string `mapstructure:"navbar_brand"`
|
NavBarBrand string `mapstructure:"navbar_brand"`
|
||||||
NavBarBrandURL string `mapstructure:"navbar_brand_url"`
|
NavBarBrandURL string `mapstructure:"navbar_brand_url"`
|
||||||
NavBarAllServer string `mapstructure:"navbar_all_servers"`
|
NavBarAllServer string `mapstructure:"navbar_all_servers"`
|
||||||
NavBarAllURL string `mapstructure:"navbar_all_url"`
|
NavBarAllURL string `mapstructure:"navbar_all_url"`
|
||||||
BgpmapInfo string `mapstructure:"bgpmap_info"`
|
BgpmapInfo string `mapstructure:"bgpmap_info"`
|
||||||
TelegramBotName string `mapstructure:"telegram_bot_name"`
|
TelegramBotName string `mapstructure:"telegram_bot_name"`
|
||||||
ProtocolFilter string `mapstructure:"protocol_filter"`
|
ProtocolFilter string `mapstructure:"protocol_filter"`
|
||||||
NameFilter string `mapstructure:"name_filter"`
|
NameFilter string `mapstructure:"name_filter"`
|
||||||
TimeOut int `mapstructure:"timeout"`
|
TimeOut int `mapstructure:"timeout"`
|
||||||
|
ConnectionTimeOut int `mapstructure:"connection_timeout"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse settings with viper, and convert to legacy setting format
|
// Parse settings with viper, and convert to legacy setting format
|
||||||
@@ -87,9 +88,12 @@ func parseSettings() {
|
|||||||
pflag.String("name-filter", "", "protocol name regex to hide in summary tables (RE2 syntax); defaults to none if not set")
|
pflag.String("name-filter", "", "protocol name regex to hide in summary tables (RE2 syntax); defaults to none if not set")
|
||||||
viper.BindPFlag("name_filter", pflag.Lookup("name-filter"))
|
viper.BindPFlag("name_filter", pflag.Lookup("name-filter"))
|
||||||
|
|
||||||
pflag.Int("time-out", 120, "time before request timed out, in seconds; defaults to 120 if not set")
|
pflag.Int("time-out", 120, "time before backend HTTP request times out, in seconds; defaults to 120 if not set")
|
||||||
viper.BindPFlag("timeout", pflag.Lookup("time-out"))
|
viper.BindPFlag("timeout", pflag.Lookup("time-out"))
|
||||||
|
|
||||||
|
pflag.Int("connection-time-out", 5, "time before backend TCP connection times out, in seconds; defaults to 5 if not set")
|
||||||
|
viper.BindPFlag("connection_timeout", pflag.Lookup("connection-time-out"))
|
||||||
|
|
||||||
pflag.Parse()
|
pflag.Parse()
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
@@ -139,6 +143,7 @@ func parseSettings() {
|
|||||||
|
|
||||||
setting.nameFilter = viperSettings.NameFilter
|
setting.nameFilter = viperSettings.NameFilter
|
||||||
setting.timeOut = viperSettings.TimeOut
|
setting.timeOut = viperSettings.TimeOut
|
||||||
|
setting.connectionTimeOut = viperSettings.ConnectionTimeOut
|
||||||
|
|
||||||
fmt.Printf("%#v\n", setting)
|
fmt.Printf("%#v\n", setting)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user