File tree 1 file changed +7
-1
lines changed
1 file changed +7
-1
lines changed Original file line number Diff line number Diff line change 5
5
"context"
6
6
"fmt"
7
7
"io"
8
+ "runtime"
8
9
"sync"
9
10
"time"
10
11
@@ -19,7 +20,6 @@ type controlFrame struct {
19
20
// Conn represents a WebSocket connection.
20
21
// Pings will always be automatically responded to with pongs, you do not
21
22
// have to do anything special.
22
- // TODO set finalizer
23
23
type Conn struct {
24
24
subprotocol string
25
25
br * bufio.Reader
@@ -59,6 +59,8 @@ func (c *Conn) close(err error) {
59
59
}
60
60
61
61
c .closeOnce .Do (func () {
62
+ runtime .SetFinalizer (c , nil )
63
+
62
64
c .closeErr = err
63
65
64
66
cerr := c .closer .Close ()
@@ -82,6 +84,10 @@ func (c *Conn) init() {
82
84
c .read = make (chan opcode )
83
85
c .readBytes = make (chan []byte )
84
86
87
+ runtime .SetFinalizer (c , func (c * Conn ) {
88
+ c .Close (StatusInternalError , "websocket: connection ended up being garbage collected" )
89
+ })
90
+
85
91
go c .writeLoop ()
86
92
go c .readLoop ()
87
93
}
You can’t perform that action at this time.
0 commit comments