You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

61 lines
1.4 KiB

  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. // This file implements an http.Client with request timeouts set by command
  7. // line flags.
  8. package main
  9. import (
  10. "fmt"
  11. "net"
  12. "net/http"
  13. "os"
  14. "github.com/gregjones/httpcache"
  15. "github.com/gregjones/httpcache/memcache"
  16. "github.com/spf13/viper"
  17. "github.com/golang/gddo/httputil"
  18. )
  19. func newHTTPClient() *http.Client {
  20. t := newCacheTransport()
  21. requestTimeout := viper.GetDuration(ConfigRequestTimeout)
  22. t.Transport = &http.Transport{
  23. Proxy: http.ProxyFromEnvironment,
  24. Dial: (&net.Dialer{
  25. Timeout: viper.GetDuration(ConfigDialTimeout),
  26. KeepAlive: requestTimeout / 2,
  27. }).Dial,
  28. ResponseHeaderTimeout: requestTimeout / 2,
  29. TLSHandshakeTimeout: requestTimeout / 2,
  30. }
  31. return &http.Client{
  32. // Wrap the cached transport with GitHub authentication.
  33. Transport: httputil.NewAuthTransport(t),
  34. Timeout: requestTimeout,
  35. }
  36. }
  37. func newCacheTransport() *httpcache.Transport {
  38. // host and port are set by GAE Flex runtime, can be left blank locally.
  39. host := os.Getenv("MEMCACHE_PORT_11211_TCP_ADDR")
  40. if host == "" {
  41. host = "localhost"
  42. }
  43. port := os.Getenv("MEMCACHE_PORT_11211_TCP_PORT")
  44. if port == "" {
  45. port = "11211"
  46. }
  47. addr := fmt.Sprintf("%s:%s", host, port)
  48. return httpcache.NewTransport(
  49. memcache.New(addr),
  50. )
  51. }