Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing a potential memory leak problem #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,17 @@ func (e *Engine) startMainLoop() {
<-e.txErr
<-e.rxErr

timeout := time.Duration(5) * time.Second
idleTimer := time.NewTimer(timeout)
defer idleTimer.Stop()
outer:
for _, ob := range e.stObservers {
for {
idleTimer.Reset(timeout)
select {
case ob <- e.state:
continue outer
case <-time.After(5 * time.Second):
case <-idleTimer.C:
log.Printf("Waited 5 seconds for state channel %v\n", ob)
}
}
Expand Down Expand Up @@ -310,11 +314,15 @@ func (e *Engine) deliverToObservers(r Reply) {
}

func (e *Engine) deliverToObserver(c chan<- Reply, r Reply) {
timeout := time.Duration(5) * time.Second
idleTimer := time.NewTimer(timeout)
defer idleTimer.Stop()
for {
idleTimer.Reset(timeout)
select {
case c <- r:
return
case <-time.After(time.Duration(5) * time.Second):
case <-idleTimer.C:
log.Printf("Waited 5 seconds for reply channel %v\n", c)
}
}
Expand Down
6 changes: 5 additions & 1 deletion engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ func getGatewayURL() string {
}

func (e *Engine) expect(t *testing.T, seconds int, ch chan Reply, expected []IncomingMessageID) (Reply, error) {
timeout := time.Duration(seconds) * time.Second
idleTimer := time.NewTimer(timeout)
defer idleTimer.Stop()
for {
idleTimer.Reset(timeout)
select {
case <-time.After(time.Duration(seconds) * time.Second):
case <-idleTimer.C:
return nil, errors.New("Timeout waiting")
case v := <-ch:
if v.code() == 0 {
Expand Down
5 changes: 4 additions & 1 deletion manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,13 @@ func (a *AbstractManager) Close() {
// require the final result of a Manager (and have no interest in each update).
// The Manager is guaranteed to be closed before it returns.
func SinkManager(m Manager, timeout time.Duration, updateStop int) (updates int, err error) {
idleTimer := time.NewTimer(timeout)
defer idleTimer.Stop()
for {
sentClose := false
idleTimer.Reset(timeout)
select {
case <-time.After(timeout):
case <-idleTimer.C:
m.Close()
return updates, fmt.Errorf("SinkManager: no new update in %s", timeout)
case _, ok := <-m.Refresh():
Expand Down