Rust in Action Notes Chapter 9 Time Management

  1. This chapter mainly talks about how to implement a Network Time Protocol NTP (Network Time Protocol) client. Google's world time synchronization error is about 7 milliseconds, and the open source website CockroachDB has a delay of tens of milliseconds. The NTP protocol is used. When dealing with time-sensitive data, the library has become the de facto standard library chrono;
  2. Due to the tidal influence and the earth's torque, the length of each second is not fixed in fact, leading to two time mechanisms, one is TAI, which is used in the world's atomic clock, and the length of each second is fixed; the other is UTC, which is used in ordinary life, and a leap second is added almost every 18 months. By 2016, the deviation between TAI and UTC has reached 36 seconds; two kinds of clocks are usually running in computer systems, one is called real-time clock (real-time clock), according to physical devices (such as quartz clocks) Vibration to count the time, used in the scene without power drive, one is called system time (system time), the system time is increased according to hardware interrupts, and all applications on the computer obtain the time through the system time;
  3. Section 9.3 introduces several time-related terms, such as Absolute time, Real-time clock, , system clock, monotonically increasing, steady clock, High accuracy, High resolutionetc. fast clock, which are very helpful for understanding various times;
  4. The encoding of time uses two 32-bit integers, the first represents seconds, and the second represents fractions of a second. There are two advantages, which are simple and easy to understand, and efficient in calculation. There are also two challenges. The range is fixed (there is an upper limit, because the integer width is limited), and it is inaccurate (integers are discrete rather than continuous). Several common ways to represent time are: a. b. MS Windows FILETIME (starting from Windows 2000), a 64-bit unsigned integer, representing increments (increments) of 100 nanoseconds from January 1, 1601 (UTC time) to that time; c. the chrono library of the Rust community, a 32-bit signed integer, and an enumeration type of NaiveTime to represent different time zones; d. time_t type, in the C standard library libc, different versions also have certain For the difference, see Page 298 for details;
  5. 9.5 gives the specific calling process for the application to obtain the system time, as followsFigure 9.1
  6. The method of obtaining the local time can be used chrono::Local::now(). If the return value is an exclamation mark !, it means that the function will never return, and the code unimplemented!()will panic when it runs to the macro;
  7. Use clap::Argor clap::Appto process input parameters, clap::Argwhich can help to easily process input parameters, clap::Apppackage the entire program into an application, and add requirements such as version, parameter description, and whether parameters are required;
  8. You can cargo.tomlspecify [target.'cfg(not(windows))'.dependencies]dependencies for non-windows operating systems when compiling. Similarly, remove not can be used to set dependencies for Windows operating systems;
  9. NTP has two modes, one is always on, which adopts the form of p2p to reach a stable consensus on "now" in the local area network, and the other is request/response, which obtains a centrally recognized timestamp by sending a request to the server, and calibrates it by recording the transmission time;
  10. In the request/response mode, there are 4 time points that need to be recorded. t1 indicates the timestamp when the client sends the request, t2 indicates the timestamp when the server receives the request from the client, t3 indicates the timestamp when the server sends a response, and t4 indicates the timestamp when the client receives a response. The format of these four timestamps is specified in RFC2030. The process is shown in the figure below. Section 9.9.1 provides a detailed Figure 9.2implementation .θδ \deltaδ , as shown in the table below, represents the transmission delay from the client to the server. In actual applications, requests will be sent to multiple servers to reduce the error of a single serviceTable 9.2

Guess you like

Origin blog.csdn.net/Mint2yx4/article/details/131456676