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.

64 lines
1.4 KiB

  1. package msgclient
  2. import (
  3. "io"
  4. "log"
  5. "google.golang.org/grpc"
  6. "github.com/chrislusf/seaweedfs/weed/messaging/broker"
  7. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  8. )
  9. type PubChannel struct {
  10. client messaging_pb.SeaweedMessaging_PublishClient
  11. grpcConnection *grpc.ClientConn
  12. }
  13. func (mc *MessagingClient) NewPubChannel(chanName string) (*PubChannel, 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. pc, err := setupPublisherClient(grpcConnection, tp)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return &PubChannel{
  28. client: pc,
  29. grpcConnection: grpcConnection,
  30. }, nil
  31. }
  32. func (pc *PubChannel) Publish(m []byte) error {
  33. return pc.client.Send(&messaging_pb.PublishRequest{
  34. Data: &messaging_pb.Message{
  35. Value: m,
  36. },
  37. })
  38. }
  39. func (pc *PubChannel) Close() error {
  40. // println("send closing")
  41. if err := pc.client.Send(&messaging_pb.PublishRequest{
  42. Data: &messaging_pb.Message{
  43. IsClose: true,
  44. },
  45. }); err != nil {
  46. log.Printf("err send close: %v", err)
  47. }
  48. // println("receive closing")
  49. if _, err := pc.client.Recv(); err != nil && err != io.EOF {
  50. log.Printf("err receive close: %v", err)
  51. }
  52. // println("close connection")
  53. if err := pc.grpcConnection.Close(); err != nil {
  54. log.Printf("err connection close: %v", err)
  55. }
  56. return nil
  57. }