Skip to content

[coap] optimize retransmission timer scheduling - #12366

Merged
jwhui merged 1 commit into
openthread:mainfrom
abtink:coap/retx-timer
Feb 8, 2026
Merged

[coap] optimize retransmission timer scheduling#12366
jwhui merged 1 commit into
openthread:mainfrom
abtink:coap/retx-timer

Conversation

@abtink

@abtink abtink commented Feb 4, 2026

Copy link
Copy Markdown
Member

This commit optimizes the CoAP retransmission timer logic by removing the ScheduleRetransmissionTimer() method, which iterated over all pending requests to determine the next fire time.

The logic is updated as follows:

  • HandleRetransmissionTimer() now determines the next fire time while iterating over the mPendingRequests list to process retransmissions. This avoids a redundant second pass over the list.
  • NextFireTime is used to track the earliest fire time.
  • CopyAndEnqueueMessage() uses Timer::FireAtIfEarlier() to update the timer only if the new message's fire time is earlier than the current schedule.
  • DequeueMessage() no longer triggers a schedule update. If the dequeued message was the next to expire, the timer will fire, perform no actions, and then reschedule itself.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @abtink, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors and optimizes the CoAP retransmission timer mechanism. By integrating the timer scheduling logic directly into the retransmission handling process and leveraging proactive timer updates, it reduces redundant iterations and improves the efficiency of managing pending CoAP messages. The changes also enhance code organization by encapsulating metadata-related operations within the Metadata struct, leading to a cleaner and more maintainable codebase.

Highlights

  • Retransmission Timer Optimization: The dedicated ScheduleRetransmissionTimer() method has been removed, eliminating a redundant iteration over all pending requests to determine the next retransmission time.
  • Integrated Timer Scheduling: HandleRetransmissionTimer() now efficiently determines the next retransmission fire time during its single pass over pending requests, combining processing and scheduling into one operation.
  • Proactive Timer Updates: When a new message is enqueued, the timer is updated using Timer::FireAtIfEarlier(), ensuring the timer is only rescheduled if the new message requires an earlier retransmission than currently planned.
  • Metadata Encapsulation and Refactoring: Several helper methods (Init, HasSamePeerAddrAndPort, ShouldRetransmit, UpdateRetxCounterAndTimeout, CopyInfoTo, IsObserveSubscription) have been added to the Metadata struct, centralizing CoAP message metadata logic and improving code organization.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/core/coap/coap.cpp
    • Removed the CoapBase::ScheduleRetransmissionTimer() function.
    • Modified CoapBase::HandleRetransmissionTimer() to calculate the next timer fire time during its processing loop and use mRetransmissionTimer.FireAt() at the end.
    • Updated CoapBase::CopyAndEnqueueMessage() to use mRetransmissionTimer.FireAtIfEarlier() instead of calling ScheduleRetransmissionTimer().
    • Removed the call to ScheduleRetransmissionTimer() from CoapBase::DequeueMessage().
    • Refactored CoapBase::SendMessage() to utilize the new Metadata::Init() method for initializing message metadata.
    • Modified CoapBase::ProcessObserveSend() to accept Metadata &aMetadata directly, streamlining observe-related metadata handling.
    • Removed the standalone CoapBase::IsObserveSubscription() function, as its logic was moved into the Metadata struct.
    • Added several new helper methods to the CoapBase::Metadata struct, including Init, HasSamePeerAddrAndPort, ShouldRetransmit, UpdateRetxCounterAndTimeout, CopyInfoTo, and IsObserveSubscription.
    • Renamed mNextTimerShot to mTimerFireTime, mRetransmissionTimeout to mRetxTimeout, and mRetransmissionsRemaining to mRetxRemaining within Metadata usage for clarity.
  • src/core/coap/coap.hpp
    • Removed the declaration for CoapBase::ScheduleRetransmissionTimer().
    • Updated the declaration for CoapBase::ProcessObserveSend() to reflect the new Metadata &aMetadata parameter.
    • Removed the declaration for CoapBase::IsObserveSubscription().
    • Added declarations for the new helper methods within the CoapBase::Metadata struct.
    • Updated member variable names in CoapBase::Metadata to mTimerFireTime, mRetxTimeout, and mRetxRemaining to match the implementation.
Activity
  • No specific activity (comments, reviews, etc.) was provided in the context for this pull request.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request provides a well-executed optimization for the CoAP retransmission timer logic. By removing the ScheduleRetransmissionTimer() method and integrating its logic into HandleRetransmissionTimer(), you've successfully eliminated a redundant iteration over the pending requests list. The use of Timer::FireAtIfEarlier() in CopyAndEnqueueMessage() and the removal of the timer update from DequeueMessage() are also efficient changes that contribute to the overall optimization. The related refactoring, such as introducing new methods on the Metadata struct, significantly improves code readability and encapsulation. The changes are logically sound and enhance both performance and maintainability. Great work!

