Skip to content

Commit 3f3fa21

Browse files
committed
retry also prepare
1 parent 377e00c commit 3f3fa21

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

src/Connection/Snowflake/SnowflakeStatement.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class SnowflakeStatement implements Statement
2222
*/
2323
private $dbh;
2424

25+
private RetryProxy $retry;
26+
2527
/**
2628
* @var resource
2729
*/
@@ -37,8 +39,30 @@ class SnowflakeStatement implements Statement
3739
/**
3840
* @param resource $dbh database handle
3941
*/
40-
public function __construct($dbh, string $query)
42+
public function __construct($dbh, string $query, RetryProxy|null $retry = null)
4143
{
44+
if ($retry === null) {
45+
$this->retry = new RetryProxy(
46+
new CallableRetryPolicy(
47+
function (Throwable $e): bool {
48+
if (str_contains($e->getMessage(), 'SYSTEM$ALLOWLIST')) {
49+
// Retry in case of SYSTEM$ALLOWLIST error #prod_24_7___inc_25140
50+
// this is usually accompanied with SNFLK incidents
51+
// or can happen in case hostname is wrong
52+
return true;
53+
}
54+
return false;
55+
},
56+
5, // 5 attempts
57+
),
58+
new UniformRandomBackOffPolicy(
59+
3_000, // 3 seconds
60+
10_000, // 10 seconds
61+
),
62+
);
63+
} else {
64+
$this->retry = $retry;
65+
}
4266
$this->dbh = $dbh;
4367
$this->query = $query;
4468
$this->stmt = $this->prepare();
@@ -49,7 +73,10 @@ public function __construct($dbh, string $query)
4973
*/
5074
private function prepare()
5175
{
52-
$stmt = @odbc_prepare($this->dbh, $this->query);
76+
/** @var resource|false $stmt */
77+
$stmt = $this->retry->call(function () {
78+
return @odbc_prepare($this->dbh, $this->query);
79+
});
5380
if (!$stmt) {
5481
throw DriverException::newFromHandle($this->dbh);
5582
}
@@ -87,27 +114,8 @@ public function execute($params = null): Result
87114
}
88115
}
89116

90-
$proxy = new RetryProxy(
91-
new CallableRetryPolicy(
92-
function (Throwable $e): bool {
93-
if (str_contains($e->getMessage(), 'SYSTEM$ALLOWLIST')) {
94-
// Retry in case of SYSTEM$ALLOWLIST error #prod_24_7___inc_25140
95-
// this is usually accompanied with SNFLK incidents
96-
// or can happen in case hostname is wrong
97-
return true;
98-
}
99-
return false;
100-
},
101-
5, // 5 attempts
102-
),
103-
new UniformRandomBackOffPolicy(
104-
3_000, // 3 seconds
105-
10_000, // 10 seconds
106-
),
107-
);
108-
109117
try {
110-
$proxy->call(function () {
118+
$this->retry->call(function () {
111119
odbc_execute(
112120
$this->stmt,
113121
$this->repairBinding($this->params),

0 commit comments

Comments
 (0)