Fix memory leaks

This commit is contained in:
Leonid Bugaev
2015-08-26 22:25:58 +03:00
parent 1fea397d54
commit 8daa7fdcab
4 changed files with 12 additions and 5 deletions
+2
View File
@@ -20,5 +20,7 @@ ADD . /gopath/src/github.com/buger/gor/
RUN javac -cp /tmp/commons-io-2.4/commons-io-2.4.jar ./examples/middleware/echo.java
RUN apt-get install graphviz -y
RUN go get -u github.com/golang/lint/golint
RUN go get
+1 -1
View File
@@ -20,7 +20,7 @@ drace:
docker run -v `pwd`:$(SOURCE_PATH) -t -i --env GORACE="halt_on_error=1" gor go test ./... $(ARGS) -v -race -timeout 15s
dtest:
docker run -v `pwd`:$(SOURCE_PATH) -t -i gor go test ./... $(ARGS) -v -timeout 60s
docker run -v `pwd`:$(SOURCE_PATH) -t -i gor go test ./... -timeout 5s $(ARGS) -v
dcover:
docker run -v `pwd`:$(SOURCE_PATH) -t -i --env GORACE="halt_on_error=1" gor go test $(ARGS) -race -v -timeout 15s -coverprofile=coverage.out
+1 -1
View File
@@ -257,4 +257,4 @@ func (t *Listener) Close() {
close(t.quit)
t.conn.Close()
return
}
}
+8 -3
View File
@@ -46,7 +46,6 @@ func NewTCPMessage(ID string, delChan chan *TCPMessage, Ack uint32, expire *time
msg.Start = time.Now().UnixNano()
msg.packetsChan = make(chan *TCPPacket)
msg.delChan = delChan // used for notifying that message completed or expired
msg.timer = time.NewTimer(0)
go msg.listen()
@@ -69,6 +68,10 @@ func (t *TCPMessage) listen() {
// Timeout notifies message to stop listening, close channel and message ready to be sent
func (t *TCPMessage) Timeout() {
if t.timer != nil {
t.timer.Stop()
}
select {
// In some cases Timeout can be called multiple times (do not know how yet)
// Ensure that we did not close channel 2 times
@@ -139,12 +142,14 @@ func (t *TCPMessage) AddPacket(packet *TCPPacket) {
t.Timeout()
} else {
// If more then 1 packet, wait for more, and set expiration
if len(t.packets) == 1 {
if len(t.packets) > 1 {
// Every time we receive packet we reset this timer
t.timer = time.AfterFunc(*t.expire, t.Timeout)
} else {
// Reset message timeout timer
t.timer.Reset(*t.expire)
if t.timer != nil {
t.timer.Reset(*t.expire)
}
}
}
}