-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from emidander/AddGooglePubSub
Add new publisher for Google Pub Sub.
- Loading branch information
Showing
5 changed files
with
132 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package publisher | ||
|
||
import ( | ||
"context" | ||
"github.com/goccy/go-json" | ||
) | ||
|
||
type GooglePubSubPublisher struct { | ||
pubSubConnection *PubSubConnection | ||
} | ||
|
||
func NewGooglePubSubPublisher(pubSubConnection *PubSubConnection) *GooglePubSubPublisher { | ||
return &GooglePubSubPublisher{ | ||
pubSubConnection, | ||
} | ||
} | ||
|
||
// Publish send events, implements eventPublisher. | ||
func (p *GooglePubSubPublisher) Publish(ctx context.Context, topic string, event Event) error { | ||
body, err := json.Marshal(event) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return p.pubSubConnection.Publish(ctx, topic, body) | ||
} | ||
|
||
func (p *GooglePubSubPublisher) Close() error { | ||
return p.pubSubConnection.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package publisher | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"sync" | ||
|
||
"cloud.google.com/go/pubsub" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
type PubSubConnection struct { | ||
logger *slog.Logger | ||
client *pubsub.Client | ||
projectID string | ||
topics map[string]*pubsub.Topic | ||
mu sync.RWMutex | ||
} | ||
|
||
func NewPubSubConnection(ctx context.Context, logger *slog.Logger, pubSubProjectId string) (*PubSubConnection, error) { | ||
if pubSubProjectId == "" { | ||
return nil, fmt.Errorf("project id is required for pub sub connection") | ||
} | ||
|
||
c, err := pubsub.NewClient(ctx, pubSubProjectId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &PubSubConnection{ | ||
logger: logger, | ||
client: c, | ||
projectID: pubSubProjectId, | ||
topics: make(map[string]*pubsub.Topic), | ||
mu: sync.RWMutex{}, | ||
}, nil | ||
} | ||
|
||
func (c *PubSubConnection) getTopic(topic string) *pubsub.Topic { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
if top, ok := c.topics[topic]; ok { | ||
return top | ||
} | ||
|
||
t := c.client.TopicInProject(topic, c.projectID) | ||
t.PublishSettings.NumGoroutines = 1 | ||
t.PublishSettings.CountThreshold = 1 | ||
c.topics[topic] = t | ||
return t | ||
} | ||
|
||
func (c *PubSubConnection) Publish(ctx context.Context, topic string, data []byte) error { | ||
t := c.getTopic(topic) | ||
defer t.Flush() | ||
|
||
var res *pubsub.PublishResult | ||
var err error | ||
res = t.Publish(ctx, &pubsub.Message{ | ||
Data: data, | ||
}) | ||
|
||
_, err = res.Get(ctx) | ||
if err != nil { | ||
c.logger.Error("Failed to publish message", "err", err) | ||
if status.Code(err) == codes.NotFound { | ||
return fmt.Errorf("topic not found %w", err) | ||
} else { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *PubSubConnection) Close() error { | ||
return c.client.Close() | ||
} |