Add the limit header information to the given response.

@param Response $response
@param int      $maxAttempts
@param int      $remainingAttempts
@param int|null $retryAfter

@return Response
517003
protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null)
    {
        $headers = [
            'X-RateLimit-Limit' => $maxAttempts,
            'X-RateLimit-Remaining' => $remainingAttempts,
        ];

        if (!is_null($retryAfter)) {
            $headers['Retry-After'] = $retryAfter;
        }

        $response->headers->add($headers);

        return $response;
    }
25626
public function setRequestAllowance(RequestInterface $request, ResponseInterface $response)
    {
        $route = $this->getRoute($request);

        $remaining = $response->getHeader('X-RateLimit-Remaining');
        $reset     = $response->getHeader('X-RateLimit-Reset');
        if (empty($remaining) || empty($reset) || (int) $remaining[0] > 0) {
            return;
        }

        $this->redis->set($this->getKey($route.'.reset'), static::MAX_TTL, $reset[0]);
    }
232002
public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1)
    {
        $key = $this->resolveRequestSignature($request);

        if ($this->limiter->tooManyAttempts($key, $maxAttempts, $decayMinutes)) {
            return $this->buildResponse($key, $maxAttempts);
        }

        $this->limiter->hit($key, $decayMinutes);

        $response = $next($request);

        return $this->addHeaders(
            $response, $maxAttempts,
            $this->calculateRemainingAttempts($key, $maxAttempts)
        );
    }
413417
protected function setHeadersIfNotProcessed(Response $response, \DateTime $lastMod)
    {
        $response->setNotModified();
        $response->setLastModified($lastMod);
    }
1161679
private function getRetryDelay() {
		return function ( $numberOfRetries, Response $response = null ) {
			// The $response argument is only passed as of Guzzle 6.2.2.
			if ( $response !== null ) {
				// Retry-After may be a number of seconds or an absolute date (RFC 7231,
				// section 7.1.3).
				$retryAfter = $response->getHeaderLine( 'Retry-After' );

				if ( is_numeric( $retryAfter ) ) {
					return 1000 * $retryAfter;
				}

				if ( $retryAfter ) {
					$seconds = strtotime( $retryAfter ) - time();
					return 1000 * max( 1, $seconds );
				}
			}

			return 1000 * $numberOfRetries;
		};
	}
882627
public function priority($priority, $retryTimeout = null, $expireAfter = null)
    {
        $this->noEmergencyWithoutRetryOrExpire($priority, $retryTimeout, $expireAfter);

        $this->priority = $priority;
        $this->retry = $retryTimeout;
        $this->expire = $expireAfter;

        return $this;
    }
1338490
@Override
	public boolean canRetry(RetryContext context) {
		Throwable t = context.getLastThrowable();
		return (t == null || retryForException(t))
				&& context.getRetryCount() < this.maxAttempts;
	}
57285
public function checkRateLimit($user, $request, $response, $action)
    {
        [$limit, $window] = $user->getRateLimit($request, $action);
        [$allowance, $timestamp] = $user->loadAllowance($request, $action);

        $current = time();

        $allowance += (int) (($current - $timestamp) * $limit / $window);
        if ($allowance > $limit) {
            $allowance = $limit;
        }

        if ($allowance < 1) {
            $user->saveAllowance($request, $action, 0, $current);
            $this->addRateLimitHeaders($response, $limit, 0, $window);
            throw new TooManyRequestsHttpException($this->errorMessage);
        }

        $user->saveAllowance($request, $action, $allowance - 1, $current);
        $this->addRateLimitHeaders($response, $limit, $allowance - 1, (int) (($limit - $allowance + 1) * $window / $limit));
    }
1181934
protected function addPreviousNextLinks( $request, $response ) {

		$page = (int) $request['page'];
		$max_pages = (int) ceil( $this->totalBooks / (int) $request['per_page'] );

		$response->header( 'X-WP-Total', (int) $this->totalBooks );
		$response->header( 'X-WP-TotalPages', $max_pages );

		$request_params = $request->get_query_params();
		$base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

		if ( $page > 1 ) {
			$prev_page = $page - 1;
			if ( $prev_page > $max_pages ) {
				$prev_page = $max_pages;
			}
			$prev_link = add_query_arg( 'page', $prev_page, $base );
			$response->link_header( 'prev', $prev_link );
		}
		if ( $max_pages > $page ) {
			$next_page = $page + 1;
			$next_link = add_query_arg( 'page', $next_page, $base );
			$response->link_header( 'next', $next_link );
		}
	}
