You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

110 lines
3.4 KiB

  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pubsub // import "cloud.google.com/go/pubsub"
  15. import (
  16. "context"
  17. "fmt"
  18. "os"
  19. "runtime"
  20. "time"
  21. "cloud.google.com/go/internal/version"
  22. vkit "cloud.google.com/go/pubsub/apiv1"
  23. "google.golang.org/api/option"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/keepalive"
  26. )
  27. const (
  28. // ScopePubSub grants permissions to view and manage Pub/Sub
  29. // topics and subscriptions.
  30. ScopePubSub = "https://www.googleapis.com/auth/pubsub"
  31. // ScopeCloudPlatform grants permissions to view and manage your data
  32. // across Google Cloud Platform services.
  33. ScopeCloudPlatform = "https://www.googleapis.com/auth/cloud-platform"
  34. maxAckDeadline = 10 * time.Minute
  35. )
  36. // Client is a Google Pub/Sub client scoped to a single project.
  37. //
  38. // Clients should be reused rather than being created as needed.
  39. // A Client may be shared by multiple goroutines.
  40. type Client struct {
  41. projectID string
  42. pubc *vkit.PublisherClient
  43. subc *vkit.SubscriberClient
  44. }
  45. // NewClient creates a new PubSub client.
  46. func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (c *Client, err error) {
  47. var o []option.ClientOption
  48. // Environment variables for gcloud emulator:
  49. // https://cloud.google.com/sdk/gcloud/reference/beta/emulators/pubsub/
  50. if addr := os.Getenv("PUBSUB_EMULATOR_HOST"); addr != "" {
  51. conn, err := grpc.Dial(addr, grpc.WithInsecure())
  52. if err != nil {
  53. return nil, fmt.Errorf("grpc.Dial: %v", err)
  54. }
  55. o = []option.ClientOption{option.WithGRPCConn(conn)}
  56. } else {
  57. o = []option.ClientOption{
  58. // Create multiple connections to increase throughput.
  59. option.WithGRPCConnectionPool(runtime.GOMAXPROCS(0)),
  60. option.WithGRPCDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
  61. Time: 5 * time.Minute,
  62. })),
  63. }
  64. o = append(o, openCensusOptions()...)
  65. }
  66. o = append(o, opts...)
  67. pubc, err := vkit.NewPublisherClient(ctx, o...)
  68. if err != nil {
  69. return nil, fmt.Errorf("pubsub: %v", err)
  70. }
  71. subc, err := vkit.NewSubscriberClient(ctx, option.WithGRPCConn(pubc.Connection()))
  72. if err != nil {
  73. // Should never happen, since we are passing in the connection.
  74. // If it does, we cannot close, because the user may have passed in their
  75. // own connection originally.
  76. return nil, fmt.Errorf("pubsub: %v", err)
  77. }
  78. pubc.SetGoogleClientInfo("gccl", version.Repo)
  79. subc.SetGoogleClientInfo("gccl", version.Repo)
  80. return &Client{
  81. projectID: projectID,
  82. pubc: pubc,
  83. subc: subc,
  84. }, nil
  85. }
  86. // Close releases any resources held by the client,
  87. // such as memory and goroutines.
  88. //
  89. // If the client is available for the lifetime of the program, then Close need not be
  90. // called at exit.
  91. func (c *Client) Close() error {
  92. // Return the first error, because the first call closes the connection.
  93. err := c.pubc.Close()
  94. _ = c.subc.Close()
  95. return err
  96. }
  97. func (c *Client) fullyQualifiedProjectName() string {
  98. return fmt.Sprintf("projects/%s", c.projectID)
  99. }