LP1912834 OpenSRF JS Max Parallel Requests Logging user/berick/lp1912834-max-parallel-net-logging
authorBill Erickson <berickxx@gmail.com>
Fri, 22 Jan 2021 20:53:40 +0000 (15:53 -0500)
committerBill Erickson <berickxx@gmail.com>
Wed, 27 Jan 2021 15:26:04 +0000 (10:26 -0500)
Log a console error message when the web client code sends more than
OSRF_MAX_PARALLEL_REQUESTS (currently 10) in parallel.  When the max
number of requests limit is reached and new requests continue to be
sent, an error message is logged to notify the developer to consider
refactoring the client code to use batched calls / API's.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
src/javascript/opensrf.js

index 03c79a9..83fc963 100644 (file)
@@ -64,6 +64,8 @@ var OSRF_STATUS_VERSIONNOTSUPPORTED = 505;
 // TODO: get path from ./configure prefix
 var SHARED_WORKER_LIB = '/js/dojo/opensrf/opensrf_ws_shared.js'; 
 
+var OSRF_MAX_PARALLEL_REQUESTS = 10; 
+
 /* The following classes map directly to network-serializable opensrf objects */
 
 function osrfMessage(hash) {
@@ -271,6 +273,9 @@ OpenSRF.set_subclass = function(cls, pcls) {
     eval(str);
 };
 
+// Some static request queueing functions.
+OpenSRF.throttleAlerted = false;
+OpenSRF.activeCount = 0;
 
 /* general session superclass */
 OpenSRF.Session = function() {
@@ -289,6 +294,29 @@ OpenSRF.Session.prototype.cleanup = function() {
 };
 
 OpenSRF.Session.prototype.send = function(osrf_msg, args) {
+
+    if (osrf_msg.type() != 'DISCONNECT') {
+        // DISCONNECT messages do not return a response, so there's
+        // no way to know when have completed.  Avoid tracking them
+        // as 'active'
+        OpenSRF.activeCount++;
+
+        if (OpenSRF.activeCount > OSRF_MAX_PARALLEL_REQUESTS 
+            && !OpenSRF.throttleAlerted) {
+
+            OpenSRF.throttleAlerted = true;
+            console.error('Too many [' +
+                OpenSRF.activeCount + '] active network requests in progress!' 
+                + ' Code requires refactoring to avoid so many active requests'
+            );
+        }
+    }
+
+    if (OpenSRF.activeCount >= OSRF_MAX_PARALLEL_REQUESTS) {
+        // Reset the alert state after the backlog clears.
+        OpenSRF.throttleAlerted = false;
+    }
+
     args = (args) ? args : {};
     switch(OpenSRF.Session.transport) {
         case OSRF_TRANSPORT_TYPE_WS:
@@ -706,6 +734,7 @@ OpenSRF.Stack.handle_message = function(ses, osrf_msg) {
 
 
         if(status == OSRF_STATUS_COMPLETE) {
+            OpenSRF.activeCount--;
             if(req) {
                 req.complete = true;
                 if(req.oncomplete && !req.oncomplete_called) {
@@ -717,6 +746,7 @@ OpenSRF.Stack.handle_message = function(ses, osrf_msg) {
 
         if(status == OSRF_STATUS_OK) {
             ses.state = OSRF_APP_SESSION_CONNECTED;
+            OpenSRF.activeCount--;
 
             /* call the connect callback */
             if(ses.onconnect && !ses.onconnect_called) {
@@ -727,6 +757,7 @@ OpenSRF.Stack.handle_message = function(ses, osrf_msg) {
 
         // capture all 400's and 500's as method errors
         if ((status+'').match(/^4/) || (status+'').match(/^5/)) {
+            OpenSRF.activeCount--;
             if(req && req.onmethoderror) 
                 return req.onmethoderror(req, status, status_text);
         }