Skip to content

net/ntptime/ntptime.py: Allow overriding default NTP timeout. #554

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

Closed
wants to merge 1 commit into from

Conversation

iabdalkader
Copy link
Contributor

  • The default 1 second timeout is sometimes not enough. Allow overriding the default socket timeout by passing a timeout arg.

@dpgeorge
Copy link
Member

There's also settime() which calls time(), and probably should also allow passing in the timeout.

But then maybe a better way to do it is have a global variable to set the timeout. This will match how you select the NTP host.

Eg:

host = "pool.ntp.org"
timeout = 1

...

    s.settimeout(timeout)

@iabdalkader
Copy link
Contributor Author

I think the host should be private and the default ex: _HOST and a default timeout _TIMEOUT=const(1.0) , and both functions accept kwordargs settime(timeout=_DEFAULT_TIMEOUT, host=_DEFAULT_HOST)

@iabdalkader iabdalkader force-pushed the ntp_timeout branch 2 times, most recently from bcff017 to 4aadcff Compare October 25, 2022 06:35
@iabdalkader
Copy link
Contributor Author

@dpgeorge How about now ?

@iabdalkader iabdalkader changed the title net/ntptime/ntptime.py: Allow passing socket timeout to ntp time. net/ntptime/ntptime.py: Allow overriding default NTP host and timeout. Oct 25, 2022
@dpgeorge
Copy link
Member

But this changes the existing public API, where you change ntptime.host to configure your desired server. We had a lot of PRs to change this behaviour already and that's why there's a comment there describing how to use it.

The idea behind having a global variable is that you can change it once -- configure it -- then you don't need to pass it in to time() every time you call it. IMO setting it once to a set value is a more common use case that passing in potentially different hosts at different locations where this is called.

(Note that it's not such bad practice having a global here, because the ntptime itself is very small and self contained, you can think of ntptime as being a singleton class, and ntptime.host as being a property that you get/set.)

@iabdalkader iabdalkader changed the title net/ntptime/ntptime.py: Allow overriding default NTP host and timeout. net/ntptime/ntptime.py: Allow overriding default NTP timeout. Oct 25, 2022
@iabdalkader
Copy link
Contributor Author

Okay, I see your point, it's not important as long as we can override it somehow. Added a timeout globally just like host

* The default 1 second timeout is sometimes not enough depending on
the host and network latencies. This patch makes timeout configurable.
@dpgeorge
Copy link
Member

Thanks for updating. Merged in 900dd1c (and I change the default from "1.0" to "1" so it doesn't use a float if it doesn't need to).

@dpgeorge dpgeorge closed this Oct 25, 2022
@iabdalkader iabdalkader deleted the ntp_timeout branch October 25, 2022 13:09
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.

2 participants