util.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/coreos/etcd/web"
  6. "io"
  7. "log"
  8. "net"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "os/signal"
  13. "runtime/pprof"
  14. "strconv"
  15. "time"
  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. func check(err error) {
  92. if err != nil {
  93. fatal(err)
  94. }
  95. }
  96. //--------------------------------------
  97. // Log
  98. //--------------------------------------
  99. var logger *log.Logger
  100. func init() {
  101. logger = log.New(os.Stdout, "[etcd] ", log.Lmicroseconds)
  102. }
  103. func infof(msg string, v ...interface{}) {
  104. logger.Printf("INFO "+msg+"\n", v...)
  105. }
  106. func debugf(msg string, v ...interface{}) {
  107. if verbose {
  108. logger.Printf("DEBUG "+msg+"\n", v...)
  109. }
  110. }
  111. func debug(v ...interface{}) {
  112. if verbose {
  113. logger.Println("DEBUG " + fmt.Sprint(v...))
  114. }
  115. }
  116. func warnf(msg string, v ...interface{}) {
  117. logger.Printf("WARN "+msg+"\n", v...)
  118. }
  119. func warn(v ...interface{}) {
  120. logger.Println("WARN " + fmt.Sprint(v...))
  121. }
  122. func fatalf(msg string, v ...interface{}) {
  123. logger.Printf("FATAL "+msg+"\n", v...)
  124. os.Exit(1)
  125. }
  126. func fatal(v ...interface{}) {
  127. logger.Println("FATAL " + fmt.Sprint(v...))
  128. os.Exit(1)
  129. }
  130. //--------------------------------------
  131. // CPU profile
  132. //--------------------------------------
  133. func runCPUProfile() {
  134. f, err := os.Create(cpuprofile)
  135. if err != nil {
  136. fatal(err)
  137. }
  138. pprof.StartCPUProfile(f)
  139. c := make(chan os.Signal, 1)
  140. signal.Notify(c, os.Interrupt)
  141. go func() {
  142. for sig := range c {
  143. infof("captured %v, stopping profiler and exiting..", sig)
  144. pprof.StopCPUProfile()
  145. os.Exit(1)
  146. }
  147. }()
  148. }
  149. //--------------------------------------
  150. // Testing
  151. //--------------------------------------
  152. func directSet() {
  153. c := make(chan bool, 1000)
  154. for i := 0; i < 1000; i++ {
  155. go send(c)
  156. }
  157. for i := 0; i < 1000; i++ {
  158. <-c
  159. }
  160. }
  161. func send(c chan bool) {
  162. for i := 0; i < 10; i++ {
  163. command := &SetCommand{}
  164. command.Key = "foo"
  165. command.Value = "bar"
  166. command.ExpireTime = time.Unix(0, 0)
  167. r.Do(command)
  168. }
  169. c <- true
  170. }