github.com/network-quality/goresponsiveness@v0.0.0-20240129151524-343954285090/utilities/transport.go (about)

     1  /*
     2   * This file is part of Go Responsiveness.
     3   *
     4   * Go Responsiveness is free software: you can redistribute it and/or modify it under
     5   * the terms of the GNU General Public License as published by the Free Software Foundation,
     6   * either version 2 of the License, or (at your option) any later version.
     7   * Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY
     8   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
     9   * PARTICULAR PURPOSE. See the GNU General Public License for more details.
    10   *
    11   * You should have received a copy of the GNU General Public License along
    12   * with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>.
    13   */
    14  
    15  package utilities
    16  
    17  import (
    18  	"context"
    19  	"net"
    20  	"net/http"
    21  	"time"
    22  
    23  	"golang.org/x/net/http2"
    24  )
    25  
    26  func OverrideHostTransport(transport *http.Transport, connectToAddr string) {
    27  	dialer := &net.Dialer{
    28  		Timeout: 10 * time.Second,
    29  	}
    30  
    31  	transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
    32  		_, port, err := net.SplitHostPort(addr)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  
    37  		if len(connectToAddr) > 0 {
    38  			addr = net.JoinHostPort(connectToAddr, port)
    39  		}
    40  
    41  		return dialer.DialContext(ctx, network, addr)
    42  	}
    43  
    44  	if t2, err := http2.ConfigureTransports(transport); err != nil {
    45  		panic("Panic: Could not properly upgrade an http/1.1 transport for http/2.")
    46  	} else {
    47  		t2.StrictMaxConcurrentStreams = true
    48  	}
    49  }