util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "os/signal"
  11. "runtime/pprof"
  12. "strconv"
  13. "time"
  14. etcdErr "github.com/coreos/etcd/error"
  15. "github.com/coreos/etcd/store"
  16. "github.com/coreos/go-raft"
  17. )
  18. //--------------------------------------
  19. // etcd http Helper
  20. //--------------------------------------
  21. // Convert string duration to time format
  22. func durationToExpireTime(strDuration string) (time.Time, error) {
  23. if strDuration != "" {
  24. duration, err := strconv.Atoi(strDuration)
  25. if err != nil {
  26. return store.Permanent, err
  27. }
  28. return time.Now().Add(time.Second * (time.Duration)(duration)), nil
  29. } else {
  30. return store.Permanent, nil
  31. }
  32. }
  33. //--------------------------------------
  34. // HTTP Utilities
  35. //--------------------------------------
  36. // sanitizeURL will cleanup a host string in the format hostname:port and
  37. // attach a schema.
  38. func sanitizeURL(host string, defaultScheme string) string {
  39. // Blank URLs are fine input, just return it
  40. if len(host) == 0 {
  41. return host
  42. }
  43. p, err := url.Parse(host)
  44. if err != nil {
  45. fatal(err)
  46. }
  47. // Make sure the host is in Host:Port format
  48. _, _, err = net.SplitHostPort(host)
  49. if err != nil {
  50. fatal(err)
  51. }
  52. p = &url.URL{Host: host, Scheme: defaultScheme}
  53. return p.String()
  54. }
  55. // sanitizeListenHost cleans up the ListenHost parameter and appends a port
  56. // if necessary based on the advertised port.
  57. func sanitizeListenHost(listen string, advertised string) string {
  58. aurl, err := url.Parse(advertised)
  59. if err != nil {
  60. fatal(err)
  61. }
  62. ahost, aport, err := net.SplitHostPort(aurl.Host)
  63. if err != nil {
  64. fatal(err)
  65. }
  66. // If the listen host isn't set use the advertised host
  67. if listen == "" {
  68. listen = ahost
  69. }
  70. return net.JoinHostPort(listen, aport)
  71. }
  72. func check(err error) {
  73. if err != nil {
  74. fatal(err)
  75. }
  76. }
  77. func getNodePath(urlPath string) string {
  78. pathPrefixLen := len("/" + version + "/keys")
  79. return urlPath[pathPrefixLen:]
  80. }
  81. //--------------------------------------
  82. // CPU profile
  83. //--------------------------------------
  84. func runCPUProfile() {
  85. f, err := os.Create(cpuprofile)
  86. if err != nil {
  87. fatal(err)
  88. }
  89. pprof.StartCPUProfile(f)
  90. c := make(chan os.Signal, 1)
  91. signal.Notify(c, os.Interrupt)
  92. go func() {
  93. for sig := range c {
  94. infof("captured %v, stopping profiler and exiting..", sig)
  95. pprof.StopCPUProfile()
  96. os.Exit(1)
  97. }
  98. }()
  99. }
  100. //--------------------------------------
  101. // Testing
  102. //--------------------------------------
  103. func directSet() {
  104. c := make(chan bool, 1000)
  105. for i := 0; i < 1000; i++ {
  106. go send(c)
  107. }
  108. for i := 0; i < 1000; i++ {
  109. <-c
  110. }
  111. }
  112. func send(c chan bool) {
  113. for i := 0; i < 10; i++ {
  114. command := &UpdateCommand{}
  115. command.Key = "foo"
  116. command.Value = "bar"
  117. command.ExpireTime = time.Unix(0, 0)
  118. //r.Do(command)
  119. }
  120. c <- true
  121. }