You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.4 KiB

  1. package msgclient
  2. import (
  3. "io"
  4. "log"
  5. "time"
  6. "google.golang.org/grpc"
  7. "github.com/chrislusf/seaweedfs/weed/messaging/broker"
  8. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  9. )
  10. type PubChannel struct {
  11. client messaging_pb.SeaweedMessaging_PublishClient
  12. grpcConnection *grpc.ClientConn
  13. }
  14. func (mc *MessagingClient) NewPubChannel(chanName string) (*PubChannel, error) {
  15. tp := broker.TopicPartition{
  16. Namespace: "chan",
  17. Topic: chanName,
  18. Partition: 0,
  19. }
  20. grpcConnection, err := mc.findBroker(tp)
  21. if err != nil {
  22. return nil, err
  23. }
  24. pc, err := setupPublisherClient(grpcConnection, tp)
  25. if err != nil {
  26. return nil, err
  27. }
  28. return &PubChannel{
  29. client: pc,
  30. grpcConnection: grpcConnection,
  31. }, nil
  32. }
  33. func (pc *PubChannel) Publish(m []byte) error {
  34. return pc.client.Send(&messaging_pb.PublishRequest{
  35. Data: &messaging_pb.Message{
  36. Value: m,
  37. },
  38. })
  39. }
  40. func (pc *PubChannel) Close() error {
  41. // println("send closing")
  42. if err := pc.client.Send(&messaging_pb.PublishRequest{
  43. Data: &messaging_pb.Message{
  44. IsClose: true,
  45. },
  46. }); err != nil {
  47. log.Printf("err send close: %v", err)
  48. }
  49. // println("receive closing")
  50. if _, err := pc.client.Recv(); err != nil && err != io.EOF {
  51. log.Printf("err receive close: %v", err)
  52. }
  53. // println("close connection")
  54. if err := pc.grpcConnection.Close(); err != nil {
  55. log.Printf("err connection close: %v", err)
  56. }
  57. return nil
  58. }
  59. type SubChannel struct {
  60. ch chan []byte
  61. stream messaging_pb.SeaweedMessaging_SubscribeClient
  62. }
  63. func (mc *MessagingClient) NewSubChannel(chanName string) (*SubChannel, error) {
  64. tp := broker.TopicPartition{
  65. Namespace: "chan",
  66. Topic: chanName,
  67. Partition: 0,
  68. }
  69. grpcConnection, err := mc.findBroker(tp)
  70. if err != nil {
  71. return nil, err
  72. }
  73. sc, err := setupSubscriberClient(grpcConnection, "", "chan", chanName, 0, time.Unix(0, 0))
  74. if err != nil {
  75. return nil, err
  76. }
  77. t := &SubChannel{
  78. ch: make(chan []byte),
  79. stream: sc,
  80. }
  81. go func() {
  82. for {
  83. resp, subErr := t.stream.Recv()
  84. if subErr == io.EOF {
  85. return
  86. }
  87. if subErr != nil {
  88. log.Printf("fail to receive from netchan %s: %v", chanName, subErr)
  89. return
  90. }
  91. if resp.Data.IsClose {
  92. t.stream.Send(&messaging_pb.SubscriberMessage{
  93. IsClose: true,
  94. })
  95. close(t.ch)
  96. return
  97. }
  98. t.ch <- resp.Data.Value
  99. }
  100. }()
  101. return t, nil
  102. }
  103. func (sc *SubChannel) Channel() chan []byte {
  104. return sc.ch
  105. }