Skip to content

Commit

Permalink
Resolve race condition when calculating RTT
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton authored Sep 17, 2019
1 parent 1983bc2 commit f160bc1
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"net"
"sync"
"time"

"golang.org/x/net/icmp"
Expand All @@ -23,9 +22,8 @@ const (

// A Request represents an icmp echo request to be sent by a client.
type Request struct {
reqTex *sync.Mutex // request lock
sentAt time.Time // time at which echo request was sent
dst net.Addr // useable destination address
sentAt time.Time // time at which echo request was sent
dst net.Addr // useable destination address

ID int // ID is the ICMP ID. It is an identifier to aid in matching echos and replies when using privileged datagrams, may be zero.
Seq int // Seq is the ICMP sequence number.
Expand Down Expand Up @@ -86,8 +84,6 @@ func Do(ctx context.Context, req *Request) (*Response, error) {

// Do sends a ping request and returns a ping response.
func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
reqTex := &sync.Mutex{}

conn, network, err := c.listen(req)
if err != nil {
return nil, err
Expand Down Expand Up @@ -115,33 +111,22 @@ func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
var (
resp *Response
readErr error

wg = &sync.WaitGroup{}
)

wg.Add(1)
go func() {
defer wg.Done()
resp, readErr = read(ctx, conn)
if readErr != nil {
return
}

reqTex.Lock()
resp.RTT = resp.rcvdAt.Sub(req.sentAt)
reqTex.Unlock()
resp.Req = req
}()

sentAt, err := send(ctx, conn, req)
if err != nil {
return nil, err
}
reqTex.Lock()

resp, readErr = read(ctx, conn)
if readErr != nil {
return nil, readErr
}

resp.RTT = resp.rcvdAt.Sub(sentAt)
req.sentAt = sentAt
reqTex.Unlock()
resp.Req = req

wg.Wait()
if readErr != nil {
return nil, readErr
}
Expand Down

0 comments on commit f160bc1

Please sign in to comment.