|
| 1 | +// Copyright (C) 2019-2021 Algorand, Inc. |
| 2 | +// This file is part of go-algorand |
| 3 | +// |
| 4 | +// go-algorand is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// go-algorand is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package network |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "net" |
| 22 | + "strconv" |
| 23 | + "sync/atomic" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/algorand/websocket" |
| 27 | + |
| 28 | + "github.com/algorand/go-deadlock" |
| 29 | + |
| 30 | + "github.com/algorand/go-algorand/config" |
| 31 | +) |
| 32 | + |
| 33 | +const pongMessageWriteDuration = time.Second |
| 34 | +const pingMessageWriteDuration = time.Second |
| 35 | + |
| 36 | +var errInvalidPongMessageContent = errors.New("invalid pong message content") |
| 37 | +var errInvalidPingMessageContent = errors.New("invalid ping message content") |
| 38 | + |
| 39 | +// latencyTracker works in conjunction with the wspeer in measuring the |
| 40 | +// communication latency over the websocket connection. |
| 41 | +type latencyTracker struct { |
| 42 | + // receivedPacketCounter is a counter for all incoming messages |
| 43 | + // placed here to be aligned with 64bit address. |
| 44 | + receivedPacketCounter uint64 |
| 45 | + |
| 46 | + // latency is the effective latency of the connection. |
| 47 | + // placed here to be aligned with 64bit address. |
| 48 | + latency int64 |
| 49 | + |
| 50 | + // lastPingSentTime is the timestamp at which we last sent a message. |
| 51 | + // this variable is only touched by checkPingSending, and therefore doesn't |
| 52 | + // need to be syncronized. The "clone" of this variable lastPingSentTimeSynced, |
| 53 | + // is being used by both the checkPingSending as well as by the pongHandler |
| 54 | + // and therefore require synchronization. |
| 55 | + lastPingSentTime int64 |
| 56 | + |
| 57 | + // static variables |
| 58 | + // ( doesn't get changed after init, hence, no synchronization needed ) |
| 59 | + |
| 60 | + // conn is the underlying connection object. |
| 61 | + conn wsPeerWebsocketConn |
| 62 | + |
| 63 | + // enabled indicates whether the pingpong is currently enabled or not. |
| 64 | + enabled bool |
| 65 | + |
| 66 | + // pingInterval is the max interval at which the client would send ping messages. |
| 67 | + pingInterval time.Duration |
| 68 | + |
| 69 | + // lastPingMu synchronize the protected variables that might be modified across |
| 70 | + // the checkPingSending and the pongHandler. All the variable below this point |
| 71 | + // need to be syncronized with the mutex. |
| 72 | + lastPingMu deadlock.Mutex |
| 73 | + |
| 74 | + // lastPingID is the last ping ID, a monotonic growing number used to ensure |
| 75 | + // that the pong message we've receive corresponds to the latest ping message |
| 76 | + // that we've sent. |
| 77 | + lastPingID uint64 |
| 78 | + |
| 79 | + // lastPingReceivedCounter stores message counter at the time we sent the ping. |
| 80 | + // In order to ensure the timing accuracy, we want to have no other messages |
| 81 | + // being exchanged. This, of course, would only delay the ping-pong until a |
| 82 | + // better measurement could be taken. |
| 83 | + lastPingReceivedCounter uint64 |
| 84 | + |
| 85 | + // lastPingSentTimeSynced, as stated above, is the syncronized version of lastPingSentTime. |
| 86 | + // it is used only in the case where we end up sending the ping message. |
| 87 | + lastPingSentTimeSynced int64 |
| 88 | +} |
| 89 | + |
| 90 | +func (lt *latencyTracker) init(conn wsPeerWebsocketConn, cfg config.Local, initialConnectionLatency time.Duration) { |
| 91 | + lt.conn = conn |
| 92 | + lt.enabled = cfg.PeerPingPeriodSeconds > 0 && cfg.EnablePingHandler |
| 93 | + lt.latency = int64(initialConnectionLatency) |
| 94 | + lt.pingInterval = time.Duration(cfg.PeerPingPeriodSeconds) * time.Second |
| 95 | + conn.SetPingHandler(lt.pingHandler) |
| 96 | + conn.SetPongHandler(lt.pongHandler) |
| 97 | +} |
| 98 | + |
| 99 | +func (lt *latencyTracker) pingHandler(message string) error { |
| 100 | + if _, err := strconv.Atoi(message); err != nil { |
| 101 | + return errInvalidPingMessageContent |
| 102 | + } |
| 103 | + err := lt.conn.WriteControl(websocket.PongMessage, []byte(message), time.Now().Add(pongMessageWriteDuration)) |
| 104 | + if err == websocket.ErrCloseSent { |
| 105 | + return nil |
| 106 | + } else if e, ok := err.(net.Error); ok && e.Temporary() { |
| 107 | + return nil |
| 108 | + } |
| 109 | + return err |
| 110 | +} |
| 111 | + |
| 112 | +func (lt *latencyTracker) pongHandler(message string) error { |
| 113 | + pongID, err := strconv.Atoi(message) |
| 114 | + if err != nil { |
| 115 | + return errInvalidPongMessageContent |
| 116 | + } |
| 117 | + |
| 118 | + lt.lastPingMu.Lock() |
| 119 | + defer lt.lastPingMu.Unlock() |
| 120 | + |
| 121 | + if uint64(pongID) != lt.lastPingID { |
| 122 | + // we've sent more than one ping since; ignore this message. |
| 123 | + return nil |
| 124 | + } |
| 125 | + if lt.receivedPacketCounter != lt.lastPingReceivedCounter { |
| 126 | + // we've received other messages since the one that we sent. The timing |
| 127 | + // here would not be accurate. |
| 128 | + return nil |
| 129 | + } |
| 130 | + lastPingSentTime := time.Unix(0, lt.lastPingSentTimeSynced) |
| 131 | + roundtripDuration := time.Since(lastPingSentTime) |
| 132 | + atomic.StoreInt64(<.latency, roundtripDuration.Nanoseconds()) |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func (lt *latencyTracker) getConnectionLatency() time.Duration { |
| 137 | + return time.Duration(atomic.LoadInt64(<.latency)) |
| 138 | +} |
| 139 | + |
| 140 | +func (lt *latencyTracker) checkPingSending(now *time.Time) error { |
| 141 | + if !lt.enabled { |
| 142 | + return nil |
| 143 | + } |
| 144 | + if now.Sub(time.Unix(0, lt.lastPingSentTime)) < lt.pingInterval { |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + // it looks like it's time to send a ping : |
| 149 | + lt.lastPingMu.Lock() |
| 150 | + defer lt.lastPingMu.Unlock() |
| 151 | + |
| 152 | + lt.lastPingID++ |
| 153 | + err := lt.conn.WriteControl(websocket.PingMessage, []byte(strconv.Itoa(int(lt.lastPingID))), now.Add(pingMessageWriteDuration)) |
| 154 | + if err == websocket.ErrCloseSent { |
| 155 | + return nil |
| 156 | + } else if e, ok := err.(net.Error); ok && e.Temporary() { |
| 157 | + return nil |
| 158 | + } |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + lt.lastPingSentTimeSynced = now.UnixNano() |
| 163 | + lt.lastPingReceivedCounter = atomic.LoadUint64(<.receivedPacketCounter) |
| 164 | + lt.lastPingSentTime = lt.lastPingSentTimeSynced |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +func (lt *latencyTracker) increaseReceivedCounter() { |
| 169 | + atomic.AddUint64(<.receivedPacketCounter, 1) |
| 170 | +} |
0 commit comments