154610
private function setHttpCacheHeaders(\DateTime $lastModified, $eTag, $maxAge)
    {
        $this->response->setMaxAge($maxAge);
        $this->response->setPublic();

        if ($this->maxAge === 0) {
            $this->response->headers->set('Cache-Control', 'no-cache');

            return;
        }

        $this->response->setLastModified($lastModified);
        $this->response->setEtag($eTag);
    }
315664
protected function buildResponse($key, $maxAttempts)
    {
        $response = new Response('Too Many Attempts.', 429);

        $retryAfter = $this->limiter->availableIn($key);

        return $this->addHeaders(
            $response, $maxAttempts,
            $this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter),
            $retryAfter
        );
    }
579860
protected function attemptConnection($maxAttempts)
    {
        $attempts = 0;
        $lastExcelption = null;

        do {

            if (!is_null($lastExcelption)) {
                // Not first error.
                $lastExcelption = null;

                $this->console->getSfStyleOutput()->block(
                    "Will attempt to re-connect to dedicated server in 30seconds"
                );
                sleep(30);
            }

            try {
                $this->console->writeln('Attempting to connect to the dedicated server!');

                $this->connection = Connection::factory(
                    $this->host,
                    $this->port,
                    $this->timeout,
                    $this->user,
                    $this->password
                );

            } catch (\Exception $e) {
                $lastExcelption = $e;
                $attempts++;
                $remainingAttemps = $maxAttempts - $attempts;

                $this->console->getSfStyleOutput()->error(
                    [
                        "Cound't connect to the dedicated server !",
                        "Attempt : $attempts, Remaining attemps : $remainingAttemps ",
                        $e->getMessage(),
                    ]
                );
            }
        } while($attempts < $maxAttempts && !is_null($lastExcelption));

        return $lastExcelption;
    }
1091709
public static function ratelimit_reset( $response, $repo ) {
		if ( isset( $response['headers']['x-ratelimit-reset'] ) ) {
			$reset                       = (int) $response['headers']['x-ratelimit-reset'];
			$wait                        = date( 'i', $reset - time() );
			static::$error_code[ $repo ] = array_merge(
				static::$error_code[ $repo ],
				[
					'git'  => 'github',
					'wait' => $wait,
				]
			);
		}
	}
77991
protected function _setHeaders($response)
    {
        $headers = $this->getConfig('headers');

        if (!is_array($headers)) {
            return $response;
        }

        return $response
            ->withHeader($headers['limit'], (string)$this->getConfig('limit'))
            ->withHeader($headers['remaining'], (string)$this->_getRemainingConnections())
            ->withHeader(
                $headers['reset'],
                (string)Cache::read($this->_getCacheExpirationKey(), static::$cacheConfig)
            );
    }
466392
public function retriesLeft($key, $maxAttempts)
    {
        $attempts = $this->attempts($key);

        return ($attempts === 0) ? $maxAttempts : $maxAttempts - $attempts + 1;
    }
1169005
private function checkPostRateLimit(ResponseInterface $response)
    {
        $postRemaining = $response->getHeaderLine('X-Post-Rate-Limit-Remaining');
        $postLimit = $response->getHeaderLine('X-Post-Rate-Limit-Limit');

        if ('' !== $postRemaining && $postRemaining < 1) {
            // X-Post-Rate-Limit-Reset: Time in seconds until your POST ratelimit is reset
            $resetTime = date('Y-m-d H:i:s', $response->getHeaderLine('X-Post-Rate-Limit-Reset'));

            throw new RateLimitException('No post credits available. The limit is ' . $postLimit . ' and will be reset at ' . $resetTime);
        }
    }
447148
function fullResponse() {
    function restifyResponseHeaders(req, res, next) {
        res.once('header', function () {

            // Restify 1.0 compatibility
            if (res.defaultResponseFormatters) {
                res.defaultResponseFormatters(res._data);
            }

            res.emit('beforeSend', res._data, res._body);

            // end backwards-compatibility
            return (setHeaders(req, res));
        });

        return (next());
    }

    return (restifyResponseHeaders);
}