-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lu Ken <[email protected]>
- Loading branch information
1 parent
d5e964a
commit 87388b4
Showing
5 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
fastapi[standard] | ||
uvicorn | ||
uvicorn | ||
pydantic | ||
pydantic-settings | ||
ntplib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
""" | ||
Configure | ||
""" | ||
from pydantic_settings import BaseSettings | ||
|
||
class Settings(BaseSettings): | ||
ntp_server: str = "ntp.ntsc.ac.cn" | ||
|
||
settings = Settings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Utils | ||
""" | ||
import logging | ||
import ntplib | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
def sync_ntp_server() -> float: | ||
""" | ||
Sync with NTP server | ||
:return : offset in seconds | ||
""" | ||
ntp_servers = ['ntp.ntsc.ac.cn', 'ntp.sjtu.edu.cn', 'cn.ntp.org.cn', | ||
'cn.pool.ntp.org', 'ntp.aliyun.com'] | ||
retry = len(ntp_servers) - 1 | ||
client = ntplib.NTPClient() | ||
while retry > 0: | ||
LOG.info("Try to get time from NTP: %s", ntp_servers[retry]) | ||
|
||
try: | ||
ret = client.request(ntp_servers[retry], version=3) | ||
offset = (ret.recv_time - ret.orig_time + | ||
ret.dest_time - ret.tx_time) / 2 | ||
LOG.info("NTP offset: %.2f", offset) | ||
return offset | ||
except ntplib.NTPException: | ||
LOG.error("Fail to get time, try another") | ||
retry -= 1 | ||
continue | ||
return None |