Skip to content

Commit 9c1fc24

Browse files
committed
"Error sending" throttle
1 parent df6f684 commit 9c1fc24

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

playback/src/main/java/com/rusefi/io/can/PCanHelper.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,33 @@ public static CanSender createSender() {
6262
}
6363

6464
public static @NotNull CanSender createSender(PCANBasic pcan) {
65-
return (id, payload) -> {
66-
TPCANStatus status = send(pcan, id, payload);
65+
return new CanSender() {
66+
private long lastErrorTime = -1;
6767

68-
if (status == TPCANStatus.PCAN_ERROR_XMTFULL || status == TPCANStatus.PCAN_ERROR_QXMTFULL) {
69-
try {
70-
Thread.sleep(10);
71-
} catch (InterruptedException e) {
72-
throw new RuntimeException(e);
73-
}
68+
@Override
69+
public boolean send(int id, byte[] payload) {
70+
TPCANStatus status = PCanHelper.send(pcan, id, payload);
71+
72+
if (status == TPCANStatus.PCAN_ERROR_XMTFULL || status == TPCANStatus.PCAN_ERROR_QXMTFULL) {
73+
try {
74+
Thread.sleep(10);
75+
} catch (InterruptedException e) {
76+
throw new RuntimeException(e);
77+
}
7478
// System.out.println(String.format("Let's retry ID=%x", packet.getId()) + " OK=" + okCounter);
75-
status = send(pcan, id, payload);
76-
}
79+
status = PCanHelper.send(pcan, id, payload);
80+
}
7781

78-
boolean isHappy = status == TPCANStatus.PCAN_ERROR_OK;
79-
if (!isHappy) {
80-
System.out.println("Error sending " + status);
82+
boolean isHappy = status == TPCANStatus.PCAN_ERROR_OK;
83+
if (!isHappy) {
84+
long now = System.currentTimeMillis();
85+
if (now - lastErrorTime > 1000) {
86+
System.out.println("Error sending " + status);
87+
lastErrorTime = now;
88+
}
89+
}
90+
return isHappy;
8191
}
82-
return isHappy;
8392
};
8493
}
8594
}

0 commit comments

Comments
 (0)