Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If we publish two stream objects to two different topics, they should be able to run in parallel. #19

Open
bgood opened this issue Mar 17, 2021 · 0 comments
Labels
P2 Priority 2

Comments

@bgood
Copy link
Collaborator

bgood commented Mar 17, 2021

If we publish two stream objects to two different topics, they should be able to run in parallel, right? However, the current code only allows a single go routine to publish a message. We can change this to:

mux.Lock()
topic, ok := mClients[topicName]
if !ok {
	topic = pubsubClient.c.
		TopicInProject(topicName, config.ConfigurationMap.GOOGLE_PROJECT_ID)
	mClients[topicName] = topic
}
mux.Unlock()

message := &pubsub.Message{}
message.Data, _ = json.Marshal(streamObj)
_, err := topic.Publish(context.Background(), message).Get(ctx)
if err != nil {
	logger.LogError(err)
}

topic.Publish is a thread-safe method: https://github.com/googleapis/google-cloud-go/blob/pubsub/v1.8.2/pubsub/topic.go#L434-L435 (t.scheduler also has an internal sync.Mutex).

Originally posted by @hengfengli in #3 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 Priority 2
Projects
None yet
Development

No branches or pull requests

1 participant