Skip to content

Too many requests from plugin #16

@luzat

Description

@luzat

The plugin currently executes $api->isAuthenticated() very often. One case is in the backend in Widget::register, which is called on just about every request. It contains this line:

if (!\is_admin() || $api->isAuthenticated()) {

This leads to requesting recipient lists on each call. This alone often adds 50-100+ ms for each page load in the backend. I don't think authentication needs to be checked in most cases, but mainly when explicitly asking for it or changing information; maybe using a cron job for checking settings, too. A working patch for the case above seems to be to replace the line above with:

if (!\is_admin() || $api->isConfigured()) {

I don't see any negative consequences coming from that. One could consider caching isAuthenticated in WordPress transients, too, and remove those once settings are changed.

Another serious problem that I encountered, but which may be system specific, is, that the request is happening using cURL and often takes 3-6 seconds (!). This occurs because WordPress at one point uses curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);. This leads to a very slow connection (maybe related to some outdated SSL/TLS configuration on client or server?). I was able to fix it using:

add_action('http_api_curl', static function ($handle, $args, $url) {
  if (preg_match('#^https://apiv\\d+\\.emailsys\\.net/#', $url) !== false) {
    curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
  }
}, 10, 3);

CURL_HTTP_VERSION_NONE works equally well and is probably more backwards compatible for old cURL versions. This may be a candidate for inclusion, too. Load times are down to a few 100 ms for a somewhat bloated backend compared to 4-7 seconds before.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions