discovery.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package http
  2. import (
  3. "net/http"
  4. "strconv"
  5. "time"
  6. "go-common/app/infra/discovery/model"
  7. "go-common/library/ecode"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. const (
  11. _pollWaitSecond = 30 * time.Second
  12. )
  13. func register(c *bm.Context) {
  14. arg := new(model.ArgRegister)
  15. if err := c.Bind(arg); err != nil {
  16. return
  17. }
  18. i := model.NewInstance(arg)
  19. if i.Status == 0 || i.Status > 2 {
  20. c.JSON(nil, ecode.RequestErr)
  21. return
  22. }
  23. // register replication
  24. if arg.DirtyTimestamp > 0 {
  25. i.DirtyTimestamp = arg.DirtyTimestamp
  26. }
  27. svr.Register(c, i, arg.LatestTimestamp, arg.Replication)
  28. c.JSON(nil, nil)
  29. }
  30. func renew(c *bm.Context) {
  31. arg := new(model.ArgRenew)
  32. if err := c.Bind(arg); err != nil {
  33. return
  34. }
  35. // renew
  36. c.JSON(svr.Renew(c, arg))
  37. }
  38. func cancel(c *bm.Context) {
  39. arg := new(model.ArgCancel)
  40. if err := c.Bind(arg); err != nil {
  41. c.JSON(nil, ecode.RequestErr)
  42. return
  43. }
  44. c.JSON(nil, svr.Cancel(c, arg))
  45. }
  46. func fetchAll(c *bm.Context) {
  47. c.JSON(svr.FetchAll(c), nil)
  48. }
  49. func fetch(c *bm.Context) {
  50. arg := new(model.ArgFetch)
  51. if err := c.Bind(arg); err != nil {
  52. return
  53. }
  54. c.JSON(svr.Fetch(c, arg))
  55. }
  56. func fetchs(c *bm.Context) {
  57. arg := new(model.ArgFetchs)
  58. if err := c.Bind(arg); err != nil {
  59. return
  60. }
  61. c.JSON(svr.Fetchs(c, arg))
  62. }
  63. func poll(c *bm.Context) {
  64. arg := new(model.ArgPolls)
  65. if err := c.Bind(arg); err != nil {
  66. return
  67. }
  68. ch, new, err := svr.Polls(c, arg)
  69. if err != nil && err != ecode.NotModified {
  70. c.JSON(nil, err)
  71. return
  72. }
  73. // wait for instance change
  74. select {
  75. case e := <-ch:
  76. if len(arg.Treeid) != 0 {
  77. c.JSON(e[strconv.FormatInt(arg.Treeid[0], 10)], nil)
  78. } else {
  79. c.JSON(e[arg.Appid[0]], nil)
  80. }
  81. if !new {
  82. svr.DelConns(arg) // broadcast will delete all connections of appid
  83. }
  84. case <-time.After(_pollWaitSecond):
  85. c.JSON(nil, ecode.NotModified)
  86. svr.DelConns(arg)
  87. case <-c.Writer.(http.CloseNotifier).CloseNotify():
  88. c.JSON(nil, ecode.NotModified)
  89. svr.DelConns(arg)
  90. }
  91. }
  92. func polls(c *bm.Context) {
  93. arg := new(model.ArgPolls)
  94. if err := c.Bind(arg); err != nil {
  95. return
  96. }
  97. if len(arg.Treeid) != len(arg.LatestTimestamp) && len(arg.Appid) != len(arg.LatestTimestamp) {
  98. c.JSON(nil, ecode.RequestErr)
  99. return
  100. }
  101. ch, new, err := svr.Polls(c, arg)
  102. if err != nil && err != ecode.NotModified {
  103. c.JSON(nil, err)
  104. return
  105. }
  106. // wait for instance change
  107. select {
  108. case e := <-ch:
  109. c.JSON(e, nil)
  110. if !new {
  111. svr.DelConns(arg) // broadcast will delete all connections of appid
  112. }
  113. case <-time.After(_pollWaitSecond):
  114. c.JSON(nil, ecode.NotModified)
  115. svr.DelConns(arg)
  116. case <-c.Writer.(http.CloseNotifier).CloseNotify():
  117. c.JSON(nil, ecode.NotModified)
  118. svr.DelConns(arg)
  119. }
  120. }
  121. func polling(c *bm.Context) {
  122. arg := new(model.ArgPolling)
  123. if err := c.Bind(arg); err != nil {
  124. return
  125. }
  126. c.JSON(svr.Polling(c, arg))
  127. }
  128. func set(c *bm.Context) {
  129. var (
  130. arg = new(model.ArgSet)
  131. )
  132. if err := c.Bind(arg); err != nil {
  133. return
  134. }
  135. // len of status,metadata must equal to len of hostname or be zero
  136. if (len(arg.Hostname) != len(arg.Status) && len(arg.Status) != 0) ||
  137. (len(arg.Hostname) != len(arg.Metadata) && len(arg.Metadata) != 0) {
  138. c.JSON(nil, ecode.RequestErr)
  139. return
  140. }
  141. c.JSON(nil, svr.Set(c, arg))
  142. }
  143. func nodes(c *bm.Context) {
  144. c.JSON(svr.Nodes(c), nil)
  145. }