Skip to content

Commit

Permalink
Bug fix: remove race condition for startTime
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Nov 5, 2017
1 parent d105c25 commit 152c27a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func (c *Connection) runClient() error {
"sender?": c.IsSender,
})

startTime := time.Now()
c.HashedCode = Hash(c.Code)

var wg sync.WaitGroup
Expand All @@ -227,10 +226,14 @@ func (c *Connection) runClient() error {
gotResponse bool
gotConnectionInUse bool
notPresent bool
startTime time.Time
sync.RWMutex
}

responses := new(responsesStruct)
responses.Lock()
responses.startTime = time.Now()
responses.Unlock()
for id := 0; id < c.NumberOfConnections; id++ {
go func(id int) {
defer wg.Done()
Expand Down Expand Up @@ -292,7 +295,9 @@ func (c *Connection) runClient() error {
time.Sleep(100 * time.Millisecond)
// Write data from file
logger.Debug("send file")
startTime = time.Now()
responses.Lock()
responses.startTime = time.Now()
responses.Unlock()
c.bar.Reset()
if err := c.sendFile(id, connection); err != nil {
log.Error(err)
Expand Down Expand Up @@ -387,7 +392,9 @@ func (c *Connection) runClient() error {
if id == 0 {
fmt.Printf("\n\nReceiving (<-%s)..\n", sendersAddress)
}
startTime = time.Now()
responses.Lock()
responses.startTime = time.Now()
responses.Unlock()
c.bar.SetMax(c.File.Size)
c.bar.Reset()
if err := c.receiveFile(id, connection); err != nil {
Expand All @@ -406,6 +413,8 @@ func (c *Connection) runClient() error {
return nil // connection was in use, just quit cleanly
}

timeSinceStart := time.Since(responses.startTime) / time.Second

if c.IsSender {
if responses.gotTimeout {
fmt.Println("Timeout waiting for receiver")
Expand Down Expand Up @@ -471,7 +480,6 @@ func (c *Connection) runClient() error {
fmt.Printf("\nReceived file written to %s", path.Join(c.Path, c.File.Name))
}
}
timeSinceStart := time.Since(startTime) / time.Second
fmt.Printf(" (%s/s)\n", humanize.Bytes(uint64(float64(c.File.Size)/float64(timeSinceStart))))
return nil
}
Expand Down

0 comments on commit 152c27a

Please sign in to comment.