0

How would I modify the following to fail with concurrent writes (Using If-Match and ETag headers) ?

let sync = Runtime.getSync();
exports.handler = function(context, event, callback) {
  let map = sync.syncMaps("MyMap");
  map.syncMapItems(event.Digits).fetch().then(item => {
      map.syncMapItems(event.Digits).update({key: item.key, data:item.data + 1})
      .then(item2 => {
      }).catch(err => {
        console.log("Update Error:" + err);
      });
   }).catch(err => {
       console.log("Fetch Error:" + err);
   });
}
r.t.s.
  • 585
  • 1
  • 4
  • 12
  • What have you tried so far and what is causing you an issue with this? – philnash Jul 11 '19 at 04:18
  • I have read about the If-Match and ETag headers to solve the problems of concurrent writes ... and that they would be adding API support. I just can't find the documentation. Occasionally I get calls at the same time ... both issue a fetch, both issue an update, but I lose the results from the first update. A typical concurrent read modify write problem. There is a solution ... I just do not know or can find the appropriate documentation. – r.t.s. Jul 11 '19 at 14:49
  • [Is this the documentation you need?](https://www.twilio.com/docs/sync/mutation-and-conflict-resolution) – philnash Jul 12 '19 at 05:30
  • That's the documentation I did read ... I just can't find documentation on how to apply this to a Sync Map Item. – r.t.s. Jul 13 '19 at 17:27

1 Answers1

1

Twilio developer evangelist here.

The documentation on mutating data and protecting against conflicts in Twilio Sync does indeed mention that you can use If-Match and ETag headers.

The documentation calls out that:

Please note that If-Match header support is not currently enabled in the REST helper libraries. Support is coming soon.

So, if you want to use the If-Match header to ensure you are not writing conflicting entries to the Sync Map Item then you will need to build the HTTP requests yourself.

Everything in the documentation for Sync Documents and If-Match applies to individual Sync Map Items too.

The documentation for the REST API for Sync Map Items includes how to build up the URL you need to make the request yourself.

Let me know if this helps and if you have any trouble making the requests yourself.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • A nice/clean approach would be to have a mutate method on a Map (or other sync object) that that is similar to the update method, but instead of taking a key value, takes a function as an argument. That function has the current item as an argument and returns the mutated item. – r.t.s. Jul 15 '19 at 12:21
  • Yes, though it's still possible when dealing with the REST API that between you getting the latest update and then mutating it, that it could have been changed under the hood again. As the docs say, we'll hopefully get an implementation of at least the basic version soon. Otherwise, let me know how you get on. – philnash Jul 16 '19 at 01:41