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.

63 lines
1.1 KiB

  1. package msgclient
  2. import (
  3. "io"
  4. "log"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/messaging/broker"
  7. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  8. )
  9. type SubChannel struct {
  10. ch chan []byte
  11. stream messaging_pb.SeaweedMessaging_SubscribeClient
  12. }
  13. func (mc *MessagingClient) NewSubChannel(chanName string) (*SubChannel, error) {
  14. tp := broker.TopicPartition{
  15. Namespace: "chan",
  16. Topic: chanName,
  17. Partition: 0,
  18. }
  19. grpcConnection, err := mc.findBroker(tp)
  20. if err != nil {
  21. return nil, err
  22. }
  23. sc, err := setupSubscriberClient(grpcConnection, "", "chan", chanName, 0, time.Unix(0, 0))
  24. if err != nil {
  25. return nil, err
  26. }
  27. t := &SubChannel{
  28. ch: make(chan []byte),
  29. stream: sc,
  30. }
  31. go func() {
  32. for {
  33. resp, subErr := t.stream.Recv()
  34. if subErr == io.EOF {
  35. return
  36. }
  37. if subErr != nil {
  38. log.Printf("fail to receive from netchan %s: %v", chanName, subErr)
  39. return
  40. }
  41. if resp.Data.IsClose {
  42. t.stream.Send(&messaging_pb.SubscriberMessage{
  43. IsClose: true,
  44. })
  45. close(t.ch)
  46. return
  47. }
  48. t.ch <- resp.Data.Value
  49. }
  50. }()
  51. return t, nil
  52. }
  53. func (sc *SubChannel) Channel() chan []byte {
  54. return sc.ch
  55. }