// TODO: get path from ./configure prefix
var SHARED_WORKER_LIB = '/js/dojo/opensrf/opensrf_ws_shared.js';
+var MAX_PARALLEL_REQUESTS = 5;
+
/* The following classes map directly to network-serializable opensrf objects */
function osrfMessage(hash) {
};
+// Some static request queueing functions.
+OpenSRF.pendingRequests = [];
+OpenSRF.activeRequests = [];
+OpenSRF.throttleAlerted = false;
+
+OpenSRF.sendFromQueue = function() {
+ var pending = OpenSRF.pendingRequests;
+ var active = OpenSRF.activeRequests;
+
+ while (pending.length > 0 && active.length < MAX_PARALLEL_REQUESTS) {
+ var request = pending.shift();
+ active.push(request);
+
+ var ses = request.session;
+ var osrf_msg = request.osrf_msg;
+ var req_args = request.req_args;
+
+ ses.sendToNet(osrf_msg, req_args);
+ }
+
+ if (pending.length > 0) {
+ if (!OpenSRF.throttleAlerted) {
+
+ OpenSRF.throttleAlerted = true;
+ // Seeing this log in the console means the coder
+ // should consider refactoring to use batch calls.
+ console.warn('Net throttling activated on max requests');
+ }
+
+ } else {
+ // Reset now the backlog has been cleared.
+ OpenSRF.throttleAlerted = false;
+ }
+}
+
+OpenSRF.removeFromQueue = function(session, osrf_msg) {
+ var sesThread = session.thread;
+ var msgThread = osrf_msg.threadTrace();
+
+ // Start at the front of the list as those requests are
+ // more likely to be complete just by FIFO logic.
+ for (var idx = OpenSRF.activeRequests.length - 1; idx > 0; idx--) {
+ var req = OpenSRF.activeRequests[idx];
+ if (req.session.thread == sesThread &&
+ req.osrf_msg.threadTrace() == msgThread) {
+ OpenSRF.activeRequests.splice(idx, 1);
+ }
+ }
+
+ // Every completed request is a chance for a pending request
+ // to make it to the big leagues.
+ OpenSRF.sendFromQueue();
+}
+
/* general session superclass */
OpenSRF.Session = function() {
this.remote_id = null;
};
OpenSRF.Session.prototype.send = function(osrf_msg, args) {
+ OpenSRF.pendingRequests.push({
+ session: this,
+ osrf_msg: osrf_msg,
+ req_args: args
+ });
+ OpenSRF.sendFromQueue();
+}
+
+OpenSRF.Session.prototype.sendToNet = function(osrf_msg, args) {
args = (args) ? args : {};
switch(OpenSRF.Session.transport) {
case OSRF_TRANSPORT_TYPE_WS:
if(status == OSRF_STATUS_COMPLETE) {
+ OpenSRF.removeFromQueue(ses, osrf_msg);
if(req) {
req.complete = true;
if(req.oncomplete && !req.oncomplete_called) {
// capture all 400's and 500's as method errors
if ((status+'').match(/^4/) || (status+'').match(/^5/)) {
+ OpenSRF.removeFromQueue(ses, osrf_msg);
if(req && req.onmethoderror)
return req.onmethoderror(req, status, status_text);
}