github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/netns/example_test.go (about)

     1  // Copyright (c) 2016 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package netns_test
     6  
     7  import (
     8  	"net"
     9  	"net/http"
    10  	"time"
    11  
    12  	"github.com/aristanetworks/goarista/netns"
    13  )
    14  
    15  func ExampleDo_httpClient() {
    16  	vrf := "management"
    17  	vrf = netns.VRFToNetNS(vrf) // vrf is now "ns-management"
    18  
    19  	dial := func(network, address string) (net.Conn, error) {
    20  		var conn net.Conn
    21  		err := netns.Do(vrf, func() error {
    22  			var err error
    23  			conn, err = (&net.Dialer{
    24  				Timeout:   30 * time.Second, // This is the connection timeout
    25  				KeepAlive: 30 * time.Second,
    26  			}).Dial(network, address)
    27  			return err
    28  		})
    29  		return conn, err
    30  	}
    31  
    32  	client := &http.Client{
    33  		Transport: &http.Transport{
    34  			//TLSClientConfig: ..., <- if you need SSL/TLS.
    35  			Dial: dial,
    36  		},
    37  		Timeout: 30 * time.Second, // This is the request timeout
    38  	}
    39  
    40  	resp, err := client.Get("http://example.com")
    41  	_ = resp
    42  	_ = err
    43  }