MCPcopy
hub / github.com/grpc/grpc-go / sync

Method sync

benchmark/latency/latency.go:164–251  ·  view source on GitHub ↗

sync does a handshake and then measures the latency on the network in coordination with the other side.

()

Source from the content-addressed store, hash-verified

162// sync does a handshake and then measures the latency on the network in
163// coordination with the other side.
164func (c *conn) sync() error {
165 const (
166 pingMsg = "syncPing"
167 warmup = 10 // minimum number of iterations to measure latency
168 giveUp = 50 // maximum number of iterations to measure latency
169 accuracy = time.Millisecond // req'd accuracy to stop early
170 goodRun = 3 // stop early if latency within accuracy this many times
171 )
172
173 type syncMsg struct {
174 SendT int64 // Time sent. If zero, stop.
175 RecvT int64 // Time received. If zero, fill in and respond.
176 }
177
178 // A trivial handshake
179 if err := binary.Write(c.Conn, binary.BigEndian, []byte(pingMsg)); err != nil {
180 return err
181 }
182 var ping [8]byte
183 if err := binary.Read(c.Conn, binary.BigEndian, &ping); err != nil {
184 return err
185 } else if string(ping[:]) != pingMsg {
186 return fmt.Errorf("malformed handshake message: %v (want %q)", ping, pingMsg)
187 }
188
189 // Both sides are alive and syncing. Calculate network delay / clock skew.
190 att := 0
191 good := 0
192 var latency time.Duration
193 localDone, remoteDone := false, false
194 send := true
195 for !localDone || !remoteDone {
196 if send {
197 if err := binary.Write(c.Conn, binary.BigEndian, syncMsg{SendT: now().UnixNano()}); err != nil {
198 return err
199 }
200 att++
201 send = false
202 }
203
204 // Block until we get a syncMsg
205 m := syncMsg{}
206 if err := binary.Read(c.Conn, binary.BigEndian, &m); err != nil {
207 return err
208 }
209
210 if m.RecvT == 0 {
211 // Message initiated from other side.
212 if m.SendT == 0 {
213 remoteDone = true
214 continue
215 }
216 // Send response.
217 m.RecvT = now().UnixNano()
218 if err := binary.Write(c.Conn, binary.BigEndian, m); err != nil {
219 return err
220 }
221 continue

Callers 1

ConnMethod · 0.95

Calls 3

WriteMethod · 0.65
ReadMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected