防止多个chrome.storage API调用中出现竞争条件的最佳方法

Best way to prevent race condition in multiple chrome.storage API calls?

本文关键字:竞争 条件 方法 最佳 调用 chrome API storage      更新时间:2023-09-26
  1. 某事物请求执行任务
  2. 其他东西从存储中取出任务列表,并检查其中是否有任务
  3. 如果有任务,它会删除一个任务,较小的"任务列表"会重新存储

在步骤2和3之间,如果发生多个请求,并且同一任务将被服务两次,则可能会出现竞争条件

在"签出"单个任务时"锁定"任务表"以防止任何其他请求的正确解决方案是否正确?

什么是对性能影响最小的解决方案,例如执行延迟,以及如何使用chrome.storage API在javascript中实现?

一些代码例如:

function decide_response ( ) {
    if(script.replay_type == "reissue") {
            function next_task( tasks ) {
                var no_tasks = (tasks.length == 0);
                if( no_tasks ) {
                    target_complete_responses.close_requester();
                }
                else {
                    var next_task = tasks.pop();
                    function notify_execute () {
                        target_complete_responses.notify_requester_execute( next_task );
                    }
                    setTable("tasks", tasks, notify_execute);
                }
            }
            getTable( "tasks", next_tasks );
    ...
    }
...
}

我认为您可以利用javascript在上下文中是单线程的这一事实,即使使用异步chrome.storage API,也可以在没有锁的情况下进行管理。只要你不使用chrome.storage.sync,也就是说,如果云可能有变化,也可能没有变化,我认为所有的赌注都没有了

我会这样做(即兴写,没有测试,没有错误处理):

var getTask = (function() {
  // Private list of requests.
  var callbackQueue = [];
  // This function is called when chrome.storage.local.set() has
  // completed storing the updated task list.
  var tasksWritten = function(nComplete) {
    // Remove completed requests from the queue.
    callbackQueue = callbackQueue.slice(nComplete);
    // Handle any newly arrived requests.
    if (callbackQueue.length)
      chrome.storage.local.get('tasks', distributeTasks);
  };
  // This function is called via chrome.storage.local.get() with the
  // task list.
  var distributeTasks = function(items) {
    // Invoke callbacks with tasks.
    var tasks = items['tasks'];
    for (var i = 0; i < callbackQueue.length; ++i)
      callbackQueue[i](tasks[i] || null);
    // Update and store the task list. Pass the number of requests
    // handled as an argument to the set() handler because the queue
    // length may change by the time the handler is invoked.
    chrome.storage.local.set(
      { 'tasks': tasks.slice(callbackQueue.length) },
      function() {
        tasksWritten(callbackQueue.length);
      }
    );
  };
  // This is the public function task consumers call to get a new
  // task. The task is returned via the callback argument.
  return function(callback) {
    if (callbackQueue.push(callback) === 1)
      chrome.storage.local.get('tasks', distributeTasks);
  };
})();

这将来自使用者的任务请求作为回调存储在本地内存的队列中。当一个新的请求到达时,回调被添加到队列中,任务列表被提取iff这是队列中唯一的请求。否则,我们可以假设队列已经在处理中(这是一个隐式锁,只允许一个执行链访问任务列表)。

当任务列表被提取时,任务被分配给请求。请注意,如果在提取完成之前有多个请求到达,则可能会有多个。如果请求多于任务,则此代码只将null传递给回调。要阻止请求直到更多任务到达,请保留未使用的回调,并在添加任务时重新启动请求处理。如果任务既可以动态生成也可以动态消耗,请记住,也需要防止出现竞争条件,但此处未显示。

重要的是,在存储更新的任务列表之前,不要再次读取任务列表。为了实现这一点,在更新完成之前,不会从队列中删除请求。然后,我们需要确保处理在此期间到达的任何请求(可以缩短对chrome.storage.local.get()的调用,但为了简单起见,我这样做了)。

这种方法应该非常有效,因为它应该尽量减少对任务列表的更新,同时仍然尽可能快地做出响应。没有明确的锁定或等待。如果您在其他上下文中有任务使用者,请设置一个chrome.extension消息处理程序来调用getTask()函数。