Skip to content

Commit b89e8af

Browse files
authored
PHPLARA-259 Add connection-level maxTimeMS defaults (#3571)
1 parent 6ed32fe commit b89e8af

2 files changed

Lines changed: 73 additions & 5 deletions

File tree

src/Query/Builder.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,6 @@ public function toMql(): array
447447
$options = [];
448448

449449
// Apply order, offset, limit and projection
450-
if ($this->timeout) {
451-
$options['maxTimeMS'] = (int) ($this->timeout * 1000);
452-
}
453-
454450
if ($this->orders) {
455451
$options['sort'] = $this->grammar->prepareFieldsForQuery($this->orders);
456452
}
@@ -1778,6 +1774,12 @@ private static function vectorSearchSupportsAutoEmbedding(): bool
17781774
*/
17791775
private function inheritConnectionOptions(array $options = []): array
17801776
{
1777+
$queryTimeout = $this->timeout ? (int) ($this->timeout * 1000) : $this->connection->getConfig('options.maxTimeMS');
1778+
1779+
if ($queryTimeout !== null) {
1780+
$options['maxTimeMS'] ??= (int) $queryTimeout;
1781+
}
1782+
17811783
if (! isset($options['session'])) {
17821784
$session = $this->connection->getSession();
17831785
if ($session) {

tests/Query/BuilderTest.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Closure;
1010
use DateTimeImmutable;
1111
use Illuminate\Database\Eloquent\Collection;
12+
use Illuminate\Support\Arr;
1213
use Illuminate\Tests\Database\DatabaseQueryBuilderTest;
1314
use InvalidArgumentException;
1415
use LogicException;
@@ -57,6 +58,24 @@ public function testMql(array $expected, Closure $build, ?string $requiredMethod
5758
$this->assertEquals($expected, $mql, var_export($mql, true));
5859
}
5960

61+
#[DataProvider('provideConnectionOptions')]
62+
public function testConnectionOptionsAreInheritedByMql(array $expected, Closure $build, array $config): void
63+
{
64+
$builder = $build($this->getBuilder(true, $config));
65+
$this->assertInstanceOf(Builder::class, $builder);
66+
$mql = $builder->toMql();
67+
68+
if (isset($expected['find'][1])) {
69+
$expected['find'][1]['typeMap'] = ['root' => 'object', 'document' => 'array'];
70+
}
71+
72+
if (isset($expected['aggregate'][1])) {
73+
$expected['aggregate'][1]['typeMap'] = ['root' => 'object', 'document' => 'array'];
74+
}
75+
76+
$this->assertEquals($expected, $mql, var_export($mql, true));
77+
}
78+
6079
public static function provideQueryBuilderToMql(): iterable
6180
{
6281
/**
@@ -1769,10 +1788,57 @@ public function prepareFieldsForQuery(array $values, bool $root = true): array
17691788
);
17701789
}
17711790

1772-
private function getBuilder(bool $renameEmbeddedIdField = true): Builder
1791+
public static function provideConnectionOptions(): iterable
1792+
{
1793+
yield 'find inherits maxTimeMS from config' => [
1794+
['find' => [[], ['maxTimeMS' => 2000]]],
1795+
fn (Builder $builder) => $builder,
1796+
['options' => ['maxTimeMS' => 2000]],
1797+
];
1798+
1799+
yield 'aggregate inherits maxTimeMS from config' => [
1800+
[
1801+
'aggregate' => [
1802+
[['$group' => ['_id' => ['foo' => '$foo'], 'foo' => ['$last' => '$foo']]]],
1803+
['maxTimeMS' => 2000],
1804+
],
1805+
],
1806+
fn (Builder $builder) => $builder->groupBy('foo'),
1807+
['options' => ['maxTimeMS' => 2000]],
1808+
];
1809+
1810+
yield 'distinct inherits maxTimeMS from config' => [
1811+
['distinct' => ['foo', [], ['maxTimeMS' => 2000]]],
1812+
fn (Builder $builder) => $builder->distinct('foo'),
1813+
['options' => ['maxTimeMS' => 2000]],
1814+
];
1815+
1816+
yield 'options maxTimeMS overrides config maxTimeMS' => [
1817+
['find' => [[], ['maxTimeMS' => 5000]]],
1818+
fn (Builder $builder) => $builder->options(['maxTimeMS' => 5000]),
1819+
['options' => ['maxTimeMS' => 2000]],
1820+
];
1821+
1822+
yield 'timeout overrides config maxTimeMS' => [
1823+
['find' => [[], ['maxTimeMS' => 1000]]],
1824+
fn (Builder $builder) => $builder->timeout(1),
1825+
['options' => ['maxTimeMS' => 2000]],
1826+
];
1827+
1828+
yield 'empty config does not inject maxTimeMS' => [
1829+
['find' => [[], []]],
1830+
fn (Builder $builder) => $builder,
1831+
[],
1832+
];
1833+
}
1834+
1835+
private function getBuilder(bool $renameEmbeddedIdField = true, array $config = []): Builder
17731836
{
17741837
$connection = $this->createStub(Connection::class);
17751838
$connection->method('getRenameEmbeddedIdField')->willReturn($renameEmbeddedIdField);
1839+
$connection->method('getConfig')->willReturnCallback(
1840+
static fn (?string $key = null): mixed => Arr::get($config, $key),
1841+
);
17761842
$processor = $this->createStub(Processor::class);
17771843
$connection->method('getSession')->willReturn(null);
17781844
$connection->method('getQueryGrammar')->willReturn(new Grammar($connection));

0 commit comments

Comments
 (0)