remove context (did not work)
@@ -2,7 +2,6 @@ package checkntpoffset
import (
"bufio"
- "context"
"fmt"
"io"
"math"
@@ -117,9 +116,27 @@ func detectNTPDname() (ntpdName string, err error) {
func getNTPOffset(ntpServers string) (float64, error) {
if ntpServers != "" {
- ctx := context.Background()
- ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
- defer cancel()
+ return getNTPOffsetFromNTPServers(ntpServers)
+ }
+
+ ntpdName, err := detectNTPDname()
+ if err != nil {
+ return 0.0, err
+ }
+ switch ntpdName {
+ case ntpNTPD:
+ return getNTPOffsetFromNTPD()
+ case ntpChronyd:
+ return getNTPOffsetFromChrony()
+ }
+ return 0.0, fmt.Errorf("unsupported ntp daemon %q", ntpdName)
+}
+
+// getNTPOffsetFromNTPServers ask time to ntp servers and return NTP Offset.
+// Use first response, ignore others
+//
+// FIXME need fluent cancel mechanism
+func getNTPOffsetFromNTPServers(ntpServers string) (offset float64, err error) {
resultChan := make(chan float64)
for _, ntpServer := range strings.Split(ntpServers, ",") {
go func(ntpServer string) error {
@@ -135,25 +152,11 @@ func getNTPOffset(ntpServers string) (float64, error) {
}
select {
- case <-ctx.Done():
+ case <-time.After(time.Duration(ntpTimeout) * time.Second):
return 0.0, fmt.Errorf("NTP offset cannot get from %q", ntpServers)
- case offset := <-resultChan:
+ case offset = <-resultChan:
return offset, nil
}
-
- }
-
- ntpdName, err := detectNTPDname()
- if err != nil {
- return 0.0, err
- }
- switch ntpdName {
- case ntpNTPD:
- return getNTPOffsetFromNTPD()
- case ntpChronyd:
- return getNTPOffsetFromChrony()
- }
- return 0.0, fmt.Errorf("unsupported ntp daemon %q", ntpdName)
}
func getNTPOffsetFromNTPD() (offset float64, err error) {
