Skip to content

Commit

Permalink
fixed bug on subscription options
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreKerol committed May 27, 2019
1 parent 84c6576 commit b523e22
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions pubsub/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ type PubSubMessenger struct {
m sync.Mutex
}

// Subscribe perform a subscription on topic with given options
func (s *PubSubMessenger) Subscribe(ctx context.Context, topicName string, h Handler, opt *SubscriptionOptions) error {
// !! do not use opt param directly
options := s.checkOptions(opt)
topic, err := s.getTopic(ctx, topicName)
if err != nil {
return err
}
opt.SubscriptionName += "-" + topicName
sub, err := s.getSubscription(ctx, topic, opt)
options.SubscriptionName += "-" + topicName
sub, err := s.getSubscription(ctx, topic, options)
if err != nil {
return err
}
Expand Down Expand Up @@ -139,7 +142,6 @@ func (s *PubSubMessenger) getTopic(ctx context.Context, topicName string) (topic
}

func (s *PubSubMessenger) getSubscription(ctx context.Context, topic *pubsub.Topic, opt *SubscriptionOptions) (sub *ps.Subscription, err error) {
s.checkOptions(opt)
sub = s.c.Subscription(opt.SubscriptionName)
// Create the topic if it doesn't exist.
exists, err := sub.Exists(ctx)
Expand All @@ -156,12 +158,16 @@ func (s *PubSubMessenger) getSubscription(ctx context.Context, topic *pubsub.Top
return
}

func (s *PubSubMessenger) checkOptions(opt *SubscriptionOptions) {
func (s *PubSubMessenger) checkOptions(opt *SubscriptionOptions) *SubscriptionOptions {
if opt == nil {
panic("pubsub: subscription options can't be nil")
} else {
if opt.SubscriptionName == "" {
panic("invalid subscription name")
}
}
return &SubscriptionOptions{
ConcurrentHandlers: opt.ConcurrentHandlers,
SubscriptionName: opt.SubscriptionName,
}
}
2 changes: 1 addition & 1 deletion pubsub/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func parseOptions(opt *Options) (ret []option.ClientOption) {
ret = make([]option.ClientOption, 1)
ret[0] = option.WithCredentialsFile(opt.ServiceAccountPath)
} else if len(opt.Host) == 0 {
// preprod prod
// preprod
ret = make([]option.ClientOption, 0)
} else {
// old local env
Expand Down

0 comments on commit b523e22

Please sign in to comment.