Skip to content

Commit 38faead

Browse files
committed
feat: add setTimeout method to ClickHouse adapter for configurable HTTP request timeout
1 parent db011b1 commit 38faead

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

src/Usage/Adapter/ClickHouse.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ public function setUseFinal(bool $useFinal): self
108108
return $this;
109109
}
110110

111+
/**
112+
* Set the HTTP request timeout in milliseconds.
113+
*
114+
* @param int $milliseconds Timeout in milliseconds (min: 1000ms, max: 600000ms)
115+
* @return self
116+
* @throws Exception If timeout is out of valid range
117+
*/
118+
public function setTimeout(int $milliseconds): self
119+
{
120+
if ($milliseconds < 1000) {
121+
throw new Exception('Timeout must be at least 1000 milliseconds (1 second)');
122+
}
123+
if ($milliseconds > 600000) {
124+
throw new Exception('Timeout cannot exceed 600000 milliseconds (10 minutes)');
125+
}
126+
$this->client->setTimeout($milliseconds);
127+
return $this;
128+
}
129+
111130
/**
112131
* Enable or disable query logging for debugging.
113132
*

tests/Usage/Adapter/ClickHouseTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,94 @@ public function testHealthCheckFailure(): void
449449
$this->assertIsFloat($health['response_time']);
450450
}
451451
}
452+
453+
/**
454+
* Test setTimeout() method with valid timeout
455+
*/
456+
public function testSetTimeoutValid(): void
457+
{
458+
$host = getenv('CLICKHOUSE_HOST') ?: 'clickhouse';
459+
$username = getenv('CLICKHOUSE_USER') ?: 'default';
460+
$password = getenv('CLICKHOUSE_PASSWORD') ?: 'clickhouse';
461+
$port = (int) (getenv('CLICKHOUSE_PORT') ?: 8123);
462+
$secure = (bool) (getenv('CLICKHOUSE_SECURE') ?: false);
463+
464+
$adapter = new ClickHouseAdapter($host, $username, $password, $port, $secure);
465+
466+
// Test setting valid timeout
467+
$result = $adapter->setTimeout(5000); // 5 seconds
468+
469+
// Should return self for chaining
470+
$this->assertInstanceOf(ClickHouseAdapter::class, $result);
471+
472+
// Test that it still works after setting timeout
473+
$health = $adapter->healthCheck();
474+
$this->assertTrue($health['healthy']);
475+
}
476+
477+
/**
478+
* Test setTimeout() with minimum timeout (1 second)
479+
*/
480+
public function testSetTimeoutMinimum(): void
481+
{
482+
$host = getenv('CLICKHOUSE_HOST') ?: 'clickhouse';
483+
$username = getenv('CLICKHOUSE_USER') ?: 'default';
484+
$password = getenv('CLICKHOUSE_PASSWORD') ?: 'clickhouse';
485+
$port = (int) (getenv('CLICKHOUSE_PORT') ?: 8123);
486+
487+
$adapter = new ClickHouseAdapter($host, $username, $password, $port);
488+
$adapter->setTimeout(1000); // 1 second minimum
489+
490+
$this->assertTrue(true); // If we reach here, no exception was thrown
491+
}
492+
493+
/**
494+
* Test setTimeout() with maximum timeout (10 minutes)
495+
*/
496+
public function testSetTimeoutMaximum(): void
497+
{
498+
$host = getenv('CLICKHOUSE_HOST') ?: 'clickhouse';
499+
$username = getenv('CLICKHOUSE_USER') ?: 'default';
500+
$password = getenv('CLICKHOUSE_PASSWORD') ?: 'clickhouse';
501+
$port = (int) (getenv('CLICKHOUSE_PORT') ?: 8123);
502+
503+
$adapter = new ClickHouseAdapter($host, $username, $password, $port);
504+
$adapter->setTimeout(600000); // 10 minutes maximum
505+
506+
$this->assertTrue(true); // If we reach here, no exception was thrown
507+
}
508+
509+
/**
510+
* Test setTimeout() with timeout below minimum
511+
*/
512+
public function testSetTimeoutBelowMinimum(): void
513+
{
514+
$this->expectException(\Exception::class);
515+
$this->expectExceptionMessage('Timeout must be at least 1000 milliseconds');
516+
517+
$host = getenv('CLICKHOUSE_HOST') ?: 'clickhouse';
518+
$username = getenv('CLICKHOUSE_USER') ?: 'default';
519+
$password = getenv('CLICKHOUSE_PASSWORD') ?: 'clickhouse';
520+
$port = (int) (getenv('CLICKHOUSE_PORT') ?: 8123);
521+
522+
$adapter = new ClickHouseAdapter($host, $username, $password, $port);
523+
$adapter->setTimeout(999); // Below minimum
524+
}
525+
526+
/**
527+
* Test setTimeout() with timeout above maximum
528+
*/
529+
public function testSetTimeoutAboveMaximum(): void
530+
{
531+
$this->expectException(\Exception::class);
532+
$this->expectExceptionMessage('Timeout cannot exceed 600000 milliseconds');
533+
534+
$host = getenv('CLICKHOUSE_HOST') ?: 'clickhouse';
535+
$username = getenv('CLICKHOUSE_USER') ?: 'default';
536+
$password = getenv('CLICKHOUSE_PASSWORD') ?: 'clickhouse';
537+
$port = (int) (getenv('CLICKHOUSE_PORT') ?: 8123);
538+
539+
$adapter = new ClickHouseAdapter($host, $username, $password, $port);
540+
$adapter->setTimeout(600001); // Above maximum
541+
}
452542
}

0 commit comments

Comments
 (0)