Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repetitive Notif Checking triggers Error 508 (Loop Detected)

I call an AJAX to check DB if there is new notif every 3 or 10 seconds with the same query from 4 different browsers at the same time. But at some point after loop 100+, the server returns Error 508 (Loop Detected). This is just simple site so I don't think I need VPS server.

I added timestamp in SELECT as query differentiator, put unset, flush, mysqli_free_result, pause, mysqli_kill, mysqli_close, but error still occurs. Entry Processes hit 20/20.

Script

var counter = 1;
var notiftimer;

$(document).ready(function() {
    ajax_loadnotifs();
});

function ajax_loadnotifs() {
    $.ajax({
        type: "post",
        url: "service.php",
        dataType: "json",
        data: { action:'loadnotifs' },
        success: function(data, textStatus, jqXHR){
            $("div").append($("<p>").text(counter++ + ": succeeded"));

            notiftimer = setTimeout(function() {
                ajax_loadnotifs();
            }, 3000);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(jqXHR.responseText);
        }
    });
}

service.php

$link = mysqli_connect('localhost', 'root', 'root', 'testdb');
$notifs = array();

$query = "SELECT id, message FROM notifs LIMIT 20";
if (!$temp_notifs = mysqli_query($link, $query)) {
    die(json_encode(array("errmsg" => "Selecting notifs.")));
}

while($notif = mysqli_fetch_assoc($temp_notifs)) {
    $notifs[] = $notif;
}

mysqli_close($link);        
echo json_encode($notifs);

cPanel - Resource Usage Overview

cPanel - Resource Usage Overview

When Entry Processes hits 20/20, I get Error 508. How to maintain low server Entry Processes? (Tested with 4 different browsers, run them all until loop 100+ on shared hosting. No issue on local computer)

like image 613
Jeaf Gilbert Avatar asked Sep 13 '17 06:09

Jeaf Gilbert


1 Answers

What is considered an Entry Processes?

An "Entry Process" is how many PHP scripts you have running at a single time.

Source: https://billing.stablehost.com/knowledgebase/186/What-is-considered-an-Entry-Processes.html

So the underlying problem as you've found out is eventually you are running too many processes at the same time. There are a few things you can do to solve the issue.

Option 1

Find a new web host. This is perhaps the simplest but also the most costly depending on what sort of financial arrangement you have with your current host. Find one that does not have this restriction.

Option 2

Increase the time between ajax requests. Why do you need to request every 3 seconds? That is a very, very short amount of time. What about 15 seconds? Or 30 seconds? Or heck, even 1 minute? Your users probably don't need their data refreshed as often as you think.

Option 3

Only perform the ajax call if the current tab/window is in focus. There's no reason to keep polling for notifications if the user isn't even looking at your page.

Check out Document.hasFocus(): https://developer.mozilla.org/en-US/docs/Web/API/Document/hasFocus

Option 4

Implement a caching layer. If you feel like you still need to request data very, very often then improve how quickly you retrieve this data. How you implement caching is up to you but in some cases even using a file write/read can reduce the amount of time and resources needed to fulfill the request.

After you get the notifications from the database simply save the JSON into a text file and have subsequent resquests delivered from there until the database data changes. See if this improves performance.

If you want to get even more focused on caching you can look at options like Memcached (https://en.wikipedia.org/wiki/Memcached) or Redis (https://en.wikipedia.org/wiki/Redis).

Try combining multiple options for even better performance!

like image 126
Mike S Avatar answered Sep 29 '22 20:09

Mike S