另客网go项目公用的代码库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package hooks
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "sync"
  7. "time"
  8. "github.com/sirupsen/logrus"
  9. "github.com/wpajqz/go-sdk/export"
  10. )
  11. const (
  12. timeout = 5 * time.Second
  13. retryInterval = 100 * time.Microsecond
  14. )
  15. const (
  16. notifyPath = "/v1/notify"
  17. )
  18. var (
  19. defaultClient *Client
  20. once = &sync.Once{}
  21. )
  22. type Client struct{ tcpClient *export.Client }
  23. func (c *Client) Notify(req *NotifyRequest) (*NotifyResponse, error) {
  24. if err := req.Check(); err != nil {
  25. return nil, err
  26. }
  27. param, err := json.Marshal(req)
  28. if err != nil {
  29. return nil, err
  30. }
  31. var resp NotifyResponse
  32. err = c.tcpClient.SyncSend(notifyPath, param, &RequestStatusCallback{
  33. Success: func(header, body []byte) {
  34. err = json.Unmarshal(body, &resp)
  35. },
  36. Error: func(code int, message string) {
  37. err = errors.New(message)
  38. },
  39. })
  40. if err != nil {
  41. return nil, err
  42. }
  43. return &resp, err
  44. }
  45. func NewClient(server string, port int) *Client {
  46. once.Do(func() {
  47. defaultClient = &Client{tcpClient: export.NewClient(server, port, &ReadyStateCallback{})}
  48. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  49. defer cancel()
  50. for {
  51. select {
  52. case <-time.After(retryInterval):
  53. if defaultClient.tcpClient.GetReadyState() == export.OPEN {
  54. return
  55. }
  56. case <-ctx.Done():
  57. panic("connect link timeout ...")
  58. }
  59. }
  60. })
  61. return defaultClient
  62. }
  63. func GetClient() *Client {
  64. return defaultClient
  65. }
  66. type (
  67. NotifyRequest struct {
  68. Project string `json:"project"`
  69. Maintainers []string `json:"maintainers"`
  70. Message string `json:"message"`
  71. Time string `json:"time"`
  72. }
  73. NotifyResponse struct {
  74. }
  75. )
  76. func (ar *NotifyRequest) Check() error {
  77. return nil
  78. }
  79. type (
  80. ReadyStateCallback struct{}
  81. RequestStatusCallback struct {
  82. Start func()
  83. End func()
  84. Success func(header, body []byte)
  85. Error func(code int, message string)
  86. }
  87. )
  88. func (readyStateCallback *ReadyStateCallback) OnOpen() {
  89. logrus.Info("open link socket connection")
  90. }
  91. func (readyStateCallback *ReadyStateCallback) OnClose() {
  92. logrus.Info("close link socket connection")
  93. }
  94. func (readyStateCallback *ReadyStateCallback) OnError(err string) {
  95. logrus.Infof("error:%s", err)
  96. }
  97. func (r RequestStatusCallback) OnStart() {
  98. if r.Start != nil {
  99. r.Start()
  100. }
  101. }
  102. func (r RequestStatusCallback) OnSuccess(header, body []byte) {
  103. if r.Success != nil {
  104. r.Success(header, body)
  105. }
  106. }
  107. func (r RequestStatusCallback) OnError(code int, message string) {
  108. if r.Error != nil {
  109. r.Error(code, message)
  110. }
  111. }
  112. func (r RequestStatusCallback) OnEnd() {
  113. if r.End != nil {
  114. r.End()
  115. }
  116. }