-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63c061d
commit ad15e4a
Showing
8 changed files
with
124 additions
and
73 deletions.
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
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
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 |
---|---|---|
@@ -1,62 +1,109 @@ | ||
package ntp | ||
|
||
import ( | ||
"context" | ||
"math" | ||
"sync" | ||
"time" | ||
|
||
"github.com/beevik/ntp" | ||
"github.com/pactus-project/pactus/util/errors" | ||
"github.com/pactus-project/pactus/util/logger" | ||
) | ||
|
||
type Server struct { | ||
threshold time.Duration | ||
logger *logger.SubLogger | ||
serverList []string | ||
} | ||
|
||
func NewNtpServer() *Server { | ||
server := &Server{ | ||
threshold: 1 * time.Second, | ||
serverList: []string{ | ||
"0.pool.ntp.org", | ||
"1.pool.ntp.org", | ||
"2.pool.ntp.org", | ||
"3.pool.ntp.org", | ||
}, | ||
} | ||
const ( | ||
maxClockOffset = time.Duration(math.MinInt64) | ||
) | ||
|
||
type Checker struct { | ||
lock sync.RWMutex | ||
ctx context.Context | ||
cancel func() | ||
|
||
ticker *time.Ticker | ||
threshold time.Duration | ||
offset time.Duration | ||
interval time.Duration | ||
} | ||
|
||
server.logger = logger.NewSubLogger("_ntp", server) | ||
func NewNtpChecker(interval, threshold time.Duration) *Checker { | ||
ctxWithCancel, cancel := context.WithCancel(context.Background()) | ||
server := &Checker{ | ||
ctx: ctxWithCancel, | ||
cancel: cancel, | ||
interval: interval, | ||
threshold: threshold, | ||
ticker: time.NewTicker(interval), | ||
} | ||
|
||
return server | ||
} | ||
|
||
func (s *Server) ClockOffset() time.Duration { | ||
clockOffset := time.Duration(math.MinInt64) | ||
func (*Checker) clockOffset() time.Duration { | ||
server := "pool.ntp.org" | ||
response, err := ntp.Query(server) | ||
if err != nil { | ||
logger.Error("ntp error", "server", server, "error", err) | ||
|
||
for _, server := range s.serverList { | ||
remoteTime, err := ntp.Time(server) | ||
if err != nil { | ||
s.logger.Debug("ntp error", "server", server, "error", err) | ||
return maxClockOffset | ||
} | ||
|
||
continue | ||
} | ||
if err := response.Validate(); err != nil { | ||
logger.Error("ntp error", "server", server, "error", err) | ||
|
||
clockOffset = time.Since(remoteTime) | ||
return maxClockOffset | ||
} | ||
|
||
break | ||
return response.ClockOffset | ||
} | ||
|
||
func (c *Checker) checkClockOffset() { | ||
for { | ||
select { | ||
case <-c.ctx.Done(): | ||
return | ||
|
||
case <-c.ticker.C: | ||
offset := c.clockOffset() | ||
c.lock.Lock() | ||
c.offset = offset | ||
c.lock.Unlock() | ||
|
||
if c.offset == maxClockOffset { | ||
logger.Error("error on getting clock offset") | ||
} else if c.OutOfSync(offset) { | ||
logger.Error( | ||
"The node is out of sync with the network time", | ||
"threshold", c.threshold, | ||
"offset", offset, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return clockOffset | ||
func (c *Checker) OutOfSync(offset time.Duration) bool { | ||
return math.Abs(float64(offset)) > float64(c.threshold) | ||
} | ||
|
||
func (s *Server) String() string { | ||
return "ntp" | ||
func (c *Checker) Start() { | ||
go c.checkClockOffset() | ||
} | ||
|
||
func (s *Server) GetThreshold() time.Duration { | ||
return s.threshold | ||
func (c *Checker) Stop() { | ||
c.cancel() | ||
c.ticker.Stop() | ||
} | ||
|
||
func (s *Server) OutOfSync(offset time.Duration) bool { | ||
return math.Abs(float64(offset)) > float64(s.threshold) | ||
func (c *Checker) GetClockOffset() (time.Duration, error) { | ||
c.lock.RLock() | ||
defer c.lock.RUnlock() | ||
|
||
offset := c.offset | ||
|
||
if offset == maxClockOffset { | ||
return 0, errors.Errorf(errors.ErrNtpError, "unable to get clock offset") | ||
} | ||
|
||
return offset, nil | ||
} |
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