Stabilize coroutine execution in ObdDeviceConnection#34
Conversation
Greptile SummaryRefactored Key improvements:
Issues found:
Confidence Score: 4/5
Important Files Changed
Last reviewed commit: 77df86f |
src/test/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnectionTest.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnection.kt
Outdated
Show resolved
Hide resolved
Greptile SummaryRefactored Key improvements:
Issues found:
Confidence Score: 4/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Client calls run command] --> B{Acquire Mutex Lock}
B --> C{Check Cache}
C -->|Cache Hit & useCache=true| D[Return Cached Response]
C -->|Cache Miss or useCache=false| E[runCommand]
E --> F[sendCommand: Write to OutputStream]
F --> G{delayTime > 0?}
G -->|Yes| H[delay delayTime]
G -->|No| I[readRawData]
H --> I
I --> J{inputStream.available > 0?}
J -->|Yes| K{Read byte}
K -->|byte == -1| L[EOF: Break]
K -->|byte == '>'| M[End: Break]
K -->|Other| N[Append to result]
N --> J
J -->|No| O{retriesCount <= maxRetries?}
O -->|Yes| P[Increment retries, delay 500ms]
P --> J
O -->|No| Q[Return result]
L --> Q
M --> Q
Q --> R{useCache=true?}
R -->|Yes| S[Store in Cache with tag:rawCommand key]
R -->|No| T[handleResponse]
S --> T
D --> U[Release Mutex Lock]
T --> U
U --> V[Return ObdResponse]
Last reviewed commit: fd19141 |
src/main/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnection.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnection.kt
Show resolved
Hide resolved
src/test/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnectionTest.kt
Outdated
Show resolved
Hide resolved
Greptile SummaryThis PR stabilizes coroutine execution in Key improvements:
Tests added for concurrent execution, cache safety, EOF handling, and cancellation propagation. Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant C1 as Coroutine 1
participant C2 as Coroutine 2
participant M as Mutex
participant Conn as ObdDeviceConnection
participant IO as InputStream/OutputStream
C1->>Conn: run(SpeedCommand)
Conn->>M: withLock
M-->>Conn: lock acquired
Conn->>IO: write("01 0D\r")
IO-->>Conn: flush
Conn->>Conn: delay(delayTime)
Conn->>IO: read until '>' or EOF
Note over C2,M: C2 attempts concurrent access
C2->>Conn: run(RPMCommand)
Conn->>M: withLock (waiting)
Note over M: Mutex blocks C2 until C1 completes
IO-->>Conn: "410D40>"
Conn-->>M: release lock
M-->>C1: ObdResponse
M-->>Conn: lock acquired
Conn->>IO: write("01 0C\r")
IO-->>Conn: flush
Conn->>Conn: delay(delayTime)
Conn->>IO: read until '>' or EOF
IO-->>Conn: "410C1AF8>"
Conn-->>M: release lock
M-->>C2: ObdResponse
Last reviewed commit: 623cfac |
src/main/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnection.kt
Outdated
Show resolved
Hide resolved
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Greptile SummaryThis PR successfully stabilizes coroutine execution in Key improvements:
Minor issue found: the retry loop condition uses Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant C1 as Coroutine 1
participant C2 as Coroutine 2
participant M as Mutex
participant ODC as ObdDeviceConnection
participant Stream as Input/OutputStream
C1->>+ODC: run(speedCommand)
ODC->>+M: withLock
M-->>ODC: lock acquired
ODC->>ODC: check cache (miss)
ODC->>Stream: write("01 0D\r")
ODC->>Stream: flush()
C2->>+ODC: run(rpmCommand)
ODC->>M: withLock
Note over M: C2 blocked waiting for mutex
ODC->>Stream: read until '>' or EOF
Stream-->>ODC: "410D40>"
ODC-->>M: release lock
ODC-->>-C1: ObdResponse
M-->>ODC: lock acquired
ODC->>ODC: check cache (miss)
ODC->>Stream: write("01 0C\r")
ODC->>Stream: flush()
ODC->>Stream: read until '>' or EOF
Stream-->>ODC: "410C1AF8>"
ODC-->>M: release lock
ODC-->>-C2: ObdResponse
Last reviewed commit: 8fe7a9a |
src/main/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnection.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnection.kt
Outdated
Show resolved
Hide resolved
src/test/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnectionTest.kt
Outdated
Show resolved
Hide resolved
src/test/kotlin/com/github/eltonvs/obd/connection/ObdDeviceConnectionTest.kt
Show resolved
Hide resolved
Additional Comments (1)
then handle in try-catch or let exception propagate (preferred) |
Summary
runBlockingfrom suspend paths inObdDeviceConnectionMutexto prevent stream interleavingInputStream.read()for-1Tests
./gradlew testpassesDocs