collection.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package mongo
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/globalsign/mgo"
  6. "github.com/tal-tech/go-zero/core/breaker"
  7. "github.com/tal-tech/go-zero/core/logx"
  8. "github.com/tal-tech/go-zero/core/stores/mongo/internal"
  9. "github.com/tal-tech/go-zero/core/timex"
  10. )
  11. const slowThreshold = time.Millisecond * 500
  12. var ErrNotFound = mgo.ErrNotFound
  13. type (
  14. Collection interface {
  15. Find(query interface{}) Query
  16. FindId(id interface{}) Query
  17. Insert(docs ...interface{}) error
  18. Pipe(pipeline interface{}) Pipe
  19. Remove(selector interface{}) error
  20. RemoveAll(selector interface{}) (*mgo.ChangeInfo, error)
  21. RemoveId(id interface{}) error
  22. Update(selector, update interface{}) error
  23. UpdateId(id, update interface{}) error
  24. Upsert(selector, update interface{}) (*mgo.ChangeInfo, error)
  25. }
  26. decoratedCollection struct {
  27. name string
  28. collection internal.MgoCollection
  29. brk breaker.Breaker
  30. }
  31. keepablePromise struct {
  32. promise breaker.Promise
  33. log func(error)
  34. }
  35. )
  36. func newCollection(collection *mgo.Collection) Collection {
  37. return &decoratedCollection{
  38. name: collection.FullName,
  39. collection: collection,
  40. brk: breaker.NewBreaker(),
  41. }
  42. }
  43. func (c *decoratedCollection) Find(query interface{}) Query {
  44. promise, err := c.brk.Allow()
  45. if err != nil {
  46. return rejectedQuery{}
  47. }
  48. startTime := timex.Now()
  49. return promisedQuery{
  50. Query: c.collection.Find(query),
  51. promise: keepablePromise{
  52. promise: promise,
  53. log: func(err error) {
  54. duration := timex.Since(startTime)
  55. c.logDuration("find", duration, err, query)
  56. },
  57. },
  58. }
  59. }
  60. func (c *decoratedCollection) FindId(id interface{}) Query {
  61. promise, err := c.brk.Allow()
  62. if err != nil {
  63. return rejectedQuery{}
  64. }
  65. startTime := timex.Now()
  66. return promisedQuery{
  67. Query: c.collection.FindId(id),
  68. promise: keepablePromise{
  69. promise: promise,
  70. log: func(err error) {
  71. duration := timex.Since(startTime)
  72. c.logDuration("findId", duration, err, id)
  73. },
  74. },
  75. }
  76. }
  77. func (c *decoratedCollection) Insert(docs ...interface{}) (err error) {
  78. return c.brk.DoWithAcceptable(func() error {
  79. startTime := timex.Now()
  80. defer func() {
  81. duration := timex.Since(startTime)
  82. c.logDuration("insert", duration, err, docs...)
  83. }()
  84. return c.collection.Insert(docs...)
  85. }, acceptable)
  86. }
  87. func (c *decoratedCollection) Pipe(pipeline interface{}) Pipe {
  88. promise, err := c.brk.Allow()
  89. if err != nil {
  90. return rejectedPipe{}
  91. }
  92. startTime := timex.Now()
  93. return promisedPipe{
  94. Pipe: c.collection.Pipe(pipeline),
  95. promise: keepablePromise{
  96. promise: promise,
  97. log: func(err error) {
  98. duration := timex.Since(startTime)
  99. c.logDuration("pipe", duration, err, pipeline)
  100. },
  101. },
  102. }
  103. }
  104. func (c *decoratedCollection) Remove(selector interface{}) (err error) {
  105. return c.brk.DoWithAcceptable(func() error {
  106. startTime := timex.Now()
  107. defer func() {
  108. duration := timex.Since(startTime)
  109. c.logDuration("remove", duration, err, selector)
  110. }()
  111. return c.collection.Remove(selector)
  112. }, acceptable)
  113. }
  114. func (c *decoratedCollection) RemoveAll(selector interface{}) (info *mgo.ChangeInfo, err error) {
  115. err = c.brk.DoWithAcceptable(func() error {
  116. startTime := timex.Now()
  117. defer func() {
  118. duration := timex.Since(startTime)
  119. c.logDuration("removeAll", duration, err, selector)
  120. }()
  121. info, err = c.collection.RemoveAll(selector)
  122. return err
  123. }, acceptable)
  124. return
  125. }
  126. func (c *decoratedCollection) RemoveId(id interface{}) (err error) {
  127. return c.brk.DoWithAcceptable(func() error {
  128. startTime := timex.Now()
  129. defer func() {
  130. duration := timex.Since(startTime)
  131. c.logDuration("removeId", duration, err, id)
  132. }()
  133. return c.collection.RemoveId(id)
  134. }, acceptable)
  135. }
  136. func (c *decoratedCollection) Update(selector, update interface{}) (err error) {
  137. return c.brk.DoWithAcceptable(func() error {
  138. startTime := timex.Now()
  139. defer func() {
  140. duration := timex.Since(startTime)
  141. c.logDuration("update", duration, err, selector, update)
  142. }()
  143. return c.collection.Update(selector, update)
  144. }, acceptable)
  145. }
  146. func (c *decoratedCollection) UpdateId(id, update interface{}) (err error) {
  147. return c.brk.DoWithAcceptable(func() error {
  148. startTime := timex.Now()
  149. defer func() {
  150. duration := timex.Since(startTime)
  151. c.logDuration("updateId", duration, err, id, update)
  152. }()
  153. return c.collection.UpdateId(id, update)
  154. }, acceptable)
  155. }
  156. func (c *decoratedCollection) Upsert(selector, update interface{}) (info *mgo.ChangeInfo, err error) {
  157. err = c.brk.DoWithAcceptable(func() error {
  158. startTime := timex.Now()
  159. defer func() {
  160. duration := timex.Since(startTime)
  161. c.logDuration("upsert", duration, err, selector, update)
  162. }()
  163. info, err = c.collection.Upsert(selector, update)
  164. return err
  165. }, acceptable)
  166. return
  167. }
  168. func (c *decoratedCollection) logDuration(method string, duration time.Duration, err error, docs ...interface{}) {
  169. content, e := json.Marshal(docs)
  170. if e != nil {
  171. logx.Error(err)
  172. } else if err != nil {
  173. if duration > slowThreshold {
  174. logx.WithDuration(duration).Slowf("[MONGO] mongo(%s) - slowcall - %s - fail(%s) - %s",
  175. c.name, method, err.Error(), string(content))
  176. } else {
  177. logx.WithDuration(duration).Infof("mongo(%s) - %s - fail(%s) - %s",
  178. c.name, method, err.Error(), string(content))
  179. }
  180. } else {
  181. if duration > slowThreshold {
  182. logx.WithDuration(duration).Slowf("[MONGO] mongo(%s) - slowcall - %s - ok - %s",
  183. c.name, method, string(content))
  184. } else {
  185. logx.WithDuration(duration).Infof("mongo(%s) - %s - ok - %s", c.name, method, string(content))
  186. }
  187. }
  188. }
  189. func (p keepablePromise) accept(err error) error {
  190. p.promise.Accept()
  191. p.log(err)
  192. return err
  193. }
  194. func (p keepablePromise) keep(err error) error {
  195. if acceptable(err) {
  196. p.promise.Accept()
  197. } else {
  198. p.promise.Reject(err.Error())
  199. }
  200. p.log(err)
  201. return err
  202. }
  203. func acceptable(err error) bool {
  204. return err == nil || err == mgo.ErrNotFound
  205. }