|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are |
| 8 | + * met: |
| 9 | + * |
| 10 | + * * Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * * Redistributions in binary form must reproduce the above |
| 13 | + * copyright notice, this list of conditions and the following disclaimer |
| 14 | + * in the documentation and/or other materials provided with the |
| 15 | + * distribution. |
| 16 | + * * Neither the name of Google Inc. nor the names of its |
| 17 | + * contributors may be used to endorse or promote products derived from |
| 18 | + * this software without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | + |
| 33 | +namespace Google\Cloud\Spanner\Middleware; |
| 34 | + |
| 35 | +use Google\ApiCore\Call; |
| 36 | +use Google\ApiCore\Middleware\MiddlewareInterface; |
| 37 | + |
| 38 | +/** |
| 39 | + * Middleware that adds the RequestId header to each rpc call made by spanner |
| 40 | + * |
| 41 | + * @internal |
| 42 | + */ |
| 43 | +class RequestIdHeaderMiddleware implements MiddlewareInterface |
| 44 | +{ |
| 45 | + private const REQUEST_ID_HEADER_NAME = 'x-goog-spanner-request-id'; |
| 46 | + private const VERSION = 1; |
| 47 | + private static string $process; |
| 48 | + private static int $currentClient = 1; |
| 49 | + private int $client; |
| 50 | + private int $channel; |
| 51 | + private int $request = 0; |
| 52 | + |
| 53 | + /** @var callable */ |
| 54 | + private $nextHandler; |
| 55 | + |
| 56 | + public function __construct(callable $nextHandler, int $channelId) |
| 57 | + { |
| 58 | + $this->nextHandler = $nextHandler; |
| 59 | + $this->channel = $channelId; |
| 60 | + $this->client = self::$currentClient++; |
| 61 | + } |
| 62 | + |
| 63 | + public function __invoke(Call $call, array $options) |
| 64 | + { |
| 65 | + $options['headers'][self::REQUEST_ID_HEADER_NAME] = [$this->getNewHeaderValue($options)]; |
| 66 | + $next = $this->nextHandler; |
| 67 | + return $next( |
| 68 | + $call, |
| 69 | + $options |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns a new Header value |
| 75 | + * |
| 76 | + * @param array $options The options passed to the middlewre from GAX. |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + private function getNewHeaderValue(array $options): string |
| 80 | + { |
| 81 | + $template = '%s.%s.%s.%s.%s.%s'; |
| 82 | + |
| 83 | + $process = $this->getProcess(); |
| 84 | + $client = $this->client; |
| 85 | + $channel = $this->channel; |
| 86 | + $attempt = $this->getAttempt($options); |
| 87 | + $request = $this->getNextRequestValue($attempt); |
| 88 | + |
| 89 | + return sprintf($template, self::VERSION, $process, $client, $channel, $request, $attempt); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Gets the process id for the RequestId header. |
| 94 | + * |
| 95 | + * @return string |
| 96 | + */ |
| 97 | + private function getProcess(): string |
| 98 | + { |
| 99 | + if (empty(self::$process)) { |
| 100 | + $rawProcess = random_bytes(8); |
| 101 | + // We want a hex encoded value |
| 102 | + self::$process = bin2hex($rawProcess); |
| 103 | + } |
| 104 | + |
| 105 | + return self::$process; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Reads the options array passed form GAX and looks for the `retryAttempt` value. |
| 110 | + * If none is found, returns 1, meaning this is the first attempt |
| 111 | + * |
| 112 | + * @param array $options The options passed in from GAX |
| 113 | + * @return int |
| 114 | + */ |
| 115 | + private function getAttempt(array $options): int |
| 116 | + { |
| 117 | + if (empty($options['retryAttempt'])) { |
| 118 | + return 1; |
| 119 | + } |
| 120 | + |
| 121 | + return $options['retryAttempt'] + 1; |
| 122 | + } |
| 123 | + |
| 124 | + private function getNextRequestValue(int $attempt): int |
| 125 | + { |
| 126 | + if ($attempt > 1) { |
| 127 | + return $this->request; |
| 128 | + } |
| 129 | + |
| 130 | + $this->request++; |
| 131 | + $currentValue = $this->request; |
| 132 | + return $currentValue; |
| 133 | + } |
| 134 | +} |
0 commit comments