@github-actions

github-actions Bot commented Feb 4, 2026

Copy link
Copy Markdown

Merging #12366 into main

name branch text data bss total
ot-cli-ftd b38881e 482176 860 66564 549600
bccb0ed 482112 860 66564 549536
+/- -64 +0 +0 -64
ot-ncp-ftd b38881e 447980 764 61800 510544
bccb0ed 447916 764 61800 510480
+/- -64 +0 +0 -64
ot-cli-mtd b38881e 373896 764 50996 425656
bccb0ed 373832 764 50996 425592
+/- -64 +0 +0 -64
ot-ncp-mtd b38881e 354956 764 46264 401984
bccb0ed 354892 764 46264 401920
+/- -64 +0 +0 -64
ot-cli-ftd-br b38881e 589696 868 136484 727048
bccb0ed 589632 868 136484 726984
+/- -64 +0 +0 -64
ot-rcp b38881e 63264 568 20804 84636
bccb0ed 63264 568 20804 84636
+/- +0 +0 +0 +0
Library files
name branch text data bss total
libopenthread-ftd.a b38881e 249439 95 40343 289877
bccb0ed 249383 95 40343 289821
+/- -56 +0 +0 -56
libopenthread-cli-ftd.a b38881e 61790 0 8091 69881
bccb0ed 61790 0 8091 69881
+/- +0 +0 +0 +0
libopenthread-ncp-ftd.a b38881e 33581 0 5948 39529
bccb0ed 33581 0 5948 39529
+/- +0 +0 +0 +0
libopenthread-mtd.a b38881e 167341 0 24807 192148
bccb0ed 167285 0 24807 192092
+/- -56 +0 +0 -56
libopenthread-cli-mtd.a b38881e 41422 0 8059 49481
bccb0ed 41422 0 8059 49481
+/- +0 +0 +0 +0
libopenthread-ncp-mtd.a b38881e 25805 0 5948 31753
bccb0ed 25805 0 5948 31753
+/- +0 +0 +0 +0
libopenthread-ftd-br.a b38881e 358609 100 110223 468932
bccb0ed 358553 100 110223 468876
+/- -56 +0 +0 -56
libopenthread-cli-ftd-br.a b38881e 80203 0 8131 88334
bccb0ed 80203 0 8131 88334
+/- +0 +0 +0 +0
libopenthread-rcp.a b38881e 9944 0 5060 15004
bccb0ed 9944 0 5060 15004
+/- +0 +0 +0 +0
libopenthread-radio.a b38881e 19537 0 238 19775
bccb0ed 19537 0 238 19775
+/- +0 +0 +0 +0

@abtink

abtink commented Feb 4, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request provides a solid optimization for the CoAP retransmission timer scheduling. By removing the separate ScheduleRetransmissionTimer() method and integrating its logic into HandleRetransmissionTimer(), a redundant iteration over the pending requests is avoided. The use of Timer::FireAtIfEarlier() when enqueuing a new message and the removal of the immediate timer reschedule from DequeueMessage() are effective optimizations that improve efficiency. The implementation is clean, correct, and the logic is sound. I approve of these changes.

This commit optimizes the CoAP retransmission timer logic by removing
the `ScheduleRetransmissionTimer()` method, which iterated over all
pending requests to determine the next fire time.

The logic is updated as follows:
- `HandleRetransmissionTimer()` now determines the next fire time
  while iterating over the `mPendingRequests` list to process
  retransmissions. This avoids a redundant second pass over the list.
- `NextFireTime` is used to track the earliest fire time.
- `CopyAndEnqueueMessage()` uses `Timer::FireAtIfEarlier()` to
  update the timer only if the new message's fire time is earlier
  than the current schedule.
- `DequeueMessage()` no longer triggers a schedule update. If the
  dequeued message was the next to expire, the timer will fire,
  perform no actions, and then reschedule itself.
@codecov

codecov Bot commented Feb 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.17%. Comparing base (b38881e) to head (5884e75).
⚠️ Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12366      +/-   ##
==========================================
+ Coverage   75.45%   77.17%   +1.72%     
==========================================
  Files         686      688       +2     
  Lines       93857    91453    -2404     
==========================================
- Hits        70817    70580     -237     
+ Misses      23040    20873    -2167     
Files with missing lines Coverage Δ
src/core/coap/coap.cpp 86.50% <100.00%> (+0.25%) ⬆️
src/core/coap/coap.hpp 90.00% <ø> (ø)

... and 254 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abtink
abtink marked this pull request as ready for review February 6, 2026 20:38
@github-project-automation github-project-automation Bot moved this to Under review in Improving CoAP/TMF Feb 6, 2026
@jwhui
jwhui merged commit 54b3573 into openthread:main Feb 8, 2026
105 checks passed
@github-project-automation github-project-automation Bot moved this from Under review to Merged in Improving CoAP/TMF Feb 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

3 participants