Skip to content

Commit

Permalink
feat: Adds rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hui committed Feb 10, 2024
1 parent 909047b commit 72cda53
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/cellular/sf_cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
#include "product.hpp"
#include "sys/NVRAM.hpp"
#include "system.hpp"
#include "consts.hpp"

#include <stdint.h>

#include "Particle.h"

namespace sf::cloud
{
system_tick_t last_publish_time = 0;
int wait_connect(int timeout_ms)
{
uint16_t n_attempts;
Expand Down Expand Up @@ -74,6 +76,12 @@ namespace sf::cloud

int publish_blob(const char* title, const char* blob)
{
system_tick_t time_since_last = millis() - last_publish_time;
// SF_OSAL_printf("%u ms since last publish" __NL__, time_since_last);
if (time_since_last < SF_UPLOAD_MS_PER_TRANSMIT)
{
delay(SF_UPLOAD_MS_PER_TRANSMIT - time_since_last);
}
if (strlen(blob) > particle::protocol::MAX_EVENT_DATA_LENGTH)
{
return OVERSIZE_DATA;
Expand All @@ -90,6 +98,7 @@ namespace sf::cloud
{
return PUBLISH_FAIL;
}
last_publish_time = millis();
return SUCCESS;
}

Expand Down
1 change: 1 addition & 0 deletions src/cellular/sf_cloud.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace sf
/**
* @brief Publishes the specified message
*
* This function may block for as much as 20 seconds.
*
* @param title Message title. Must be 1-64 characters and only letters,
* numbers, underscores, dashes, or slashes.
Expand Down

0 comments on commit 72cda53

Please sign in to comment.