buffet: Queue multiple command update requests to the server
When we update command data quickly (e.g. progress, status, results)
we issue multiple asynchronous PATCH requests to GCD server for the
same command resource which end up overwriting each other's data.
Now if a PATCH request is in flight to the server, another PATCH
request for the same command resource will not be sent until the
previous one completes (either successfully or with an error).
In meantime, command property updates accumulate and will be sent
out to the server in the next request batch.
BUG=brillo:821
TEST=`FEATURES=test emerge-link buffet`
Deployed on device and tested with live GCD server.
Change-Id: I863a8b7689281e09017c8533f7613cef5681ff28
Reviewed-on: https://chromium-review.googlesource.com/266646
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/commands/cloud_command_proxy.h b/buffet/commands/cloud_command_proxy.h
index e78ff44..12c7f88 100644
--- a/buffet/commands/cloud_command_proxy.h
+++ b/buffet/commands/cloud_command_proxy.h
@@ -5,10 +5,12 @@
#ifndef BUFFET_COMMANDS_CLOUD_COMMAND_PROXY_H_
#define BUFFET_COMMANDS_CLOUD_COMMAND_PROXY_H_
-#include <base/macros.h>
-
+#include <bitset>
#include <string>
+#include <base/macros.h>
+#include <base/memory/weak_ptr.h>
+
#include "buffet/commands/command_proxy_interface.h"
namespace buffet {
@@ -24,14 +26,37 @@
~CloudCommandProxy() override = default;
// CommandProxyInterface implementation/overloads.
- void OnResultsChanged(const native_types::Object& results) override;
- void OnStatusChanged(const std::string& status) override;
- void OnProgressChanged(int progress) override;
+ void OnResultsChanged() override;
+ void OnStatusChanged() override;
+ void OnProgressChanged() override;
private:
+ // Flags used to mark the command resource parts that need to be updated on
+ // the server.
+ using CommandUpdateFlags = std::bitset<3>;
+
+ // Sends an asynchronous request to GCD server to update the command resource.
+ void SendCommandUpdate();
+
+ // Retry last failed request.
+ void ResendCommandUpdate();
+
+ // Callback invoked by the asynchronous PATCH request to the server.
+ // Called both in a case of successfully updating server command resource
+ // and in case of an error, indicated by the |success| parameter.
+ void OnUpdateCommandFinished(bool success);
+
CommandInstance* command_instance_;
DeviceRegistrationInfo* device_registration_info_;
+ // Set to true while a pending PATCH request is in flight to the server.
+ bool command_update_in_progress_{false};
+ // The flags indicating of new command resource updates since the last req.
+ CommandUpdateFlags new_pending_command_updates_;
+ // The flags indicating of command updates currently in flight.
+ CommandUpdateFlags in_progress_command_updates_;
+
+ base::WeakPtrFactory<CloudCommandProxy> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(CloudCommandProxy);
};