Skip to content

Commit

Permalink
Merge pull request #1 from couchbaselabs/master
Browse files Browse the repository at this point in the history
Fixed a couple of goroutine leaks.
  • Loading branch information
igm committed Mar 13, 2013
2 parents ca5c254 + 5c834d5 commit a6a65a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
5 changes: 4 additions & 1 deletion sockjs/conn-utils.go
Expand Up @@ -27,6 +27,9 @@ func hijack(rw http.ResponseWriter) (conn net.Conn, err error) {
func connectionClosedGuard(conn net.Conn, conn_interrupted chan<- bool) {
_, err := conn.Read([]byte{1})
if err != nil {
conn_interrupted <- true
select {
case conn_interrupted <- true:
default:
}
}
}
4 changes: 2 additions & 2 deletions sockjs/handler.go
Expand Up @@ -115,11 +115,11 @@ func activeConnectionState(c *conn) connectionStateFn {
}()

// start protocol handling
conn_closed := make(chan bool)
conn_closed := make(chan bool, 1)
defer func() { conn_closed <- true }()
go c.activeConnectionGuard(conn_closed)

conn_interrupted := make(chan bool)
conn_interrupted := make(chan bool, 1)
go connectionClosedGuard(conn, conn_interrupted)

bytes_sent := 0
Expand Down
18 changes: 9 additions & 9 deletions sockjs/types.go
Expand Up @@ -7,6 +7,7 @@ Cotains package internal types (not public)
import (
"errors"
"io"
"time"
)

// Error variable
Expand All @@ -22,14 +23,16 @@ type conn struct {
context
input_channel chan []byte
output_channel chan []byte
timeout time.Duration
httpTransactions chan *httpTransaction
}

func newConn(ctx *context) *conn {
return &conn{
input_channel: make(chan []byte),
output_channel: make(chan []byte),
output_channel: make(chan []byte, 64),
httpTransactions: make(chan *httpTransaction),
timeout: time.Second * 30,
context: *ctx,
}
}
Expand All @@ -42,16 +45,13 @@ func (this *conn) ReadMessage() ([]byte, error) {
}

func (this *conn) WriteMessage(val []byte) (count int, err error) {
defer func() {
if recover() != nil {
err = ErrConnectionClosed
}
}()
val2 := make([]byte, len(val))
copy(val2, val)
go func() {
this.output_channel <- val2
}()
select {
case this.output_channel <- val2:
case <-time.After(this.timeout):
return 0, ErrConnectionClosed
}
return len(val), nil
}

Expand Down

0 comments on commit a6a65a8

Please sign in to comment.