// 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.throttleAlerted = false;
+OpenSRF.activeCount = 0;
+
+OpenSRF.sendFromQueue = function() {
+ var pending = OpenSRF.pendingRequests;
+
+ while (pending.length > 0 && OpenSRF.activeCount < MAX_PARALLEL_REQUESTS) {
+ var request = pending.shift();
+
+ var ses = request.session;
+ var osrf_msg = request.osrf_msg;
+ var req_args = request.req_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++;
+ }
+
+ 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;
+ }
+
+ // Useful for debugging issues with pending/active queues
+ //console.log('pending: ' + pending.length + ' active: ' + OpenSRF.activeCount);
+}
+
+OpenSRF.removeFromQueue = function() {
+ OpenSRF.activeCount--;
+
+ // 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) {
}
if(status == OSRF_STATUS_OK) {
+ OpenSRF.removeFromQueue(ses, osrf_msg);
ses.state = OSRF_APP_SESSION_CONNECTED;
/* call the connect callback */
// 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);
}