util.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. func (r *raftServer) dispatch(c Command, w http.ResponseWriter, req *http.Request, toURL func(name string) (string, bool)) error {
  37. if r.State() == raft.Leader {
  38. if response, err := r.Do(c); err != nil {
  39. return err
  40. } else {
  41. if response == nil {
  42. return etcdErr.NewError(300, "Empty response from raft", store.UndefIndex, store.UndefTerm)
  43. }
  44. event, ok := response.(*store.Event)
  45. if ok {
  46. bytes, err := json.Marshal(event)
  47. if err != nil {
  48. fmt.Println(err)
  49. }
  50. w.Header().Add("X-Etcd-Index", fmt.Sprint(event.Index))
  51. w.Header().Add("X-Etcd-Term", fmt.Sprint(event.Term))
  52. w.WriteHeader(http.StatusOK)
  53. w.Write(bytes)
  54. return nil
  55. }
  56. bytes, _ := response.([]byte)
  57. w.WriteHeader(http.StatusOK)
  58. w.Write(bytes)
  59. return nil
  60. }
  61. } else {
  62. leader := r.Leader()
  63. // current no leader
  64. if leader == "" {
  65. return etcdErr.NewError(300, "", store.UndefIndex, store.UndefTerm)
  66. }
  67. url, _ := toURL(leader)
  68. redirect(url, w, req)
  69. return nil
  70. }
  71. }
  72. func redirect(hostname string, w http.ResponseWriter, req *http.Request) {
  73. path := req.URL.Path
  74. url := hostname + path
  75. debugf("Redirect to %s", url)
  76. http.Redirect(w, req, url, http.StatusTemporaryRedirect)
  77. }
  78. // sanitizeURL will cleanup a host string in the format hostname:port and
  79. // attach a schema.
  80. func sanitizeURL(host string, defaultScheme string) string {
  81. // Blank URLs are fine input, just return it
  82. if len(host) == 0 {
  83. return host
  84. }
  85. p, err := url.Parse(host)
  86. if err != nil {
  87. fatal(err)
  88. }
  89. // Make sure the host is in Host:Port format
  90. _, _, err = net.SplitHostPort(host)
  91. if err != nil {
  92. fatal(err)
  93. }
  94. p = &url.URL{Host: host, Scheme: defaultScheme}
  95. return p.String()
  96. }
  97. // sanitizeListenHost cleans up the ListenHost parameter and appends a port
  98. // if necessary based on the advertised port.
  99. func sanitizeListenHost(listen string, advertised string) string {
  100. aurl, err := url.Parse(advertised)
  101. if err != nil {
  102. fatal(err)
  103. }
  104. ahost, aport, err := net.SplitHostPort(aurl.Host)
  105. if err != nil {
  106. fatal(err)
  107. }
  108. // If the listen host isn't set use the advertised host
  109. if listen == "" {
  110. listen = ahost
  111. }
  112. return net.JoinHostPort(listen, aport)
  113. }
  114. func check(err error) {
  115. if err != nil {
  116. fatal(err)
  117. }
  118. }
  119. func getNodePath(urlPath string) string {
  120. pathPrefixLen := len("/" + version + "/keys")
  121. return urlPath[pathPrefixLen:]
  122. }
  123. //--------------------------------------
  124. // CPU profile
  125. //--------------------------------------
  126. func runCPUProfile() {
  127. f, err := os.Create(cpuprofile)
  128. if err != nil {
  129. fatal(err)
  130. }
  131. pprof.StartCPUProfile(f)
  132. c := make(chan os.Signal, 1)
  133. signal.Notify(c, os.Interrupt)
  134. go func() {
  135. for sig := range c {
  136. infof("captured %v, stopping profiler and exiting..", sig)
  137. pprof.StopCPUProfile()
  138. os.Exit(1)
  139. }
  140. }()
  141. }
  142. //--------------------------------------
  143. // Testing
  144. //--------------------------------------
  145. func directSet() {
  146. c := make(chan bool, 1000)
  147. for i := 0; i < 1000; i++ {
  148. go send(c)
  149. }
  150. for i := 0; i < 1000; i++ {
  151. <-c
  152. }
  153. }
  154. func send(c chan bool) {
  155. for i := 0; i < 10; i++ {
  156. command := &UpdateCommand{}
  157. command.Key = "foo"
  158. command.Value = "bar"
  159. command.ExpireTime = time.Unix(0, 0)
  160. //r.Do(command)
  161. }
  162. c <- true
  163. }