Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
remplace int64 by time.Time and time.Duration
Browse files Browse the repository at this point in the history
Signed-off-by: Yoan Blanc <yoan.blanc@exoscale.ch>
  • Loading branch information
Yoan Blanc committed Apr 10, 2018
1 parent f3dff43 commit 3cc97ba
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 42 deletions.
4 changes: 2 additions & 2 deletions cmd/exoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ func main() {
engine.PingPeers()
engine.CheckState()

i = i + 1%100
time.Sleep(time.Duration(engine.Interval) * time.Second)
i = (i + 1) % 100
time.Sleep(engine.Interval)
}
}()

Expand Down
25 changes: 11 additions & 14 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ const DefaultPort = 12345
// ProtoVersion version of the protocol
const ProtoVersion = "0201"

// SkewMillis how much time to way
const SkewMillis = 100

// Skew how much time to way
const Skew time.Duration = 100 * time.Millisecond
// Skew how much time to wait
const Skew = 100 * time.Millisecond

// Verbose makes the client talkative
var Verbose = false
Expand Down Expand Up @@ -72,7 +69,7 @@ func NewEngineWatchdog(client *egoscale.Client, ip, instanceID string, interval
engine := &Engine{
client: client,
DeadRatio: deadRatio,
Interval: interval,
Interval: time.Duration(interval) * time.Second,
Priority: sendbuf[2],
SendBuf: sendbuf,
peers: make(map[string]*Peer),
Expand All @@ -82,7 +79,7 @@ func NewEngineWatchdog(client *egoscale.Client, ip, instanceID string, interval
ElasticIP: netip,
VirtualMachineID: instanceID,
ZoneID: zoneID,
InitHoldOff: CurrentTimeMillis() + (1000 * int64(deadRatio) * int64(interval)) + SkewMillis,
InitHoldOff: time.Now().Add(time.Duration(int64(interval)*int64(deadRatio))*time.Second + Skew),
}

for _, peerAddress := range peers {
Expand Down Expand Up @@ -157,7 +154,7 @@ func (engine *Engine) PingPeers() error {
// do not account for errors
peer.Send(engine.SendBuf)
}
engine.LastSend = CurrentTimeMillis()
engine.LastSend = time.Now()
return nil
}

Expand Down Expand Up @@ -399,17 +396,17 @@ func (engine *Engine) UpdatePeer(addr net.UDPAddr, payload *Payload) {
if peer, ok := engine.peers[addr.IP.String()]; ok {
peer.Priority = payload.Priority
peer.NicID = payload.NicID
peer.LastSeen = CurrentTimeMillis()
peer.LastSeen = time.Now()
return
}

Logger.Warning("peer not found in configuration")
}

// PeerIsNewlyDead contains the logic to say if the peer is considered dead
func (engine *Engine) PeerIsNewlyDead(now int64, peer *Peer) bool {
peerDiff := now - peer.LastSeen
dead := peerDiff > int64(engine.Interval*engine.DeadRatio)*1000
func (engine *Engine) PeerIsNewlyDead(now time.Time, peer *Peer) bool {
peerDiff := now.Sub(peer.LastSeen)
dead := peerDiff > (engine.Interval * time.Duration(engine.DeadRatio))
if dead != peer.Dead {
if dead {
Logger.Info(fmt.Sprintf("peer %s last seen %dms ago, considering dead.", peer.UDPAddr.IP, peerDiff))
Expand Down Expand Up @@ -461,9 +458,9 @@ func (engine *Engine) CheckState() {

time.Sleep(Skew)

now := CurrentTimeMillis()
now := time.Now()

if now <= engine.InitHoldOff {
if now.Before(engine.InitHoldOff) {
return
}

Expand Down
13 changes: 13 additions & 0 deletions state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package exoip

//go:generate stringer -type=State

// State represents the state : backup, master
type State int

const (
// StateBackup represents the backup state
StateBackup State = iota
// StateMaster represents the master state
StateMaster
)
10 changes: 0 additions & 10 deletions time.go

This file was deleted.

21 changes: 5 additions & 16 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/syslog"
"net"
"sync"
"time"

"github.com/exoscale/egoscale"
)
Expand All @@ -15,7 +16,7 @@ type Peer struct {
UDPAddr *net.UDPAddr
Dead bool
Priority byte
LastSeen int64
LastSeen time.Time
NicID string
conn *net.UDPConn
}
Expand All @@ -33,30 +34,18 @@ type wrappedLogger struct {
stdWriter *log.Logger
}

//go:generate stringer -type=State

// State represents the state : backup, master
type State int

const (
// StateBackup represents the backup state
StateBackup State = iota
// StateMaster represents the master state
StateMaster
)

// Engine represents the ExoIP engine structure
type Engine struct {
client *egoscale.Client
DeadRatio int
Interval int
Interval time.Duration
Priority byte
SendBuf []byte
peers map[string]*Peer
peersMu sync.RWMutex
State State
LastSend int64
InitHoldOff int64
LastSend time.Time
InitHoldOff time.Time
ElasticIP net.IP
VirtualMachineID string
SecurityGroupName string
Expand Down

0 comments on commit 3cc97ba

Please sign in to comment.