This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
sender.go
54 lines (45 loc) · 1.44 KB
/
sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package implementations
import (
"context"
"fmt"
"github.com/NYTimes/gizmo/pubsub"
"github.com/Shopify/sarama"
pbcloudevents "github.com/cloudevents/sdk-go/binding/format/protobuf/v2"
"github.com/cloudevents/sdk-go/protocol/kafka_sarama/v2"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/flyteorg/flytestdlib/logger"
)
type Receiver = string
const (
Kafka Receiver = "Kafka"
)
// PubSubSender Implementation of Sender
type PubSubSender struct {
Pub pubsub.Publisher
}
func (s *PubSubSender) Send(ctx context.Context, notificationType string, event cloudevents.Event) error {
eventByte, err := pbcloudevents.Protobuf.Marshal(&event)
if err != nil {
logger.Errorf(ctx, "Failed to marshal cloudevent with error: %v", err)
return err
}
if err := s.Pub.PublishRaw(ctx, notificationType, eventByte); err != nil {
logger.Errorf(ctx, "Failed to publish a message with key [%s] and message [%s] and error: %v", notificationType, event.String(), err)
return err
}
return nil
}
// KafkaSender Implementation of Sender
type KafkaSender struct {
Client cloudevents.Client
}
func (s *KafkaSender) Send(ctx context.Context, notificationType string, event cloudevents.Event) error {
if result := s.Client.Send(
// Set the producer message key
kafka_sarama.WithMessageKey(ctx, sarama.StringEncoder(event.ID())),
event,
); cloudevents.IsUndelivered(result) {
return fmt.Errorf("failed to send cloud event: %v", result)
}
return nil
}