util.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "os/signal"
  12. "runtime/pprof"
  13. "strconv"
  14. "time"
  15. "github.com/coreos/etcd/web"
  16. )
  17. //--------------------------------------
  18. // etcd http Helper
  19. //--------------------------------------
  20. // Convert string duration to time format
  21. func durationToExpireTime(strDuration string) (time.Time, error) {
  22. if strDuration != "" {
  23. duration, err := strconv.Atoi(strDuration)
  24. if err != nil {
  25. return time.Unix(0, 0), err
  26. }
  27. return time.Now().Add(time.Second * (time.Duration)(duration)), nil
  28. } else {
  29. return time.Unix(0, 0), nil
  30. }
  31. }
  32. //--------------------------------------
  33. // Web Helper
  34. //--------------------------------------
  35. var storeMsg chan string
  36. // Help to send msg from store to webHub
  37. func webHelper() {
  38. storeMsg = make(chan string)
  39. etcdStore.SetMessager(storeMsg)
  40. for {
  41. // transfer the new msg to webHub
  42. web.Hub().Send(<-storeMsg)
  43. }
  44. }
  45. // startWebInterface starts web interface if webURL is not empty
  46. func startWebInterface() {
  47. if argInfo.WebURL != "" {
  48. // start web
  49. go webHelper()
  50. go web.Start(r.Server, argInfo.WebURL)
  51. }
  52. }
  53. //--------------------------------------
  54. // HTTP Utilities
  55. //--------------------------------------
  56. func decodeJsonRequest(req *http.Request, data interface{}) error {
  57. decoder := json.NewDecoder(req.Body)
  58. if err := decoder.Decode(&data); err != nil && err != io.EOF {
  59. warnf("Malformed json request: %v", err)
  60. return fmt.Errorf("Malformed json request: %v", err)
  61. }
  62. return nil
  63. }
  64. func encodeJsonResponse(w http.ResponseWriter, status int, data interface{}) {
  65. w.Header().Set("Content-Type", "application/json")
  66. w.WriteHeader(status)
  67. if data != nil {
  68. encoder := json.NewEncoder(w)
  69. encoder.Encode(data)
  70. }
  71. }
  72. // sanitizeURL will cleanup a host string in the format hostname:port and
  73. // attach a schema.
  74. func sanitizeURL(host string, defaultScheme string) string {
  75. // Blank URLs are fine input, just return it
  76. if len(host) == 0 {
  77. return host
  78. }
  79. p, err := url.Parse(host)
  80. if err != nil {
  81. fatal(err)
  82. }
  83. // Make sure the host is in Host:Port format
  84. _, _, err = net.SplitHostPort(host)
  85. if err != nil {
  86. fatal(err)
  87. }
  88. p = &url.URL{Host: host, Scheme: defaultScheme}
  89. return p.String()
  90. }
  91. // sanitizeListenHost cleans up the ListenHost parameter and appends a port
  92. // if necessary based on the advertised port.
  93. func sanitizeListenHost(listen string, advertised string) string {
  94. aurl, err := url.Parse(advertised)
  95. if err != nil {
  96. fatal(err)
  97. }
  98. ahost, aport, err := net.SplitHostPort(aurl.Host)
  99. if err != nil {
  100. fatal(err)
  101. }
  102. // If the listen host isn't set use the advertised host
  103. if listen == "" {
  104. listen = ahost
  105. }
  106. return net.JoinHostPort(listen, aport)
  107. }
  108. func redirect(node string, etcd bool, w http.ResponseWriter, req *http.Request) {
  109. var url string
  110. path := req.URL.Path
  111. if etcd {
  112. etcdAddr, _ := nameToEtcdURL(node)
  113. url = etcdAddr + path
  114. } else {
  115. raftAddr, _ := nameToRaftURL(node)
  116. url = raftAddr + path
  117. }
  118. debugf("Redirect to %s", url)
  119. http.Redirect(w, req, url, http.StatusTemporaryRedirect)
  120. }
  121. func check(err error) {
  122. if err != nil {
  123. fatal(err)
  124. }
  125. }
  126. //--------------------------------------
  127. // Log
  128. //--------------------------------------
  129. var logger *log.Logger
  130. func init() {
  131. logger = log.New(os.Stdout, "[etcd] ", log.Lmicroseconds)
  132. }
  133. func infof(msg string, v ...interface{}) {
  134. logger.Printf("INFO "+msg+"\n", v...)
  135. }
  136. func debugf(msg string, v ...interface{}) {
  137. if verbose {
  138. logger.Printf("DEBUG "+msg+"\n", v...)
  139. }
  140. }
  141. func debug(v ...interface{}) {
  142. if verbose {
  143. logger.Println("DEBUG " + fmt.Sprint(v...))
  144. }
  145. }
  146. func warnf(msg string, v ...interface{}) {
  147. logger.Printf("WARN "+msg+"\n", v...)
  148. }
  149. func warn(v ...interface{}) {
  150. logger.Println("WARN " + fmt.Sprint(v...))
  151. }
  152. func fatalf(msg string, v ...interface{}) {
  153. logger.Printf("FATAL "+msg+"\n", v...)
  154. os.Exit(1)
  155. }
  156. func fatal(v ...interface{}) {
  157. logger.Println("FATAL " + fmt.Sprint(v...))
  158. os.Exit(1)
  159. }
  160. //--------------------------------------
  161. // CPU profile
  162. //--------------------------------------
  163. func runCPUProfile() {
  164. f, err := os.Create(cpuprofile)
  165. if err != nil {
  166. fatal(err)
  167. }
  168. pprof.StartCPUProfile(f)
  169. c := make(chan os.Signal, 1)
  170. signal.Notify(c, os.Interrupt)
  171. go func() {
  172. for sig := range c {
  173. infof("captured %v, stopping profiler and exiting..", sig)
  174. pprof.StopCPUProfile()
  175. os.Exit(1)
  176. }
  177. }()
  178. }
  179. //--------------------------------------
  180. // Testing
  181. //--------------------------------------
  182. func directSet() {
  183. c := make(chan bool, 1000)
  184. for i := 0; i < 1000; i++ {
  185. go send(c)
  186. }
  187. for i := 0; i < 1000; i++ {
  188. <-c
  189. }
  190. }
  191. func send(c chan bool) {
  192. for i := 0; i < 10; i++ {
  193. command := &SetCommand{}
  194. command.Key = "foo"
  195. command.Value = "bar"
  196. command.ExpireTime = time.Unix(0, 0)
  197. r.Do(command)
  198. }
  199. c <- true
  200. }