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

Use CLOCK_MONOTONIC for heartbeat feature #952

Open
dofmind opened this issue Sep 27, 2024 · 0 comments
Open

Use CLOCK_MONOTONIC for heartbeat feature #952

dofmind opened this issue Sep 27, 2024 · 0 comments

Comments

@dofmind
Copy link
Contributor

dofmind commented Sep 27, 2024

Please describe what you would like to see

We get the current time using CLOCK_REALTIME. This reflects the current date and time, which can be adjusted by the system (e.g. NTP synchronization or manual change by the administrator).

uint64_t get_time_micros() {
        struct timespec now;                                                                                           
        if (clock_gettime(CLOCK_REALTIME, &now) < 0) {                                                                 
                return 0;                                                                                              
        }                                                                                                              
        uint64_t now_micros = now.tv_sec * sec_to_microsec_multiplier +                                                
                        (uint64_t) ((double) now.tv_nsec * nanosec_to_microsec_multiplier);                            
        return now_micros;                                                                                             
}

We use the heartbeat signal to compare the current timestamp with the last seen timestamp of the opposite node to determine if the connection is lost. However, if the system time is suddenly jumped by NTP or the administrator, the difference between the current timestamp and the last seen timestamp may be greater than a threshold even if the connection is not lost.

This can be avoided by using CLOCK_MONOTONIC instead of CLOCK_REALTIME.

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

No branches or pull requests

1 participant