Skip to content

Commit

Permalink
Support custom NTP query retry times and timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
bclswl0827 committed Aug 28, 2024
1 parent 160acf1 commit 659cecb
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 17 deletions.
8 changes: 5 additions & 3 deletions build/assets/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
},
"ntpclient_settings": {
"host": "pool.ntp.org",
"port": 123
"port": 123,
"timeout": 5,
"retry": 5
},
"database_settings": {
"engine": "postgresql",
Expand All @@ -51,9 +53,9 @@
},
"logger_settings": {
"level": "info",
"rotation": 7,
"rotation": 5,
"lifecycle": 3,
"size": 5,
"size": 0,
"dump": "/home/yuki/observer.log"
},
"services_settings": {
Expand Down
7 changes: 6 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ func main() {

// Create time source with NTP server
logger.GetLogger(main).Infof("querying NTP server at %s:%d", conf.NtpClient.Host, conf.NtpClient.Port)
timeSource, err := timesource.New(conf.NtpClient.Host, conf.NtpClient.Port, 5, 5*time.Second)
timeSource, err := timesource.New(
conf.NtpClient.Host,
conf.NtpClient.Port,
conf.NtpClient.Retry,
time.Duration(conf.NtpClient.Timeout)*time.Second,
)
if err != nil {
logger.GetLogger(main).Fatalln(err)
}
Expand Down
6 changes: 4 additions & 2 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ type Stream struct {
}

type ntpclient struct {
Host string `json:"host"`
Port int `json:"port" validate:"min=1,max=65535"`
Host string `json:"host"`
Port int `json:"port" validate:"min=1,max=65535"`
Timeout int `json:"timeout" validate:"gte=0"`
Retry int `json:"retry" validate:"gte=0"`
}

type database struct {
Expand Down
9 changes: 3 additions & 6 deletions services/forwarder/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package forwarder

import "unsafe"

func (a *ForwarderService) getChecksum(arr []int32) uint8 {
checksum := uint8(0)

for i := 0; i < len(arr); i++ {
bytes := (*[4]byte)(unsafe.Pointer(&arr[i]))[:]

func (a *ForwarderService) getChecksum(arr []int32) (checksum uint8) {
for _, data := range arr {
bytes := (*[4]byte)(unsafe.Pointer(&data))[:]
for j := 0; j < int(unsafe.Sizeof(int32(0))); j++ {
checksum ^= bytes[j]
}
Expand Down
4 changes: 4 additions & 0 deletions services/miniseed/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func (m *MiniSeedService) handleClean() error {
if m.lifeCycle == 0 {
return nil
}

expiredFiles := []string{}
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions utils/timesource/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/beevik/ntp"
)

func New(ntpHost string, ntpPort, attempts int, timeout time.Duration) (Source, error) {
for i := 0; i < attempts; i++ {
func New(ntpHost string, ntpPort, retries int, timeout time.Duration) (Source, error) {
for i := 0; i <= retries; i++ {
res, err := ntp.QueryWithOptions(ntpHost, ntp.QueryOptions{
Port: ntpPort, Timeout: timeout,
})
Expand All @@ -18,7 +18,7 @@ func New(ntpHost string, ntpPort, attempts int, timeout time.Duration) (Source,
return Source{
ntpHost: ntpHost,
ntpPort: ntpPort,
queryAttempts: attempts,
queryRetries: retries,
queryTimeout: timeout,
LocalBaseTime: time.Now().UTC(),
ReferenceTime: res.Time,
Expand Down
2 changes: 1 addition & 1 deletion utils/timesource/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Source struct {
rwMutex sync.RWMutex
ntpHost string
ntpPort int
queryAttempts int
queryRetries int
queryTimeout time.Duration
LocalBaseTime time.Time
ReferenceTime time.Time
Expand Down
2 changes: 1 addition & 1 deletion utils/timesource/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (s *Source) Update() error {
s.rwMutex.Lock()
defer s.rwMutex.Unlock()

for i := 0; i < s.queryAttempts; i++ {
for i := 0; i <= s.queryRetries; i++ {
res, err := ntp.QueryWithOptions(s.ntpHost, ntp.QueryOptions{
Port: s.ntpPort, Timeout: s.queryTimeout,
})
Expand Down

0 comments on commit 659cecb

Please sign in to comment.