Suppress duplicate messages from a client (e.g. when network failure causes retries for RPCs that actually went through).

This commit is contained in:
Ian Gulliver
2014-05-16 23:07:38 +03:00
parent 81d7db3678
commit 0247b78c6b
3 changed files with 53 additions and 7 deletions

View File

@@ -133,8 +133,9 @@ Cosmopolite.prototype.sendMessage = function(subject, message, key) {
throw "cosmopolite: not ready";
}
var args = {
'subject': subject,
'message': JSON.stringify(message),
'subject': subject,
'message': JSON.stringify(message),
'sender_message_id': this.uuid_(),
};
if (key) {
args['key'] = key;
@@ -189,6 +190,23 @@ Cosmopolite.prototype.loggingPrefix_ = function() {
return 'cosmopolite (' + this.namespace_ + '):';
};
/**
* Generate a v4 UUID.
*
* @return {string} A universally-unique random value.
* @const
*/
Cosmopolite.prototype.uuid_ = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (Math.random() * 16) | 0;
if (c == 'x') {
return r.toString(16);
} else {
return (r & (0x03 | 0x08)).toString(16);
}
});
};
/**
* Callback when a script loads.
*/