Archived

This forum has been archived. Please start a new discussion on GitHub.

node.js and locking

I has the following function wich adds a group on a given channel to a murmur server:
function addGroup(serverId, groupName, channelId, callback) {
  var ic;

  Ice.Promise.try(function() {
    ic = Ice.initialize();
    var base = ic.stringToProxy("Meta:" + serverString);
    return Murmur.MetaPrx.checkedCast(base).then(function(murmurMeta) {
      return murmurMeta.getServer(serverId).then(function(server) {
        return server.getACL(channelId).then(function(acls, groups, inherit) {
          var obj = new Murmur.Group(groupName.toLowerCase(), true, true, true, [], [], []);
          groups.push(obj);
          return server.setACL(channelId, acls, groups, inherit).then(function() {
            return callback();
          });
        });
      });
    });
  }).finally(function() {
    if (ic) {
      ic.destroy();
    }
  }).exception(function(ex) {
    callback(ex);
  });
}

The problem there is, of course, that we have a race condition. Two callers can both read the ACL before the other one has a chance to write it to the server.
I think we need some kind of lock on this code and that can be done using something like redis. But my question is if there is a solution to this problem using just plain javascript and ICE.
Best regards and hopefully someone has a solution to this problem...

Comments

  • benoit
    benoit Rennes, France
    Hi,

    The solutions to this problem are pretty much the same as the ones used traditionally for database applications (transactions, optimistic locking, etc) and unless you can change the Murmur server API, I don't think you can guarantee that you're not going to overwrite a concurrent change.

    Here, the simplest would be to add support for an addGroup method to the Murmur server API and the server would take care of ensuring the modification is made atomically.

    Another option would for getACL to return a serial number and have setACL also take a serial number. The set would only succeed if the current serial number of the ACL matches. If it fails, the client would have to retry.

    Cheers,
    Benoit.