Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make update() non-blocking. #22

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

blackketter
Copy link

When the network is unreliable or the NTP server is unavailable, the update() function can block for a substantial amount of time. This patch separates update request and response processing and allows for a single outstanding request. Requests are retransmitted every second if there's no response.

ForceUpdate() is still available and can still block.

@FWeinb
Copy link
Collaborator

FWeinb commented Oct 11, 2016

I really like this approach! Thanks for this contribution.

@sandeepmistry Any objections?

@sandeepmistry
Copy link
Contributor

I like the concept, however is this a breaking changing in behaviour?

If so, maybe we can add a new API for non-blocking behaviour.

@blackketter
Copy link
Author

Under identical network conditions the behavior may change a bit. (i.e. A single call to update() on a slow network may not update the time.) If the use of update() is to call it multiple times (i.e. in loop() ) and that the application expects that it sometimes fails to update, then it should be fine.

@FWeinb
Copy link
Collaborator

FWeinb commented Oct 13, 2016

Adding an API for the current time in a blocking way would be great to ensure that the time was synced at least once on startup. Otherwise I think that the behavior change is not that bad.

@FWeinb FWeinb closed this Oct 13, 2016
@FWeinb FWeinb reopened this Oct 13, 2016
@blackketter
Copy link
Author

forceUpdate() continues to behave with the with the same blocking behavior in my patch. Maybe that's what should be used?

Keep in mind the original behavior of update() was to block for only one second so on unreliable or slow networks it will continue to fail.

@sandeepmistry
Copy link
Contributor

Thanks for the clarification. The suggested changes sound good to me.

Accuracy is improved by using the fractional portion of seconds.
Make millisecond accurate time available via new getEpochMillis()
function.
@blackketter
Copy link
Author

Those last three actually support #23 with a new method:

unsigned long long getEpochMillis();

@blackketter
Copy link
Author

Any chance this request will be accepted?

@sandeepmistry
Copy link
Contributor

@blackketter up to commit b68746a was good for me. Not sure why that wasn't merged then.

@FWeinb what do you think of the newer commits?

@NdK73
Copy link

NdK73 commented Dec 28, 2017

Will this PR be accepted and included?
Async updates are always very useful, IMVHO. And making 'em synchronous is quite easy :)

@corneyl
Copy link

corneyl commented Jan 16, 2018

@blackketter When I use your fork, i often get a crash of the program. I've tried to decode the stacktrace, but it doesn't show line numbers:

Exception Cause: 0  [Illegal instruction]

0x4021099d: NTPClient::checkResponse() at ??:?
0x40210b08: NTPClient::update() at ??:?
0x4020d58d: Clock::update() at ??:?
0x402113b8: PubSubClient::connected() at ??:?
0x40211635: loop at ??:?
0x402020ad: esp_schedule at ??:?
0x402020d8: loop_wrapper() at core_esp8266_main.cpp:?
0x40203fec: cont_norm at cont.o:?

The exception is in checkResponse(), any clue what this can be?

Edit: I also got this exception:

Exception Cause: 2  [InstructionFetchError: Processor internal physical address or data error during instruction fetch]

0x4021099d: NTPClient::checkResponse() at ??:?
0x40210b08: NTPClient::update() at ??:?
0x4020d58d: Clock::update() at ??:?
0x40211634: loop at ??:?
0x402020ad: esp_schedule at ??:?
0x402020d8: loop_wrapper() at core_esp8266_main.cpp:?
0x40203fec: cont_norm at cont.o:?

@blackketter
Copy link
Author

blackketter commented Jan 16, 2018 via email

@corneyl
Copy link

corneyl commented Jan 16, 2018

I think I found it: _updateCallback is not initialised.
I do not set a callback, but if I add a print statement to the callback check like this:

if (_updateCallback) {
   Serial.println("Calling callback...");
   _updateCallback(this);
}

it actually prints Calling callback... just before the stacktrace.

If I initialise _updateCallback like this: NTPUpdateCallbackFunction _updateCallback = NULL;, I do not get the exception.

@blackketter
Copy link
Author

blackketter commented Jan 16, 2018 via email

@connornishijima
Copy link

Is this library still being maintained?

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants