add retry logical

master
dingjun 4 years ago
parent cb6a4838cf
commit b46d3b802f

@ -49,6 +49,9 @@ func handleHTTP(w http.ResponseWriter, r *http.Request) {
tr := &http.Transport{
DialContext: localDialer,
}
log.Debugf("%s %s", r.Method, r.RequestURI)
resp, err := tr.RoundTrip(r)
if err != nil {
log.Errorln(err)
@ -70,22 +73,30 @@ func handleHTTP(w http.ResponseWriter, r *http.Request) {
io.Copy(w, resp.Body)
}
func handleConnect(w http.ResponseWriter, r *http.Request) {
func mydail(uri string) (net.Conn, error) {
addr := &net.TCPAddr{}
dialer := &net.Dialer{
LocalAddr: addr,
Timeout: 3 * time.Second,
}
for i := 0; i < 3; i++ {
ip := getLocalIP()
addr.IP = net.ParseIP(ip)
log.Debugf("connect to %s, use local ip %s", r.RequestURI, ip)
log.Debugf("connect to %s, use local ip %s", uri, ip)
addr := &net.TCPAddr{
IP: net.ParseIP(ip),
Port: 0,
conn, err := dialer.Dial("tcp", uri)
if err == nil {
return conn, err
}
dialer := &net.Dialer{
LocalAddr: addr,
Timeout: 10 * time.Second,
log.Errorf("connect to %s, %s, retrying", uri, err)
}
return nil, fmt.Errorf("connect to %s, max retries reached, give up", uri)
}
conn, err := dialer.Dial("tcp", r.RequestURI)
func handleConnect(w http.ResponseWriter, r *http.Request) {
conn, err := mydail(r.RequestURI)
if err != nil {
log.Errorln(err)
http.Error(w, "connection failed", http.StatusBadGateway)

Loading…
Cancel
Save