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.
 
 
 

114 lines
3.5 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. "fmt"
  17. "os"
  18. "runtime"
  19. "time"
  20. "cloud.google.com/go/internal/version"
  21. vkit "cloud.google.com/go/pubsub/apiv1"
  22. "golang.org/x/net/context"
  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. )
  35. const (
  36. prodAddr = "https://pubsub.googleapis.com/"
  37. minAckDeadline = 10 * time.Second
  38. maxAckDeadline = 10 * time.Minute
  39. )
  40. // Client is a Google Pub/Sub client scoped to a single project.
  41. //
  42. // Clients should be reused rather than being created as needed.
  43. // A Client may be shared by multiple goroutines.
  44. type Client struct {
  45. projectID string
  46. pubc *vkit.PublisherClient
  47. subc *vkit.SubscriberClient
  48. }
  49. // NewClient creates a new PubSub client.
  50. func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (c *Client, err error) {
  51. var o []option.ClientOption
  52. // Environment variables for gcloud emulator:
  53. // https://cloud.google.com/sdk/gcloud/reference/beta/emulators/pubsub/
  54. if addr := os.Getenv("PUBSUB_EMULATOR_HOST"); addr != "" {
  55. conn, err := grpc.Dial(addr, grpc.WithInsecure())
  56. if err != nil {
  57. return nil, fmt.Errorf("grpc.Dial: %v", err)
  58. }
  59. o = []option.ClientOption{option.WithGRPCConn(conn)}
  60. } else {
  61. o = []option.ClientOption{
  62. // Create multiple connections to increase throughput.
  63. option.WithGRPCConnectionPool(runtime.GOMAXPROCS(0)),
  64. option.WithGRPCDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
  65. Time: 5 * time.Minute,
  66. })),
  67. }
  68. o = append(o, openCensusOptions()...)
  69. }
  70. o = append(o, opts...)
  71. pubc, err := vkit.NewPublisherClient(ctx, o...)
  72. if err != nil {
  73. return nil, fmt.Errorf("pubsub: %v", err)
  74. }
  75. subc, err := vkit.NewSubscriberClient(ctx, option.WithGRPCConn(pubc.Connection()))
  76. if err != nil {
  77. // Should never happen, since we are passing in the connection.
  78. // If it does, we cannot close, because the user may have passed in their
  79. // own connection originally.
  80. return nil, fmt.Errorf("pubsub: %v", err)
  81. }
  82. pubc.SetGoogleClientInfo("gccl", version.Repo)
  83. subc.SetGoogleClientInfo("gccl", version.Repo)
  84. return &Client{
  85. projectID: projectID,
  86. pubc: pubc,
  87. subc: subc,
  88. }, nil
  89. }
  90. // Close releases any resources held by the client,
  91. // such as memory and goroutines.
  92. //
  93. // If the client is available for the lifetime of the program, then Close need not be
  94. // called at exit.
  95. func (c *Client) Close() error {
  96. // Return the first error, because the first call closes the connection.
  97. err := c.pubc.Close()
  98. _ = c.subc.Close()
  99. return err
  100. }
  101. func (c *Client) fullyQualifiedProjectName() string {
  102. return fmt.Sprintf("projects/%s", c.projectID)
  103. }