Skip to content
This repository was archived by the owner on May 27, 2024. It is now read-only.

Commit 044aa73

Browse files
Socket madness problem fix (#2)
1 parent c97a240 commit 044aa73

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ CHANGELOG
33

44
[//]: # (comment: Don't forget to update src/DogStatsd.php:DogStatsd::version when releasing a new version)
55

6+
# 1.6.1 / 2023-06-22
7+
* Added message counter
8+
* Close socket after `$maxCountMessagesOnSocketSession` number of messages sent
9+
610
# 1.6.0 / 2023-05-1
711
* Lazy creation of sockets
812
* Resend on socket errors. Default max attempts is 2. Can be configured with `max_attempts_to_send` option.

src/BatchedDogStatsd.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
class BatchedDogStatsd extends DogStatsd
1616
{
17+
public static $maxCountMessagesOnSocketSession = 0;
1718
public static $maxBufferLength = 50;
1819
private static $buffer = [];
1920
private static $bufferLength = 0;

src/DogStatsd.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class DogStatsd
1515

1616
const DEFAULT_MAX_ATTEMPTS_TO_SEND = 1;
1717
// phpcs:enable
18-
public static $version = '1.6.0';
18+
public static $version = '1.6.1';
1919
private static $eventUrl = '/api/v1/events';
2020
/**
2121
* @var bool|resource|\Socket
@@ -87,6 +87,9 @@ class DogStatsd
8787
// Used for the telemetry tags
8888
private $packets_dropped;
8989

90+
private $countSentMessages = 0;
91+
public static $maxCountMessagesOnSocketSession = 50;
92+
9093
/**
9194
* DogStatsd constructor, takes a configuration array. The configuration can take any of the following values:
9295
* host,
@@ -508,6 +511,9 @@ protected function sendMessage($message, $attempt = 0)
508511
return $this->sendMessage($message, ++$attempt);
509512
}
510513
}
514+
} elseif (++$this->countSentMessages > static::$maxCountMessagesOnSocketSession) {
515+
$this->closeSocket();
516+
$this->countSentMessages = 0;
511517
}
512518

513519
return $res;

tests/UnitTests/BatchedDogStatsdTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,24 @@ public function testReportSendsOnceBufferIsFilled()
3838
$batchedDog->gauge($udpMessage . '2', 21);
3939

4040
$spy = $this->getSocketSpy();
41+
4142
$this->assertSame(
4243
0,
4344
count($spy->argsFromSocketSendtoCalls),
4445
'Should not have sent any message until the buffer is full'
4546
);
46-
4747
$batchedDog->gauge($udpMessage . '3', 21);
4848

4949
$this->assertSame(
5050
1,
5151
count($spy->argsFromSocketSendtoCalls),
5252
'Should send all buffered UDP messages once buffer is filled'
5353
);
54+
$this->assertSame(
55+
1,
56+
count($spy->argsFromSocketCloseCalls),
57+
'Socket was not closed'
58+
);
5459

5560
$this->assertSameTelemetry(
5661
$expectedUdpMessageOnceSent,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace DataDog\UnitTests\DogStatsd;
4+
5+
use DataDog\DogStatsd;
6+
use DataDog\TestHelpers\SocketSpyTestCase;
7+
8+
class CountSentMessagesTest extends SocketSpyTestCase
9+
{
10+
public function testReportDoesNotSendIfBufferNotFilled()
11+
{
12+
$batchedDog = new DogStatsd();
13+
14+
$batchedDog->report('some fake UDP message');
15+
16+
$spy = $this->getSocketSpy();
17+
18+
$this->assertSame(
19+
1,
20+
count($spy->argsFromSocketSendtoCalls),
21+
'Should sent UDP message'
22+
);
23+
24+
$this->assertSame(
25+
0,
26+
count($spy->argsFromSocketCloseCalls),
27+
'Socket should be not closed'
28+
);
29+
}
30+
31+
public function testReportSendsOnceBufferIsFilled()
32+
{
33+
$batchedDog = new DogStatsd();
34+
35+
$batchedDog::$maxCountMessagesOnSocketSession = 2;
36+
37+
$udpMessage = 'some fake UDP message';
38+
39+
$spy = $this->getSocketSpy();
40+
41+
$batchedDog->gauge($udpMessage . '1', 21);
42+
$batchedDog->gauge($udpMessage . '2', 21);
43+
$batchedDog->gauge($udpMessage . '3', 21);
44+
45+
$this->assertSame(
46+
3,
47+
count($spy->argsFromSocketSendtoCalls),
48+
'Should send all UDP messages'
49+
);
50+
$this->assertSame(
51+
1,
52+
count($spy->argsFromSocketCloseCalls),
53+
'Socket was not closed'
54+
);
55+
56+
$this->assertSame(
57+
"some fake UDP message1:21|g\n",
58+
$spy->argsFromSocketSendtoCalls[0][1],
59+
'Should concatenate UDP messages with newlines'
60+
);
61+
$this->assertSame(
62+
"some fake UDP message2:21|g\n",
63+
$spy->argsFromSocketSendtoCalls[1][1],
64+
'Should concatenate UDP messages with newlines'
65+
);
66+
$this->assertSame(
67+
"some fake UDP message3:21|g\n",
68+
$spy->argsFromSocketSendtoCalls[2][1],
69+
'Should concatenate UDP messages with newlines'
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)