Skip to content
This repository has been archived by the owner on Mar 31, 2019. It is now read-only.

Commit

Permalink
adding a heartbeat to watch connection to handle a dropped connection…
Browse files Browse the repository at this point in the history
… not being picked up by the bytes reader
  • Loading branch information
zabawaba99 committed Nov 11, 2016
1 parent 3a41b87 commit dfabc3c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"log"
"net/http"
"time"
)

const (
Expand Down Expand Up @@ -165,6 +166,19 @@ func (fb *Firebase) watch(stop chan struct{}) (chan Event, error) {
resp.Body.Close()
}()

heartbeat := make(chan struct{})
go func() {
for {
select {
case <-heartbeat:
// do nothing
case <-time.After(2 * time.Minute):
resp.Body.Close()
return
}
}
}()

// start parsing response body
go func() {
defer func() {
Expand All @@ -181,6 +195,10 @@ func (fb *Firebase) watch(stop chan struct{}) (chan Event, error) {
}
}
for {
select {
case heartbeat <- struct{}{}:
default:
}
// scan for 'event:'
evt, err := readLine(scanner, "event: ")
if err != nil {
Expand Down

0 comments on commit dfabc3c

Please sign in to comment.