Skip to content

Commit 7533d09

Browse files
committed
Refactor pubsub
1 parent 5b18df7 commit 7533d09

File tree

2 files changed

+11
-27
lines changed

2 files changed

+11
-27
lines changed

pubsub/health_checker.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ func NewPubHealthChecker(name string, client *pubsub.Client, resourceId string,
3434
}
3535
return &HealthChecker{name: name, client: client, permissionType: PermissionPublish, resourceId: resourceId, timeout: 4 * time.Second}
3636
}
37-
func NewSubHealthChecker(name string, client *pubsub.Client, resourceId string, timeout ...time.Duration) *HealthChecker {
38-
if len(timeout) >= 1 {
39-
return &HealthChecker{name: name, client: client, permissionType: PermissionSubscribe, resourceId: resourceId, timeout: timeout[0]}
40-
}
41-
return &HealthChecker{name: name, client: client, permissionType: PermissionSubscribe, resourceId: resourceId, timeout: 4 * time.Second}
42-
}
37+
4338
func (h *HealthChecker) Name() string {
4439
return h.name
4540
}

pubsub/publisher.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ import (
1111
var CheckTopicPermission = CheckPermission
1212

1313
type Publisher struct {
14-
Client *pubsub.Client
15-
Topic *pubsub.Topic
16-
Convert func(context.Context, []byte)([]byte, error)
14+
Client *pubsub.Client
15+
Topic *pubsub.Topic
16+
Convert func(context.Context, []byte) ([]byte, error)
1717
}
1818

19-
func NewPublisher(ctx context.Context, client *pubsub.Client, topicId string, c *TopicConfig, options...func(context.Context, []byte)([]byte, error)) *Publisher {
19+
func NewPublisher(ctx context.Context, client *pubsub.Client, topicId string, c *TopicConfig, options ...func(context.Context, []byte) ([]byte, error)) *Publisher {
2020
topic := client.Topic(topicId)
2121
CheckTopicPermission(ctx, topic.IAM(), "pubsub.topics.publish")
22-
var convert func(context.Context, []byte)([]byte, error)
22+
var convert func(context.Context, []byte) ([]byte, error)
2323
if len(options) > 0 {
2424
convert = options[0]
2525
}
2626
return &Publisher{Client: client, Topic: ConfigureTopic(topic, c), Convert: convert}
2727
}
2828

29-
func NewPublisherByConfig(ctx context.Context, c PublisherConfig, options...func(context.Context, []byte)([]byte, error)) (*Publisher, error) {
29+
func NewPublisherByConfig(ctx context.Context, c PublisherConfig, options ...func(context.Context, []byte) ([]byte, error)) (*Publisher, error) {
3030
if c.Retry.Retry1 <= 0 {
3131
client, err := NewPubSubClient(ctx, []byte(c.Client.Credentials), c.Client.ProjectId)
3232
if err != nil {
@@ -60,25 +60,13 @@ func ConfigureTopic(topic *pubsub.Topic, c *TopicConfig) *pubsub.Topic {
6060
}
6161
return topic
6262
}
63-
func (p *Publisher) Put(ctx context.Context, data []byte, attributes map[string]string) (string, error) {
64-
return p.Publish(ctx, data, attributes)
65-
}
66-
func (p *Publisher) Send(ctx context.Context, data []byte, attributes map[string]string) (string, error) {
67-
return p.Publish(ctx, data, attributes)
68-
}
69-
func (p *Publisher) Produce(ctx context.Context, data []byte, attributes map[string]string) (string, error) {
70-
return p.Publish(ctx, data, attributes)
71-
}
72-
func (p *Publisher) Write(ctx context.Context, data []byte, attributes map[string]string) (string, error) {
73-
return p.Publish(ctx, data, attributes)
74-
}
75-
func (p *Publisher) Publish(ctx context.Context, data []byte, attributes map[string]string) (string, error) {
63+
func (p *Publisher) Publish(ctx context.Context, data []byte, attributes map[string]string) error {
7664
var binary = data
7765
var err error
7866
if p.Convert != nil {
7967
binary, err = p.Convert(ctx, data)
8068
if err != nil {
81-
return "", err
69+
return err
8270
}
8371
}
8472
msg := &pubsub.Message{
@@ -89,7 +77,8 @@ func (p *Publisher) Publish(ctx context.Context, data []byte, attributes map[str
8977
}
9078

9179
publishResult := p.Topic.Publish(ctx, msg)
92-
return publishResult.Get(ctx)
80+
_, err = publishResult.Get(ctx)
81+
return err
9382
}
9483

9584
func CheckPermission(ctx0 context.Context, iam *iam.Handle, permission string) {

0 commit comments

Comments
 (0)