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.

107 lines
2.8 KiB

  1. //go:build gocdk
  2. // +build gocdk
  3. // Package gocdk_pub_sub supports the Go CDK (Cloud Development Kit) PubSub API,
  4. // which in turn supports many providers, including Amazon SNS/SQS, Azure Service Bus,
  5. // Google Cloud PubSub, and RabbitMQ.
  6. //
  7. // In the config, select a provider and topic using a URL. See
  8. // https://godoc.org/gocloud.dev/pubsub and its sub-packages for details.
  9. //
  10. // The Go CDK PubSub API does not support administrative operations like topic
  11. // creation. Create the topic using a UI, CLI or provider-specific API before running
  12. // weed.
  13. //
  14. // The Go CDK obtains credentials via environment variables and other
  15. // provider-specific default mechanisms. See the provider's documentation for
  16. // details.
  17. package gocdk_pub_sub
  18. import (
  19. "context"
  20. "fmt"
  21. "github.com/streadway/amqp"
  22. "gocloud.dev/pubsub"
  23. _ "gocloud.dev/pubsub/awssnssqs"
  24. "gocloud.dev/pubsub/rabbitpubsub"
  25. "google.golang.org/protobuf/proto"
  26. "net/url"
  27. "path"
  28. "time"
  29. "github.com/seaweedfs/seaweedfs/weed/glog"
  30. "github.com/seaweedfs/seaweedfs/weed/notification"
  31. "github.com/seaweedfs/seaweedfs/weed/util"
  32. // _ "gocloud.dev/pubsub/azuresb"
  33. _ "gocloud.dev/pubsub/gcppubsub"
  34. _ "gocloud.dev/pubsub/natspubsub"
  35. _ "gocloud.dev/pubsub/rabbitpubsub"
  36. "os"
  37. )
  38. func init() {
  39. notification.MessageQueues = append(notification.MessageQueues, &GoCDKPubSub{})
  40. }
  41. func getPath(rawUrl string) string {
  42. parsedUrl, _ := url.Parse(rawUrl)
  43. return path.Join(parsedUrl.Host, parsedUrl.Path)
  44. }
  45. type GoCDKPubSub struct {
  46. topicURL string
  47. topic *pubsub.Topic
  48. }
  49. func (k *GoCDKPubSub) GetName() string {
  50. return "gocdk_pub_sub"
  51. }
  52. func (k *GoCDKPubSub) doReconnect() {
  53. var conn *amqp.Connection
  54. if k.topic.As(&conn) {
  55. go func() {
  56. <-conn.NotifyClose(make(chan *amqp.Error))
  57. conn.Close()
  58. k.topic.Shutdown(context.Background())
  59. for {
  60. glog.Info("Try reconnect")
  61. conn, err := amqp.Dial(os.Getenv("RABBIT_SERVER_URL"))
  62. if err == nil {
  63. k.topic = rabbitpubsub.OpenTopic(conn, getPath(k.topicURL), nil)
  64. k.doReconnect()
  65. break
  66. }
  67. glog.Error(err)
  68. time.Sleep(time.Second)
  69. }
  70. }()
  71. }
  72. }
  73. func (k *GoCDKPubSub) Initialize(configuration util.Configuration, prefix string) error {
  74. k.topicURL = configuration.GetString(prefix + "topic_url")
  75. glog.V(0).Infof("notification.gocdk_pub_sub.topic_url: %v", k.topicURL)
  76. topic, err := pubsub.OpenTopic(context.Background(), k.topicURL)
  77. if err != nil {
  78. glog.Fatalf("Failed to open topic: %v", err)
  79. }
  80. k.topic = topic
  81. k.doReconnect()
  82. return nil
  83. }
  84. func (k *GoCDKPubSub) SendMessage(key string, message proto.Message) error {
  85. bytes, err := proto.Marshal(message)
  86. if err != nil {
  87. return err
  88. }
  89. err = k.topic.Send(context.Background(), &pubsub.Message{
  90. Body: bytes,
  91. Metadata: map[string]string{"key": key},
  92. })
  93. if err != nil {
  94. return fmt.Errorf("send message via Go CDK pubsub %s: %v", k.topicURL, err)
  95. }
  96. return nil
  97. }