Hi,
According to https://cloud.google.com/pubsub/docs/reference/service_apis_overview#service_endpoints, I can specify a service endpoint when using the PubSub service.
I tried this sample command:
CLOUDSDK_API_ENDPOINT_OVERRIDES_PUBSUB=https://us-central1-pubsub.googleapis.com/ \
gcloud pubsub topics publish test-topic \
--message=“test”
It works well.
I could not find any documentation about overriding the pubsub endpoint using GoLang. But I found an API signature that might do the job.
client, err := pubsub.NewClient(ctx, projectId, option.WithEndpoint("https://us-central1-pubsub.googleapis.com/"))
However, I got a "deadline exceeded" error. Either it is not the right way to override the PubSub endpoint, or the endpoint format is incorrect.
What is the right way to achieve what `CLOUDSDK_API_ENDPOINT_OVERRIDES_PUBSUB` does on the command line but using GoLang?
Thanks
Hey @davidshen84,
I am not an expert in this area, however I did some sleuthing within my resources. The option.WithEndpoint()
method that you are using is the correct way to override the Pub/Sub service endpoint in GoLang.
However, the "deadline exceeded" error that you are seeing may be due to other reasons, such as network connectivity issues or incorrect authentication. Here are a few things you can try to troubleshoot the issue:
Check if the Pub/Sub service endpoint URL is correct and accessible from your network.
Verify that you are using the correct authentication credentials (e.g., service account key file or application default credentials) and that they have the necessary permissions to access the Pub/Sub service.
Check if there are any firewall rules or network restrictions that may be blocking your connection to the Pub/Sub service endpoint.
Increase the timeout value for your Pub/Sub client to see if that resolves the "deadline exceeded" error. You can do this using the option.WithGRPCDialOption()
method with the grpc.WithTimeout()
option.
Here's an example code snippet that shows how to create a Pub/Sub client with a custom service endpoint and a longer timeout:
import (
"context"
"fmt"
"time"
"cloud.google.com/go/pubsub"
"google.golang.org/api/option"
"google.golang.org/grpc"
)
func main() {
ctx := context.Background()
// Set the custom service endpoint and timeout duration.
endpoint := "https://us-central1-pubsub.googleapis.com/"
timeout := 60 * time.Second
// Create a Pub/Sub client with the custom endpoint and timeout.
client, err := pubsub.NewClient(ctx, "my-project", option.WithEndpoint(endpoint), option.WithGRPCDialOption(grpc.WithTimeout(timeout)))
if err != nil {
fmt.Printf("Error creating Pub/Sub client: %v\n", err)
return
}
// Use the Pub/Sub client as usual.
topic := client.Topic("test-topic")
result := topic.Publish(ctx, &pubsub.Message{Data: []byte("test message")})
id, err := result.Get(ctx)
if err != nil {
fmt.Printf("Error publishing message: %v\n", err)
return
}
fmt.Printf("Message published with ID %s\n", id)
}
I hope this helps! Let me know if you have any further questions.