Skip to content

Commit e56f3f1

Browse files
committed
accept net module tuning
1 parent 2d15b16 commit e56f3f1

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

examples/LiveNetworkNode/LiveNetworkNode.ino

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ void reportTask() {
317317
"LIVE_NETMODULES window_ms=%lu sample_runs=%lu fast_avg_lag_us=%lu "
318318
"fast_max_lag_us=%lu fast_miss=%lu wifi_attempts=%lu wifi_reconnects=%lu "
319319
"http_connect_ok=%lu http_connect_fail=%lu http_ok=%lu http_fail=%lu "
320-
"http_rate=%lu mqtt_connect_ok=%lu mqtt_connect_fail=%lu mqtt_ok=%lu "
321-
"mqtt_fail=%lu mqtt_rate=%lu http_queue=%u mqtt_queue=%u",
320+
"http_rate=%lu http_phase_to=%lu mqtt_connect_ok=%lu mqtt_connect_fail=%lu "
321+
"mqtt_ok=%lu mqtt_fail=%lu mqtt_rate=%lu http_queue=%u mqtt_queue=%u",
322322
nowMs - g_startedAtMs,
323323
g_sampleRuns,
324324
avgLagUs,
@@ -331,6 +331,7 @@ void reportTask() {
331331
httpMetrics.sendSuccesses,
332332
httpMetrics.sendFailures,
333333
percentage(httpMetrics.sendSuccesses, httpMetrics.sendFailures),
334+
httpMetrics.phaseTimeouts,
334335
mqttMetrics.connectSuccesses,
335336
mqttMetrics.connectFailures,
336337
mqttMetrics.sendSuccesses,
@@ -380,6 +381,8 @@ void setup() {
380381
wifiConfig.retryMaxMs = 10000;
381382
wifiConfig.retryJitterMs = 300;
382383
#endif
384+
wifiConfig.stablePollMultiplier = 4;
385+
wifiConfig.stableThreshold = 6;
383386
wifiConfig.manageCapabilities = true;
384387
wifiConfig.capabilityMask = Kernel::kCapNetwork;
385388
wifiConfig.stateTopicKey = kWiFiStateTopic;
@@ -391,6 +394,11 @@ void setup() {
391394
httpConfig.retryMaxMs = 3000;
392395
httpConfig.retryJitterMs = 150;
393396
httpConfig.maxRetries = 2;
397+
#if defined(ARDUINO_ARCH_ESP8266)
398+
httpConfig.phaseTimeoutMs = 300;
399+
#else
400+
httpConfig.phaseTimeoutMs = 600;
401+
#endif
394402
g_httpPump.begin(ZeroKernel,
395403
httpConnectStep,
396404
httpWriteStep,
@@ -404,6 +412,7 @@ void setup() {
404412
mqttConfig.retryMaxMs = 3000;
405413
mqttConfig.retryJitterMs = 200;
406414
mqttConfig.maxRetries = 2;
415+
mqttConfig.idleLoopIntervalMs = 250;
407416
mqttConfig.stateTopicKey = kMqttStateTopic;
408417
g_mqttPump.begin(ZeroKernel,
409418
mqttLinkProbe,

src/modules/net/ZeroHttpPump.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class ZeroHttpPump {
5252
unsigned long retryBaseMs;
5353
unsigned long retryMaxMs;
5454
unsigned long retryJitterMs;
55+
unsigned long phaseTimeoutMs;
5556
uint8_t maxRetries;
5657
bool dropOldestOnFull;
5758
bool emitCompletionEvents;
@@ -61,6 +62,7 @@ class ZeroHttpPump {
6162
retryBaseMs(250),
6263
retryMaxMs(2000),
6364
retryJitterMs(0),
65+
phaseTimeoutMs(0),
6466
maxRetries(2),
6567
dropOldestOnFull(true),
6668
emitCompletionEvents(true) {}
@@ -182,6 +184,13 @@ class ZeroHttpPump {
182184
return;
183185
}
184186

187+
if (config_.phaseTimeoutMs > 0 && phaseStartedAtMs_ != 0 &&
188+
(nowMs - phaseStartedAtMs_) > config_.phaseTimeoutMs) {
189+
metrics_.recordPhaseTimeout();
190+
handlePhaseFailure_(nowMs);
191+
return;
192+
}
193+
185194
const StepResult result = stepCurrentPhase_();
186195
if (result == kStepPending) {
187196
return;

src/modules/net/ZeroMqttPump.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class ZeroMqttPump {
3737
unsigned long retryBaseMs;
3838
unsigned long retryMaxMs;
3939
unsigned long retryJitterMs;
40+
unsigned long idleLoopIntervalMs;
4041
uint8_t maxRetries;
4142
bool dropOldestOnFull;
4243
Kernel::TopicKey stateTopicKey;
@@ -46,6 +47,7 @@ class ZeroMqttPump {
4647
retryBaseMs(500),
4748
retryMaxMs(5000),
4849
retryJitterMs(0),
50+
idleLoopIntervalMs(0),
4951
maxRetries(2),
5052
dropOldestOnFull(true),
5153
stateTopicKey(0) {}
@@ -68,6 +70,7 @@ class ZeroMqttPump {
6870
connected_(false),
6971
lastObservedConnected_(false),
7072
lastTickAtMs_(0),
73+
lastLoopAtMs_(0),
7174
queueCount_(0),
7275
pendingRetryAtMs_(0),
7376
currentRetryMs_(0),
@@ -99,6 +102,7 @@ class ZeroMqttPump {
99102
connected_ = false;
100103
lastObservedConnected_ = false;
101104
lastTickAtMs_ = 0;
105+
lastLoopAtMs_ = 0;
102106
queueCount_ = 0;
103107
pendingRetryAtMs_ = 0;
104108
currentRetryMs_ = config_.retryBaseMs;
@@ -174,8 +178,15 @@ class ZeroMqttPump {
174178
}
175179

176180
if (loopStep_ != NULL) {
177-
loopStep_(context_);
178-
metrics_.recordLoopCall();
181+
const bool idleThrottled = queueCount_ == 0 &&
182+
config_.idleLoopIntervalMs > 0 &&
183+
lastLoopAtMs_ != 0 &&
184+
(nowMs - lastLoopAtMs_) < config_.idleLoopIntervalMs;
185+
if (!idleThrottled) {
186+
loopStep_(context_);
187+
lastLoopAtMs_ = nowMs;
188+
metrics_.recordLoopCall();
189+
}
179190
}
180191

181192
if (pendingRetryAtMs_ != 0 && nowMs < pendingRetryAtMs_) {
@@ -277,6 +288,7 @@ class ZeroMqttPump {
277288
bool connected_;
278289
bool lastObservedConnected_;
279290
unsigned long lastTickAtMs_;
291+
unsigned long lastLoopAtMs_;
280292
Message queue_[kQueueCapacity];
281293
uint8_t queueCount_;
282294
unsigned long pendingRetryAtMs_;

src/modules/net/ZeroTransportMetrics.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ZeroTransportMetrics {
2929
unsigned long lastQueueDwellMs;
3030
unsigned long worstQueueDwellMs;
3131
unsigned long maxQueueDepth;
32+
unsigned long phaseTimeouts;
3233
};
3334

3435
ZeroTransportMetrics() {
@@ -47,6 +48,7 @@ class ZeroTransportMetrics {
4748
backoffSchedules_ = 0;
4849
consecutiveFailures_ = 0;
4950
maxQueueDepth_ = 0;
51+
phaseTimeouts_ = 0;
5052

5153
#if ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
5254
lastConnectLatencyMs_ = 0;
@@ -131,6 +133,10 @@ class ZeroTransportMetrics {
131133
increment16_(backoffSchedules_);
132134
}
133135

136+
void recordPhaseTimeout() {
137+
increment16_(phaseTimeouts_);
138+
}
139+
134140
Snapshot snapshot() const {
135141
Snapshot snapshot;
136142
snapshot.connectAttempts = connectAttempts_;
@@ -144,6 +150,7 @@ class ZeroTransportMetrics {
144150
snapshot.backoffSchedules = backoffSchedules_;
145151
snapshot.consecutiveFailures = consecutiveFailures_;
146152
snapshot.maxQueueDepth = maxQueueDepth_;
153+
snapshot.phaseTimeouts = phaseTimeouts_;
147154

148155
#if ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
149156
snapshot.lastConnectLatencyMs = lastConnectLatencyMs_;
@@ -189,6 +196,7 @@ class ZeroTransportMetrics {
189196
uint16_t backoffSchedules_;
190197
uint16_t consecutiveFailures_;
191198
uint8_t maxQueueDepth_;
199+
uint16_t phaseTimeouts_;
192200

193201
#if ZEROKERNEL_ENABLE_NET_EXTENDED_METRICS
194202
unsigned long lastConnectLatencyMs_;

src/modules/net/ZeroWiFiMaintainer.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class ZeroWiFiMaintainer {
2121
unsigned long retryBaseMs;
2222
unsigned long retryMaxMs;
2323
unsigned long retryJitterMs;
24+
uint8_t stablePollMultiplier;
25+
uint8_t stableThreshold;
2426
bool emitStateChangesOnly;
2527
bool manageCapabilities;
2628
Kernel::CapabilityMask capabilityMask;
@@ -31,6 +33,8 @@ class ZeroWiFiMaintainer {
3133
retryBaseMs(1000),
3234
retryMaxMs(10000),
3335
retryJitterMs(0),
36+
stablePollMultiplier(1),
37+
stableThreshold(4),
3438
emitStateChangesOnly(true),
3539
manageCapabilities(false),
3640
capabilityMask(Kernel::kCapNetwork),
@@ -53,7 +57,8 @@ class ZeroWiFiMaintainer {
5357
currentRetryMs_(0),
5458
connectAttempts_(0),
5559
reconnectTransitions_(0),
56-
stateNotifications_(0) {}
60+
stateNotifications_(0),
61+
consecutiveStablePolls_(0) {}
5762

5863
void begin(Kernel& kernel,
5964
LinkProbe probe,
@@ -90,6 +95,7 @@ class ZeroWiFiMaintainer {
9095
connectAttempts_ = 0;
9196
reconnectTransitions_ = 0;
9297
stateNotifications_ = 0;
98+
consecutiveStablePolls_ = 0;
9399
}
94100

95101
void tick() {
@@ -98,7 +104,12 @@ class ZeroWiFiMaintainer {
98104
}
99105

100106
const unsigned long nowMs = kernel_->getStats().uptimeMs;
101-
if (hasPolled_ && (nowMs - lastPollAtMs_) < config_.pollIntervalMs) {
107+
unsigned long effectivePollMs = config_.pollIntervalMs;
108+
if (config_.stablePollMultiplier > 1 &&
109+
consecutiveStablePolls_ >= config_.stableThreshold) {
110+
effectivePollMs *= config_.stablePollMultiplier;
111+
}
112+
if (hasPolled_ && (nowMs - lastPollAtMs_) < effectivePollMs) {
102113
return;
103114
}
104115

@@ -109,6 +120,9 @@ class ZeroWiFiMaintainer {
109120
if (linkUp) {
110121
if (!connected_) {
111122
++reconnectTransitions_;
123+
consecutiveStablePolls_ = 0;
124+
} else if (consecutiveStablePolls_ < 255) {
125+
++consecutiveStablePolls_;
112126
}
113127
connected_ = true;
114128
currentRetryMs_ = config_.retryBaseMs;
@@ -120,6 +134,7 @@ class ZeroWiFiMaintainer {
120134

121135
if (connected_) {
122136
connected_ = false;
137+
consecutiveStablePolls_ = 0;
123138
if (disconnectStep_ != NULL) {
124139
disconnectStep_();
125140
}
@@ -221,6 +236,7 @@ class ZeroWiFiMaintainer {
221236
unsigned long connectAttempts_;
222237
unsigned long reconnectTransitions_;
223238
unsigned long stateNotifications_;
239+
uint8_t consecutiveStablePolls_;
224240
};
225241

226242
} // namespace net

0 commit comments

Comments
 (0)