ping and pong
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m53s
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m53s
This commit is contained in:
@@ -162,6 +162,8 @@ const (
|
||||
RemoteGrainChanged = uint16(4)
|
||||
AckChange = uint16(5)
|
||||
AckError = uint16(6)
|
||||
Ping = uint16(7)
|
||||
Pong = uint16(8)
|
||||
)
|
||||
|
||||
func (p *SyncedPool) handleConnection(conn net.Conn) {
|
||||
@@ -180,6 +182,13 @@ func (p *SyncedPool) handleConnection(conn net.Conn) {
|
||||
// return
|
||||
// }
|
||||
switch packet.MessageType {
|
||||
case Ping:
|
||||
err = SendPacket(conn, Pong, func(w io.Writer) error {
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error sending pong: %v\n", err)
|
||||
}
|
||||
case RemoteNegotiate:
|
||||
negotiationCount.Inc()
|
||||
data := make([]byte, packet.DataLength)
|
||||
@@ -318,6 +327,20 @@ func (p *SyncedPool) AddRemoteWithConnection(address string, connection net.Conn
|
||||
return p.addRemoteHost(address, remote)
|
||||
}
|
||||
|
||||
func DoPing(connection net.Conn) error {
|
||||
SendPacket(connection, Ping, func(w io.Writer) error {
|
||||
return nil
|
||||
})
|
||||
t, _, err := ReceivePacket(connection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if t != Pong {
|
||||
return fmt.Errorf("unexpected message type %d", t)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SyncedPool) addRemoteHost(address string, remote RemoteHost) error {
|
||||
for _, r := range p.remotes {
|
||||
if r.Host == address {
|
||||
@@ -326,6 +349,11 @@ func (p *SyncedPool) addRemoteHost(address string, remote RemoteHost) error {
|
||||
}
|
||||
}
|
||||
|
||||
err := DoPing(remote.connection)
|
||||
if err != nil {
|
||||
log.Printf("Error pinging remote %s: %v\n", address, err)
|
||||
}
|
||||
|
||||
p.remotes = append(p.remotes, remote)
|
||||
connectedRemotes.Set(float64(len(p.remotes)))
|
||||
log.Printf("Added remote %s\n", remote.Host)
|
||||
|
||||
Reference in New Issue
Block a user