Skip to content

Commit

Permalink
improve random data generation, add nil assignments for conn objects …
Browse files Browse the repository at this point in the history
…for good measure
  • Loading branch information
c3mb0 committed Dec 5, 2016
1 parent a877f6a commit f0f0e42
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ func (c *Client) EnablePinging(willHandleDrops bool) (<-chan struct{}, bool) {
}
go func() {
// 8 bytes of random data used for PING-PONG, as per HTTP/2 spec.
data := [8]byte{byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256)), byte(rand.Intn(256))}
var data [8]byte
rand.Read(data[:])
pinger := new(time.Ticker)
var framer *http2.Framer
c.m.Lock()
Expand All @@ -180,12 +181,12 @@ func (c *Client) EnablePinging(willHandleDrops bool) (<-chan struct{}, bool) {
case <-pinger.C:
err := framer.WritePing(false, data)
if err != nil {
// APNs did not answer with pong, which means the connection
// has been dropped. Stop trying and notify the drop handler,
// if there is any.
// Could not PING the APNs server, stop trying
// and notify the drop handler, if there is any.
c.m.Lock()
c.conn = nil
c.m.Unlock()
framer = nil
pinger.Stop()
if willHandleDrops {
dropSignal <- struct{}{}
Expand All @@ -199,6 +200,10 @@ func (c *Client) EnablePinging(willHandleDrops bool) (<-chan struct{}, bool) {
pinger = time.NewTicker(PingPongFrequency)
case <-c.stopChan:
pinger.Stop()
c.m.Lock()
defer c.m.Unlock()
c.conn = nil
framer = nil
return
}
}
Expand Down

0 comments on commit f0f0e42

Please sign in to comment.