Skip to content

Commit f2417de

Browse files
committed
improve live network pacing
1 parent 52d9ad8 commit f2417de

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

examples/LiveNetworkNode/LiveNetworkNode.ino

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,19 @@ bool isWiFiConnected() {
6868

6969
void connectWiFi() {
7070
const wl_status_t status = WiFi.status();
71-
if (status == WL_CONNECTED || status == WL_IDLE_STATUS) {
71+
if (status == WL_CONNECTED) {
7272
return;
7373
}
7474
#if defined(ARDUINO_ARCH_ESP8266)
75-
if (status == WL_DISCONNECTED) {
75+
if (status == WL_DISCONNECTED || status == WL_IDLE_STATUS ||
76+
status == WL_NO_SSID_AVAIL || status == WL_CONNECT_FAILED ||
77+
status == WL_CONNECTION_LOST) {
7678
WiFi.begin(kWiFiSsid, kWiFiPassword);
7779
}
7880
#else
81+
if (status == WL_IDLE_STATUS) {
82+
return;
83+
}
7984
WiFi.begin(kWiFiSsid, kWiFiPassword);
8085
#endif
8186
}
@@ -285,18 +290,23 @@ void reportTask() {
285290
snprintf(line, sizeof(line),
286291
"LIVE_NETMODULES window_ms=%lu sample_runs=%lu fast_avg_lag_us=%lu "
287292
"fast_max_lag_us=%lu fast_miss=%lu wifi_attempts=%lu wifi_reconnects=%lu "
288-
"http_ok=%lu http_fail=%lu http_rate=%lu mqtt_ok=%lu mqtt_fail=%lu "
289-
"mqtt_rate=%lu http_queue=%u mqtt_queue=%u",
293+
"http_connect_ok=%lu http_connect_fail=%lu http_ok=%lu http_fail=%lu "
294+
"http_rate=%lu mqtt_connect_ok=%lu mqtt_connect_fail=%lu mqtt_ok=%lu "
295+
"mqtt_fail=%lu mqtt_rate=%lu http_queue=%u mqtt_queue=%u",
290296
nowMs - g_startedAtMs,
291297
g_sampleRuns,
292298
avgLagUs,
293299
g_maxLagUs,
294300
g_fastMisses,
295301
g_wifiMaintainer.connectAttempts(),
296302
g_wifiMaintainer.reconnectTransitions(),
303+
httpMetrics.connectSuccesses,
304+
httpMetrics.connectFailures,
297305
httpMetrics.sendSuccesses,
298306
httpMetrics.sendFailures,
299307
percentage(httpMetrics.sendSuccesses, httpMetrics.sendFailures),
308+
mqttMetrics.connectSuccesses,
309+
mqttMetrics.connectFailures,
300310
mqttMetrics.sendSuccesses,
301311
mqttMetrics.sendFailures,
302312
percentage(mqttMetrics.sendSuccesses, mqttMetrics.sendFailures),
@@ -346,7 +356,7 @@ void setup() {
346356
g_wifiMaintainer.begin(ZeroKernel, isWiFiConnected, connectWiFi, disconnectWiFi, wifiConfig);
347357

348358
ZeroHttpPump::Config httpConfig;
349-
httpConfig.pollIntervalMs = 75;
359+
httpConfig.pollIntervalMs = 100;
350360
httpConfig.retryBaseMs = 500;
351361
httpConfig.retryMaxMs = 3000;
352362
httpConfig.retryJitterMs = 150;
@@ -359,7 +369,7 @@ void setup() {
359369
httpConfig);
360370

361371
ZeroMqttPump::Config mqttConfig;
362-
mqttConfig.pollIntervalMs = 75;
372+
mqttConfig.pollIntervalMs = 100;
363373
mqttConfig.retryBaseMs = 500;
364374
mqttConfig.retryMaxMs = 3000;
365375
mqttConfig.retryJitterMs = 200;
@@ -374,8 +384,8 @@ void setup() {
374384

375385
ZeroKernel.addTask("Sample", sampleTask, 100, 0);
376386
ZeroKernel.addTask("WiFiMaint", wifiMaintainerTask, 100, 0);
377-
ZeroKernel.addTask("HttpPump", httpPumpTask, 75, 0);
378-
ZeroKernel.addTask("MqttPump", mqttPumpTask, 75, 0);
387+
ZeroKernel.addTask("HttpPump", httpPumpTask, 100, 0);
388+
ZeroKernel.addTask("MqttPump", mqttPumpTask, 100, 0);
379389
ZeroKernel.addTask("Dispatch", dispatchTask, 100, 0);
380390
ZeroKernel.addTask("Report", reportTask, 250, 0);
381391

src/modules/net/ZeroHttpPump.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ZeroHttpPump {
6767
};
6868

6969
static const uint8_t kQueueCapacity = ZEROKERNEL_HTTP_PUMP_QUEUE_CAPACITY;
70-
static const uint8_t kImmediatePhaseBudget = 2;
70+
static const uint8_t kImmediatePhaseBudget = 1;
7171

7272
ZeroHttpPump()
7373
: kernel_(NULL),

tests/desktop/KernelTests.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,13 +956,20 @@ int testHttpPumpModule() {
956956
g_fakeNowMs = 2;
957957
isolatedKernel.tick();
958958
pump.tick();
959-
expectTrue(pump.phase() == zerokernel::modules::net::ZeroHttpPump::kPhaseReading,
960-
"http pump advances to read after connect and write");
961-
expectTrue(g_httpWriteCalls == 1, "http pump collapses write phase in same tick");
959+
expectTrue(pump.phase() == zerokernel::modules::net::ZeroHttpPump::kPhaseWriting,
960+
"http pump advances to write after connect");
961+
expectTrue(g_httpWriteCalls == 0, "http pump defers write until next tick");
962962

963963
g_fakeNowMs = 3;
964964
isolatedKernel.tick();
965965
pump.tick();
966+
expectTrue(g_httpWriteCalls == 1, "http pump executes write phase");
967+
expectTrue(pump.phase() == zerokernel::modules::net::ZeroHttpPump::kPhaseReading,
968+
"http pump advances to read after write");
969+
970+
g_fakeNowMs = 4;
971+
isolatedKernel.tick();
972+
pump.tick();
966973
expectTrue(g_httpReadCalls == 1, "http pump executes read phase");
967974
expectTrue(pump.phase() == zerokernel::modules::net::ZeroHttpPump::kPhaseConnecting,
968975
"http pump retries after read failure");
@@ -980,6 +987,14 @@ int testHttpPumpModule() {
980987
isolatedKernel.tick();
981988
pump.tick();
982989

990+
g_fakeNowMs = 26;
991+
isolatedKernel.tick();
992+
pump.tick();
993+
994+
g_fakeNowMs = 27;
995+
isolatedKernel.tick();
996+
pump.tick();
997+
983998
const zerokernel::modules::net::ZeroTransportMetrics::Snapshot httpMetrics =
984999
pump.metrics().snapshot();
9851000
expectTrue(!pump.isBusy(), "http pump returns idle after success");

0 commit comments

Comments
 (0)