Skip to content

feat: share connection & DNS Cache to CURLRequest - #9557

Merged
michalsn merged 9 commits into
codeigniter4:4.7from
ddevsr:share-connection-curl
May 26, 2025
Merged

feat: share connection & DNS Cache to CURLRequest#9557
michalsn merged 9 commits into
codeigniter4:4.7from
ddevsr:share-connection-curl

Conversation

@ddevsr

Copy link
Copy Markdown
Collaborator

Description
I need this for reuse connection. As soon PHP 8.5 will have new function for persistent, Benefit choosing persistent_id by PHP (read more on RFC).

php/php-src@d20880c#diff-6db2da0f77fa4c56808260fdf566a723bbb52d6159077953097d05d995335558R135-R245

https://wiki.php.net/rfc/curl_share_persistence_improvement

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@michalsn

Copy link
Copy Markdown
Member

This is a nice improvement, but I think it would benefit from being more configurable.

How about introducing a $share array in the config with default values like connect and dns, and optionally supporting others like cookie, session, and psl? This way, we can control what gets shared based on the use case. If the array is empty, we can skip calling curl_share_init() entirely, which avoids unnecessary memory allocation.

We should also note in the user guide that the $share option makes the most sense when making requests to the same domain.

It's a bit unfortunate that we already have $shareOptions property in the config - hopefully it won't be confusing.

@ddevsr

Copy link
Copy Markdown
CollaboratorAuthor

In new function persistent that avoid cookie, we avoid too?

@michalsn

Copy link
Copy Markdown
Member

Do you mean curl_share_init_persistent()?
I think we should introduce a new configuration option, such as sharePersistent, which would default to false. If persistence is enabled, I’d still accept all share options - PHP will handle any invalid ones, like the cookie option, by throwing an error.

@ddevsrddevsr added enhancement PRs that improve existing functionalities 4.7 labels May 15, 2025
@ddevsr
ddevsrforce-pushed the share-connection-curl branch from 8d5fe45 to 3c20b47CompareMay 16, 2025 14:46
@ddevsr

Copy link
Copy Markdown
CollaboratorAuthor
<?php$optShareConnection = [
CURL_LOCK_DATA_CONNECT,
CURL_LOCK_DATA_DNS,
];
$this->shareConnection = curl_share_init();
foreach (array_unique($optShareConnection) as$opt) {
curl_share_setopt($this->shareConnection, CURLSHOPT_SHARE, $opt);
}

same as persistent implementation in next PR (when ready):

<?php$optShareConnection = [
CURL_LOCK_DATA_CONNECT,
CURL_LOCK_DATA_DNS,
];
$this->shareConnection = curl_share_init_persistent($optShareConnection);

@ddevsr
ddevsr marked this pull request as ready for review May 16, 2025 15:04
@ddevsr
ddevsr marked this pull request as draft May 16, 2025 15:20
@ddevsr
ddevsr marked this pull request as ready for review May 16, 2025 16:09
Comment threadapp/Config/CURLRequest.php Outdated
Comment threaduser_guide_src/source/libraries/curlrequest.rst Outdated
@ddevsrddevsr changed the title feat: share Connection & DNS Cache to CURLRequestfeat: share connection & DNS Cache to CURLRequestMay 19, 2025
Comment threaduser_guide_src/source/libraries/curlrequest.rst Outdated
Comment threadapp/Config/CURLRequest.php Outdated
Comment threadsystem/HTTP/CURLRequest.php Outdated
Comment threadsystem/HTTP/CURLRequest.php
Comment threaduser_guide_src/source/libraries/curlrequest/039.php Outdated
Comment threaduser_guide_src/source/libraries/curlrequest/040.php Outdated
@ddevsr
ddevsrforce-pushed the share-connection-curl branch from 80cca03 to 251d092CompareMay 19, 2025 13:40
@michalsn
michalsn merged commit 4046b8d into codeigniter4:4.7May 26, 2025
@michalsn

Copy link
Copy Markdown
Member

Thank you @ddevsr

@ddevsr
ddevsr deleted the share-connection-curl branch May 26, 2025 12:53
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementPRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@ddevsr@michalsn@paulbalandan