Skip to content

Commit cb94fc6

Browse files
committed
Fixed retry at BAD_SESSION
1 parent 8c2018a commit cb94fc6

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/Traits/RequestTrait.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,17 @@ protected function processResponse($service, $method, $response, $resultClass)
222222
case StatusCode::BAD_SESSION:
223223
if (method_exists($this, 'refresh'))
224224
{
225+
$data = $this->last_request_data;
226+
225227
$session = $this->refresh();
226228

227229
if (isset($this->last_request_data['session_id']))
228230
{
229231
$this->last_request_data['session_id'] = $session->id();
230232
}
231233

232-
$this->saveLastRequest($service, $method, $this->last_request_data);
234+
$data['session_id'] = $session->id();
235+
$this->saveLastRequest($service, $method, $data);
233236

234237
// only 10 retries are allowed!
235238
if ($this->last_request_try_count < 10)
@@ -315,7 +318,7 @@ protected function retryLastRequest($sleep = 100)
315318
$this->logger()->info('Going to retry the last request!');
316319

317320
usleep(max($this->last_request_try_count, 1) * $sleep * 1000); // waiting 100 ms more
318-
$this->doRequest($this->last_request_service, $this->last_request_method, $this->last_request_data);
321+
return $this->doRequest($this->last_request_service, $this->last_request_method, $this->last_request_data);
319322
}
320323
}
321324

tests/RetryOnBadSessionTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace YdbPlatform\Ydb\Test;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
6+
use YdbPlatform\Ydb\Ydb;
7+
class SessionManager extends \YdbPlatform\Ydb\Session{
8+
public static function setSessionId(\YdbPlatform\Ydb\Session $session, string $id){
9+
$session->session_id = $id;
10+
return $session;
11+
}
12+
public static function getSessionId(\YdbPlatform\Ydb\Session $session){
13+
return $session->session_id;
14+
}
15+
}
16+
class RetryOnBadSessionTest extends TestCase
17+
{
18+
public function test(){
19+
20+
$config = [
21+
22+
// Database path
23+
'database' => '/local',
24+
25+
// Database endpoint
26+
'endpoint' => 'localhost:2136',
27+
28+
// Auto discovery (dedicated server only)
29+
'discovery' => false,
30+
31+
// IAM config
32+
'iam_config' => [
33+
'insecure' => true,
34+
],
35+
'credentials' => new AnonymousAuthentication()
36+
];
37+
$ydb = new Ydb($config);
38+
$table = $ydb->table();
39+
$session = $table->session();
40+
$oldSessionId = SessionManager::getSessionId($session);
41+
$session->delete();
42+
$session = $table->session();
43+
SessionManager::setSessionId($session,$oldSessionId);
44+
print_r($session->query('select 1 as res'));
45+
self::assertEquals(
46+
1,
47+
$session->query('select 1 as res')->rows()[0]['res']
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)