|
| 1 | +# Client Configuration |
| 2 | + |
| 3 | +The Google Cloud Ruby Client Libraries allow you to configure client behavior via keyword arguments passed to the client constructor (or `configure` blocks). |
| 4 | + |
| 5 | +## 1. Customizing the API Endpoint |
| 6 | + |
| 7 | +You can modify the API endpoint to connect to a specific Google Cloud region or to a private endpoint. |
| 8 | + |
| 9 | +### Connecting to a Regional Endpoint |
| 10 | + |
| 11 | +```ruby |
| 12 | +require "google/cloud/pubsub" |
| 13 | + |
| 14 | +# Connect explicitly to the us-east1 region |
| 15 | +pubsub = Google::Cloud::Pubsub.new( |
| 16 | + endpoint: "us-east1-pubsub.googleapis.com:443" |
| 17 | +) |
| 18 | +``` |
| 19 | + |
| 20 | +## 2. Authenticating |
| 21 | + |
| 22 | +## See [Authentication](https://docs.cloud.google.com/ruby/docs/reference/help/authentication) for a comprehensive guide. |
| 23 | + |
| 24 | +## 3. Logging |
| 25 | + |
| 26 | +## See [Troubleshooting](https://docs.cloud.google.com/ruby/docs/reference/help/troubleshooting) for a comprehensive guide. |
| 27 | + |
| 28 | +## 4. Configuring a Proxy |
| 29 | + |
| 30 | +The configuration method depends on whether the client uses gRPC (most clients) or REST (e.g., Storage, BigQuery partial). |
| 31 | + |
| 32 | +### Proxy with gRPC |
| 33 | + |
| 34 | +The Ruby gRPC layer respects standard environment variables. You generally do not configure this in the Ruby code itself. |
| 35 | + |
| 36 | +Set the following environment variables in your shell or Docker container: |
| 37 | + |
| 38 | +``` |
| 39 | +export http_proxy="http://proxy.example.com:3128" |
| 40 | +export https_proxy="http://proxy.example.com:3128" |
| 41 | +``` |
| 42 | + |
| 43 | +**Handling Self-Signed Certificates (gRPC):** If your proxy uses a self-signed certificate, point gRPC to the CA bundle: |
| 44 | + |
| 45 | +``` |
| 46 | +export GRPC_DEFAULT_SSL_ROOTS_FILE_PATH="/path/to/roots.pem" |
| 47 | +``` |
| 48 | + |
| 49 | +### Proxy with REST (e.g., Google::Cloud::Storage) |
| 50 | + |
| 51 | +The underlying HTTP libraries in Ruby (often `Faraday` or `httpclient`) also respect the standard `http_proxy` environment variables automatically. |
| 52 | + |
| 53 | +However, if you must configure it in code (specifically for `Google::Cloud::Storage`), you can pass connection options: |
| 54 | + |
| 55 | +```ruby |
| 56 | +require "google/cloud/storage" |
| 57 | + |
| 58 | +storage = Google::Cloud::Storage.new( |
| 59 | + connection_options: { |
| 60 | + proxy: "http://user:password@proxy.example.com" |
| 61 | + } |
| 62 | +) |
| 63 | +``` |
| 64 | + |
| 65 | +## 5. Configuring Retries and Timeouts |
| 66 | + |
| 67 | +Ruby uses **Seconds** (Float/Integer) for time values. |
| 68 | + |
| 69 | +### Per-Call Configuration (Recommended) |
| 70 | + |
| 71 | +You can override settings for specific calls using keyword arguments (`retry_policy` and `timeout`). |
| 72 | + |
| 73 | +```ruby |
| 74 | +require "google/cloud/secret_manager" |
| 75 | + |
| 76 | +# Instantiate the client |
| 77 | +# Note: secret_manager_service gives access to the V1 GAPIC client |
| 78 | +client = Google::Cloud::SecretManager.secret_manager_service |
| 79 | + |
| 80 | +# Prepare the request |
| 81 | +parent = "projects/my-project" |
| 82 | + |
| 83 | +# Advanced Retry Configuration |
| 84 | +# Note: Time values are in SECONDS |
| 85 | +retry_policy = { |
| 86 | + initial_delay: 0.5, # Start with 0.5s wait |
| 87 | + max_delay: 5.0, # Cap wait at 5s |
| 88 | + multiplier: 2.0, # Double the wait each time |
| 89 | + retry_codes: [14] # Retry on specific gRPC error codes (e.g., UNAVAILABLE) |
| 90 | +} |
| 91 | + |
| 92 | +# Make the call |
| 93 | +request = { parent: parent } |
| 94 | +options = Gapic::CallOptions.new( |
| 95 | + retry_policy: retry_policy, |
| 96 | + timeout: 15.0 |
| 97 | +) |
| 98 | +client.list_secrets request, options |
| 99 | +``` |
| 100 | + |
| 101 | +### Available `retry_policy` Keys |
| 102 | + |
| 103 | +| Key | Type | Description | |
| 104 | +| ----- | ----- | ----- | |
| 105 | +| `initial_delay` | Float | Wait time before the first retry (in **seconds**). | |
| 106 | +| `max_delay` | Float | The maximum wait time between any two retries (in **seconds**). | |
| 107 | +| `multiplier` | Float | Multiplier applied to the delay after each failure. | |
| 108 | +| `retry_codes` | Array | List of gRPC error codes (integers) that should trigger a retry. | |
| 109 | + |
| 110 | +### Global Client Configuration |
| 111 | + |
| 112 | +You can configure defaults globally when initializing the low-level GAPIC client, though per-call is preferred for specific logic. |
| 113 | + |
| 114 | +```ruby |
| 115 | +require "google/cloud/pubsub" |
| 116 | + |
| 117 | +# Create a client with a custom timeout for all requests |
| 118 | +pubsub = Google::Cloud::Pubsub.new( |
| 119 | + timeout: 10 # Default 10 seconds for all operations |
| 120 | +) |
| 121 | +``` |
| 122 | + |
| 123 | +## 6. Other Common Configuration Options |
| 124 | + |
| 125 | +The following options can be passed to the constructor of generated clients (e.g., `Google::Cloud::Pubsub`, `Google::Cloud::Spanner`, `Google::Cloud::Storage`). |
| 126 | + |
| 127 | +| Option Key | Type | Description | |
| 128 | +| ----- | ----- | ----- | |
| 129 | +| `credentials` | String / Hash | Path to the JSON keyfile or the JSON object itself. | |
| 130 | +| `endpoint` | String | The address of the API remote host. Used for Regional Endpoints (e.g., `us-central1-pubsub.googleapis.com:443`). | |
| 131 | +| `lib_name` / `lib_version` | String | Used to append identification to the `x-goog-api-client` header for tracing/debugging. | |
| 132 | +| `timeout` | Numeric | The default timeout (in **seconds**) for requests. | |
| 133 | +| `retries` | Integer | Number of retries for the underlying HTTP/gRPC connection (distinct from logic retries). | |
| 134 | +| `project_id` | String | Explicitly sets the project ID, overriding environment variables. | |
| 135 | +| `universe_domain` | String | Overrides the default service domain (defaults to `googleapis.com`) for Cloud Universe support. | |
0 commit comments