OK, I’ve decided to start a new topic which is a fork of this earlier conversation . . . here:
To recap I am running my own server and want to change the throttling schedule . . . preferably to allow 4 .publish() events per second and ignore any overage (which there won’t be because I already impose throttling in my wiring code on the core).
So, I am combing through the source, looking for options, and lo and behold, in the spark-protocol.cpp, around line 459 we see this . . .
// Returns true on success, false on sending timeout or rate-limiting failure
bool SparkProtocol::send_event(const char *event_name, const char *data,int ttl, EventType::Enum event_type){
if (updating){
return false;
}
static system_tick_t recent_event_ticks[5] = {
(system_tick_t) -1000, (system_tick_t) -1000,
(system_tick_t) -1000, (system_tick_t) -1000,
(system_tick_t) -1000 };
static int evt_tick_idx = 0;
system_tick_t now = recent_event_ticks[evt_tick_idx] = callback_millis();
evt_tick_idx++;
evt_tick_idx %= 5;
if (now - recent_event_ticks[evt_tick_idx] < 1000) {
// exceeded allowable burst of 4 events per second <<< interesting
return false;
}
uint16_t msg_id = next_message_id();
size_t msglen = event(queue + 2, msg_id, event_name, data, ttl, event_type);
size_t wrapped_len = wrap(queue, msglen);
return (0 <= blocking_send(queue, wrapped_len));
}
So, 3 lines of questioning:
- What is happening here, it appears to be taking 5 measurements against callback_millis() and saying “hey, if the cumulative time over which the last 5 events transpired is greater than 1000 millis, then return false”, rather return no message.
a) is callback_millis() the number of milliseconds since the spark connected?
b) if returning false, how long until it is reset to true, or what resets it to true ?
- Let’s say I wanted to change this to only allow the first 4 events of every second through (true) and discard the rest (false) until the top of the next second, how would I do this?
a) if I made these changes how would I recompile the firmware (could I do this via the browser build, download, and flash via CLI). I am not sure how to see the complete firmware in the browser editor.
- Finally, there is the cloud code, in SparkCore.js there is reference to an EventSlowdown event, but I can’t find the code that actually does the throttling . . . or I don’t understand the C syntax very well!
Thank you Spark Community, this has been a wonderful adventure.
Jason