Skip to content

Commit 9297f36

Browse files
authored
Merge pull request #2 from perfbaseorg/develop
Remove guzzle async http - for now
2 parents 319537b + 84f5999 commit 9297f36

File tree

6 files changed

+13
-113
lines changed

6 files changed

+13
-113
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ $config = Config::fromArray([
9595
| `track_file_operations` | boolean | `true` | Track file operations |
9696
| `proxy` | string | `null` | HTTP/HTTPS proxy for Perfbase API calls |
9797
| `timeout` | int | `10` | Timeout seconds for Perfbase API calls |
98-
| `async` | boolean | `true` | Send traces to Perfbase API asynchronously |
9998

10099
## License
101100

src/Config.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,6 @@ class Config
141141
*/
142142
public int $timeout = 10;
143143

144-
/**
145-
* Enable asynchronous delivery of profiling data
146-
* @var bool
147-
*/
148-
public bool $async_delivery = true;
149-
150-
151144
/**
152145
* @param string|null $api_key
153146
* @param string|null $api_url
@@ -168,7 +161,6 @@ class Config
168161
* @param bool|null $track_file_operations
169162
* @param string|null $proxy
170163
* @param int|null $timeout
171-
* @param bool|null $async_delivery
172164
*/
173165
public function __construct(
174166
?string $api_key = null,
@@ -189,8 +181,7 @@ public function __construct(
189181
?bool $track_aws_sdk = null,
190182
?bool $track_file_operations = null,
191183
?string $proxy = null,
192-
?int $timeout = null,
193-
?bool $async_delivery = null
184+
?int $timeout = null
194185
)
195186
{
196187
// Get all the properties of this class

src/Http/ApiClient.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace Perfbase\SDK\Http;
44

55
use GuzzleHttp\Client as GuzzleClient;
6-
use GuzzleHttp\Promise\PromiseInterface;
7-
use GuzzleHttp\Promise\Utils;
86
use JsonException;
97
use Perfbase\SDK\Config;
108
use Perfbase\SDK\Exception\PerfbaseApiKeyMissingException;
@@ -30,12 +28,6 @@ class ApiClient
3028
*/
3129
private GuzzleClient $httpClient;
3230

33-
/**
34-
* Promises to settle before the client is destroyed
35-
* @var array<PromiseInterface>
36-
*/
37-
private array $promises = [];
38-
3931
/**
4032
* @throws PerfbaseApiKeyMissingException
4133
*/
@@ -46,24 +38,25 @@ public function __construct(Config $config)
4638
}
4739

4840
$this->config = $config;
49-
5041
$this->defaultHeaders = [
5142
'Authorization' => 'Bearer ' . $this->config->api_key,
5243
'Accept' => 'application/json',
5344
'User-Agent' => 'Perfbase-PHP-SDK/1.0',
5445
'Content-Type' => 'application/json',
46+
'Connection' => 'keep-alive',
5547
];
5648

5749
/** @var array<string, mixed> $httpClientConfig */
5850
$httpClientConfig = [];
59-
6051
$httpClientConfig['base_uri'] = $config->api_url;
6152
$httpClientConfig['timeout'] = $config->timeout;
6253

54+
// Set up proxy if configured
6355
if ($config->proxy) {
6456
$httpClientConfig['proxy'] = $config->proxy;
6557
}
6658

59+
// Set up the HTTP client
6760
$this->httpClient = new GuzzleClient($httpClientConfig);
6861
}
6962

@@ -72,12 +65,10 @@ public function __construct(Config $config)
7265
*
7366
* @param string $endpoint API endpoint to send the request to
7467
* @param array<mixed> $data Data to send in the request body
75-
* @param bool $async If true, send asynchronously; if false, wait for response
76-
*
77-
* @return string|null Response data from the API, or null if non-blocking
78-
* @throws JsonException When the HTTP request fails or returns an error
68+
* @return void
69+
* @throws JsonException
7970
*/
80-
private function post(string $endpoint, array $data, bool $async = true): ?string
71+
private function submit(string $endpoint, array $data): void
8172
{
8273
// Prepare request options
8374
$options = [
@@ -86,38 +77,23 @@ private function post(string $endpoint, array $data, bool $async = true): ?strin
8677
];
8778

8879
try {
89-
if ($async) {
90-
$this->promises[] = $this->httpClient->postAsync($endpoint, $options);
91-
return null;
92-
} else {
93-
$response = $this->httpClient->post($endpoint, $options);
94-
return (string)$response->getBody();
95-
}
80+
$this->httpClient->post($endpoint, $options);
9681
} catch (Throwable $e) {
9782
// throw new PerfbaseException('HTTP Request failed: ' . $e->getMessage());
9883
}
99-
return null;
10084
}
10185

10286
/**
10387
* Submits a trace to the Perfbase API
10488
*
10589
* @param array<mixed> $data Data to send in the request body
106-
* @param bool $async If true, send asynchronously; if false, wait for response
90+
* @return void
10791
*
108-
* @return string|null Response data from the API, or null if non-blocking
10992
* @throws JsonException When the HTTP request fails or returns an error
11093
*/
111-
public function submitTrace(array $data, bool $async = true): ?string
94+
public function submitTrace(array $data): void
11295
{
113-
return $this->post('/v1/submit', $data, $async);
96+
$this->submit('/v1/submit', $data);
11497
}
11598

116-
public function __destruct()
117-
{
118-
// Attempt to settle all outstanding async HTTP promises without blocking
119-
if (!empty($this->promises)) {
120-
Utils::settle($this->promises)->wait(false);
121-
}
122-
}
12399
}

src/Tracing/TraceInstance.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public function __destruct()
103103
* The data is automatically sent to the Perfbase API for analysis.
104104
* @param bool $andSend If true, send the collected data to the API; if false, do not send
105105
* @throws PerfbaseStateException
106+
* @throws JsonException
106107
*/
107108
public function stopProfiling(bool $andSend = true): void
108109
{
@@ -136,8 +137,7 @@ public function stopProfiling(bool $andSend = true): void
136137
public function sendProfilingData(): void
137138
{
138139
$this->apiClient->submitTrace(
139-
$this->transformData(),
140-
$this->config->async_delivery
140+
$this->transformData()
141141
);
142142
}
143143

tests/ApiClientTest.php

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -48,69 +48,4 @@ public function testInitializesWithValidApiKey(): void
4848
$this->assertInstanceOf(ApiClient::class, $apiClient);
4949
}
5050

51-
/**
52-
* @return void
53-
* @throws PerfbaseApiKeyMissingException
54-
* @throws JsonException
55-
* @covers ::get
56-
*/
57-
public function testSendsSynchronousSubmitRequestAndReturnsResponse(): void
58-
{
59-
$mock = new MockHandler([
60-
new Response(200, [], 'Success'),
61-
]);
62-
$handlerStack = HandlerStack::create($mock);
63-
$guzzleClient = new GuzzleClient(['handler' => $handlerStack]);
64-
65-
$config = new Config();
66-
$config->api_key = 'test_api_key';
67-
68-
$apiClient = new ApiClient($config);
69-
$reflection = new ReflectionClass($apiClient);
70-
$property = $reflection->getProperty('httpClient');
71-
$property->setAccessible(true);
72-
$property->setValue($apiClient, $guzzleClient);
73-
74-
$response = $apiClient->submitTrace(['key' => 'value'], false);
75-
76-
$this->assertSame('Success', $response);
77-
}
78-
79-
/**
80-
* @return void
81-
* @throws JsonException
82-
* @throws PerfbaseApiKeyMissingException
83-
* @covers ::post
84-
*/
85-
public function testSendsAsynchronousSubmitRequestAndDoesNotBlock(): void
86-
{
87-
$mock = new MockHandler([
88-
new Response(200, [], 'Success'),
89-
]);
90-
$handlerStack = HandlerStack::create($mock);
91-
$guzzleClient = new GuzzleClient(['handler' => $handlerStack]);
92-
93-
$config = new Config();
94-
$config->api_key = 'test_api_key';
95-
96-
$apiClient = new ApiClient($config);
97-
$reflection = new ReflectionClass($apiClient);
98-
$property = $reflection->getProperty('httpClient');
99-
$property->setAccessible(true);
100-
$property->setValue($apiClient, $guzzleClient);
101-
102-
$response = $apiClient->submitTrace(['key' => 'value'], true);
103-
104-
$this->assertNull($response);
105-
106-
// Check if promises array is populated
107-
$promisesProperty = $reflection->getProperty('promises');
108-
$promisesProperty->setAccessible(true);
109-
110-
/** @var array<PromiseInterface> $promises */
111-
$promises = $promisesProperty->getValue($apiClient);
112-
$this->assertIsArray($promises);
113-
$this->assertCount(1, $promises);
114-
$this->assertInstanceOf(PromiseInterface::class, $promises[0]);
115-
}
11651
}

tests/ConfigTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public function testConstructorSetsProperties(): void
8888
$this->assertFalse($config->track_file_operations);
8989
$this->assertSame('http://proxy:8080', $config->proxy);
9090
$this->assertSame(1000, $config->timeout);
91-
$this->assertFalse($config->async_delivery);
9291
}
9392

9493
/**

0 commit comments

Comments
 (0)