util.go 5.2 KB

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