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.

75 lines
1.3 KiB

  1. package client
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
  5. )
  6. type Publisher struct {
  7. publishClient messaging_pb.SeaweedMessaging_PublishClient
  8. }
  9. func (mc *MessagingClient) NewPublisher(namespace, topic string) (*Publisher, error) {
  10. stream, err := messaging_pb.NewSeaweedMessagingClient(mc.grpcConnection).Publish(context.Background())
  11. if err != nil {
  12. return nil, err
  13. }
  14. // send init message
  15. err = stream.Send(&messaging_pb.PublishRequest{
  16. Init: &messaging_pb.PublishRequest_InitMessage{
  17. Namespace: namespace,
  18. Topic: topic,
  19. Partition: 0,
  20. },
  21. })
  22. if err != nil {
  23. return nil, err
  24. }
  25. // process init response
  26. initResponse, err := stream.Recv()
  27. if err != nil {
  28. return nil, err
  29. }
  30. if initResponse.Redirect != nil {
  31. // TODO follow redirection
  32. }
  33. if initResponse.Config != nil {
  34. }
  35. // setup looks for control messages
  36. doneChan := make(chan error, 1)
  37. go func() {
  38. for {
  39. in, err := stream.Recv()
  40. if err != nil {
  41. doneChan <- err
  42. return
  43. }
  44. if in.Redirect != nil{
  45. }
  46. if in.Config != nil{
  47. }
  48. }
  49. }()
  50. return &Publisher{
  51. publishClient: stream,
  52. }, nil
  53. }
  54. func (p *Publisher) Publish(m *messaging_pb.Message) error {
  55. return p.publishClient.Send(&messaging_pb.PublishRequest{
  56. Data: m,
  57. })
  58. }
  59. func (p *Publisher) Close() error {
  60. return p.publishClient.CloseSend()
  61. }