• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

snowplow / snowplow-php-tracker / 5761683854

pending completion
5761683854

push

github

matus-tomlein
Merge branch 'release/0.7.0'

52 of 52 new or added lines in 3 files covered. (100.0%)

558 of 620 relevant lines covered (90.0%)

25.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/src/Emitters/RetryRequestManager.php
1
<?php
2
/*
3
    RetryRequestManager.php
4

5
    Copyright (c) 2014-2023 Snowplow Analytics Ltd. All rights reserved.
6

7
    This program is licensed to you under the Apache License Version 2.0,
8
    and you may not use this file except in compliance with the Apache License
9
    Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
10
    http://www.apache.org/licenses/LICENSE-2.0.
11

12
    Unless required by applicable law or agreed to in writing,
13
    software distributed under the Apache License Version 2.0 is distributed on
14
    an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15
    express or implied. See the Apache License Version 2.0 for the specific
16
    language governing permissions and limitations there under.
17
*/
18

19
namespace Snowplow\Tracker\Emitters;
20

21
use Snowplow\Tracker\Constants;
22

23
/**
24
 * Manages the state for retrying failed requests in emitters.
25
 */
26
class RetryRequestManager extends Constants {
27
    /// The number of times the current request has been retried
28
    private int $retry_count = 0;
29
    /// The maximum number of times to retry a request. Defaults to 1.
30
    private int $max_retry_attempts;
31
    /// The number of milliseconds to backoff before retrying a request. Defaults to 100ms.
32
    private int $backoff_ms;
33

34
    public function __construct(int $max_retry_attempts = NULL, int $backoff_ms = NULL) {
35
        $this->max_retry_attempts = $max_retry_attempts == NULL ? 1 : $max_retry_attempts;
54✔
36
        $this->backoff_ms = $backoff_ms == NULL ? 100 : $backoff_ms;
54✔
37
    }
38

39
    public function incrementRetryCount(): void {
40
        $this->retry_count++;
16✔
41
    }
42

43
    public function shouldRetryFailedRequest(): bool {
44
        return $this->shouldRetryForStatusCode(0);
×
45
    }
46

47
    public function shouldRetryForStatusCode(int $status_code): bool {
48
        if ($this->retry_count >= $this->max_retry_attempts) {
52✔
49
            return false;
14✔
50
        }
51

52
        // successful requests should not be retried
53
        if ($this->isGoodStatusCode($status_code)) {
52✔
54
            return false;
32✔
55
        }
56

57
        // don't retry certain 4xx codes, retry everything else
58
        return !in_array($status_code, self::NO_RETRY_STATUS_CODES);
20✔
59
    }
60

61
    public function isGoodStatusCode(int $status_code): bool {
62
        return $status_code >= 200 && $status_code < 300;
52✔
63
    }
64

65
    public function getBackoffDelayMs(): int {
66
        return $this->backoff_ms * pow(2, $this->retry_count - 1);
14✔
67
    }
68

69
    public function backoff(): void {
70
        usleep($this->getBackoffDelayMs() * 1000);
12✔
71
    }
72
}
73

74
?>
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc