example_test.go 958 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. package netns_test
  5. import (
  6. "net"
  7. "net/http"
  8. "time"
  9. "notabug.org/themusicgod1/goarista/netns"
  10. )
  11. func ExampleDo_httpClient() {
  12. vrf := "management"
  13. vrf = netns.VRFToNetNS(vrf) // vrf is now "ns-management"
  14. dial := func(network, address string) (net.Conn, error) {
  15. var conn net.Conn
  16. err := netns.Do(vrf, func() error {
  17. var err error
  18. conn, err = (&net.Dialer{
  19. Timeout: 30 * time.Second, // This is the connection timeout
  20. KeepAlive: 30 * time.Second,
  21. }).Dial(network, address)
  22. return err
  23. })
  24. return conn, err
  25. }
  26. client := &http.Client{
  27. Transport: &http.Transport{
  28. //TLSClientConfig: ..., <- if you need SSL/TLS.
  29. Dial: dial,
  30. },
  31. Timeout: 30 * time.Second, // This is the request timeout
  32. }
  33. resp, err := client.Get("http://example.com")
  34. _ = resp
  35. _ = err
  36. }