|
| 1 | +/* |
| 2 | +Copyright 2024 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package client |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "encoding/json" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "mime" |
| 22 | + "strings" |
| 23 | + "sync" |
| 24 | + "sync/atomic" |
| 25 | + |
| 26 | + pb "github.com/dapr/dapr/pkg/proto/runtime/v1" |
| 27 | + "github.com/dapr/go-sdk/service/common" |
| 28 | +) |
| 29 | + |
| 30 | +type SubscriptionHandleFunction func(event *common.TopicEvent) common.SubscriptionResponseStatus |
| 31 | + |
| 32 | +type SubscriptionOptions struct { |
| 33 | + PubsubName string |
| 34 | + Topic string |
| 35 | + DeadLetterTopic *string |
| 36 | + Metadata map[string]string |
| 37 | +} |
| 38 | + |
| 39 | +type Subscription struct { |
| 40 | + stream pb.Dapr_SubscribeTopicEventsAlpha1Client |
| 41 | + // lock locks concurrent writes to subscription stream. |
| 42 | + lock sync.Mutex |
| 43 | + closed atomic.Bool |
| 44 | +} |
| 45 | + |
| 46 | +type SubscriptionMessage struct { |
| 47 | + *common.TopicEvent |
| 48 | + sub *Subscription |
| 49 | +} |
| 50 | + |
| 51 | +func (c *GRPCClient) Subscribe(ctx context.Context, opts SubscriptionOptions) (*Subscription, error) { |
| 52 | + stream, err := c.subscribeInitialRequest(ctx, opts) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + return &Subscription{ |
| 58 | + stream: stream, |
| 59 | + }, nil |
| 60 | +} |
| 61 | + |
| 62 | +func (c *GRPCClient) SubscribeWithHandler(ctx context.Context, opts SubscriptionOptions, handler SubscriptionHandleFunction) (func() error, error) { |
| 63 | + s, err := c.Subscribe(ctx, opts) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + go func() { |
| 69 | + defer s.Close() |
| 70 | + |
| 71 | + for { |
| 72 | + msg, err := s.Receive() |
| 73 | + if err != nil { |
| 74 | + if !s.closed.Load() { |
| 75 | + logger.Printf("Error receiving messages from subscription pubsub=%s topic=%s, closing subscription: %s", |
| 76 | + opts.PubsubName, opts.Topic, err) |
| 77 | + } |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + go func() { |
| 82 | + if err := msg.respondStatus(handler(msg.TopicEvent)); err != nil { |
| 83 | + logger.Printf("Error responding to topic with event status pubsub=%s topic=%s message_id=%s: %s", |
| 84 | + opts.PubsubName, opts.Topic, msg.ID, err) |
| 85 | + } |
| 86 | + }() |
| 87 | + } |
| 88 | + }() |
| 89 | + |
| 90 | + return s.Close, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (s *Subscription) Close() error { |
| 94 | + if !s.closed.CompareAndSwap(false, true) { |
| 95 | + return errors.New("subscription already closed") |
| 96 | + } |
| 97 | + |
| 98 | + return s.stream.CloseSend() |
| 99 | +} |
| 100 | + |
| 101 | +func (s *Subscription) Receive() (*SubscriptionMessage, error) { |
| 102 | + event, err := s.stream.Recv() |
| 103 | + if err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + data := any(event.GetData()) |
| 108 | + if len(event.GetData()) > 0 { |
| 109 | + mediaType, _, err := mime.ParseMediaType(event.GetDataContentType()) |
| 110 | + if err == nil { |
| 111 | + var v interface{} |
| 112 | + switch mediaType { |
| 113 | + case "application/json": |
| 114 | + if err := json.Unmarshal(event.GetData(), &v); err == nil { |
| 115 | + data = v |
| 116 | + } |
| 117 | + case "text/plain": |
| 118 | + // Assume UTF-8 encoded string. |
| 119 | + data = string(event.GetData()) |
| 120 | + default: |
| 121 | + if strings.HasPrefix(mediaType, "application/") && |
| 122 | + strings.HasSuffix(mediaType, "+json") { |
| 123 | + if err := json.Unmarshal(event.GetData(), &v); err == nil { |
| 124 | + data = v |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + topicEvent := &common.TopicEvent{ |
| 132 | + ID: event.GetId(), |
| 133 | + Source: event.GetSource(), |
| 134 | + Type: event.GetType(), |
| 135 | + SpecVersion: event.GetSpecVersion(), |
| 136 | + DataContentType: event.GetDataContentType(), |
| 137 | + Data: data, |
| 138 | + RawData: event.GetData(), |
| 139 | + Topic: event.GetTopic(), |
| 140 | + PubsubName: event.GetPubsubName(), |
| 141 | + } |
| 142 | + |
| 143 | + return &SubscriptionMessage{ |
| 144 | + sub: s, |
| 145 | + TopicEvent: topicEvent, |
| 146 | + }, nil |
| 147 | +} |
| 148 | + |
| 149 | +func (s *SubscriptionMessage) Success() error { |
| 150 | + return s.respond(pb.TopicEventResponse_SUCCESS) |
| 151 | +} |
| 152 | + |
| 153 | +func (s *SubscriptionMessage) Retry() error { |
| 154 | + return s.respond(pb.TopicEventResponse_RETRY) |
| 155 | +} |
| 156 | + |
| 157 | +func (s *SubscriptionMessage) Drop() error { |
| 158 | + return s.respond(pb.TopicEventResponse_DROP) |
| 159 | +} |
| 160 | + |
| 161 | +func (s *SubscriptionMessage) respondStatus(status common.SubscriptionResponseStatus) error { |
| 162 | + var statuspb pb.TopicEventResponse_TopicEventResponseStatus |
| 163 | + switch status { |
| 164 | + case common.SubscriptionResponseStatusSuccess: |
| 165 | + statuspb = pb.TopicEventResponse_SUCCESS |
| 166 | + case common.SubscriptionResponseStatusRetry: |
| 167 | + statuspb = pb.TopicEventResponse_RETRY |
| 168 | + case common.SubscriptionResponseStatusDrop: |
| 169 | + statuspb = pb.TopicEventResponse_DROP |
| 170 | + default: |
| 171 | + return fmt.Errorf("unknown status, expected one of %s, %s, %s: %s", |
| 172 | + common.SubscriptionResponseStatusSuccess, common.SubscriptionResponseStatusRetry, |
| 173 | + common.SubscriptionResponseStatusDrop, status) |
| 174 | + } |
| 175 | + |
| 176 | + return s.respond(statuspb) |
| 177 | +} |
| 178 | + |
| 179 | +func (s *SubscriptionMessage) respond(status pb.TopicEventResponse_TopicEventResponseStatus) error { |
| 180 | + s.sub.lock.Lock() |
| 181 | + defer s.sub.lock.Unlock() |
| 182 | + |
| 183 | + return s.sub.stream.Send(&pb.SubscribeTopicEventsRequestAlpha1{ |
| 184 | + SubscribeTopicEventsRequestType: &pb.SubscribeTopicEventsRequestAlpha1_EventResponse{ |
| 185 | + EventResponse: &pb.SubscribeTopicEventsResponseAlpha1{ |
| 186 | + Id: s.ID, |
| 187 | + Status: &pb.TopicEventResponse{Status: status}, |
| 188 | + }, |
| 189 | + }, |
| 190 | + }) |
| 191 | +} |
| 192 | + |
| 193 | +func (c *GRPCClient) subscribeInitialRequest(ctx context.Context, opts SubscriptionOptions) (pb.Dapr_SubscribeTopicEventsAlpha1Client, error) { |
| 194 | + if len(opts.PubsubName) == 0 { |
| 195 | + return nil, errors.New("pubsub name required") |
| 196 | + } |
| 197 | + |
| 198 | + if len(opts.Topic) == 0 { |
| 199 | + return nil, errors.New("topic required") |
| 200 | + } |
| 201 | + |
| 202 | + stream, err := c.protoClient.SubscribeTopicEventsAlpha1(ctx) |
| 203 | + if err != nil { |
| 204 | + return nil, err |
| 205 | + } |
| 206 | + |
| 207 | + err = stream.Send(&pb.SubscribeTopicEventsRequestAlpha1{ |
| 208 | + SubscribeTopicEventsRequestType: &pb.SubscribeTopicEventsRequestAlpha1_InitialRequest{ |
| 209 | + InitialRequest: &pb.SubscribeTopicEventsInitialRequestAlpha1{ |
| 210 | + PubsubName: opts.PubsubName, Topic: opts.Topic, |
| 211 | + Metadata: opts.Metadata, DeadLetterTopic: opts.DeadLetterTopic, |
| 212 | + }, |
| 213 | + }, |
| 214 | + }) |
| 215 | + if err != nil { |
| 216 | + return nil, errors.Join(err, stream.CloseSend()) |
| 217 | + } |
| 218 | + |
| 219 | + return stream, nil |
| 220 | +} |
0 commit comments