Skip to content

Commit be75c42

Browse files
committed
make done channel private and create public Shutdown method
1 parent 10ab726 commit be75c42

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

websocketproxy.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package websocketproxy
33

44
import (
5+
"context"
56
"fmt"
67
"io"
78
"log"
@@ -47,9 +48,9 @@ type (
4748
// If nil, DefaultDialer is used.
4849
Dialer *websocket.Dialer
4950

50-
// Done specifies a channel for which all proxied websocket connections
51+
// done specifies a channel for which all proxied websocket connections
5152
// can be closed on demand by closing the channel.
52-
Done chan struct{}
53+
done chan struct{}
5354
}
5455

5556
websocketMsg struct {
@@ -186,6 +187,9 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
186187

187188
errClient := make(chan error, 1)
188189
errBackend := make(chan error, 1)
190+
if w.done == nil {
191+
w.done = make(chan struct{})
192+
}
189193

190194
replicateWebsocketConn := func(dst, src *websocket.Conn, errc chan error) {
191195
websocketMsgRcverC := make(chan websocketMsg, 1)
@@ -214,7 +218,7 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
214218
errc <- err
215219
break
216220
}
217-
case <-w.Done:
221+
case <-w.done:
218222
m := websocket.FormatCloseMessage(websocket.CloseGoingAway, "websocketproxy: closing connection")
219223
dst.WriteMessage(websocket.CloseMessage, m)
220224
break
@@ -234,8 +238,18 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
234238
if e, ok := err.(*websocket.CloseError); !ok || e.Code == websocket.CloseAbnormalClosure {
235239
log.Printf("websocketproxy: Error when copying from client to backend: %v", err)
236240
}
237-
case <-w.Done:
241+
case <-w.done:
242+
}
243+
}
244+
245+
// Shutdown closes ws connections by closing the done channel they are subscribed to.
246+
func (w *WebsocketProxy) Shutdown(ctx context.Context) error {
247+
// TODO: support using context for control and return error when applicable
248+
// Currently implemented such that the method signature matches http.Server.Shutdown()
249+
if w.done != nil {
250+
close(w.done)
238251
}
252+
return nil
239253
}
240254

241255
func copyHeader(dst, src http.Header) {

websocketproxy_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package websocketproxy
22

33
import (
4+
"context"
45
"log"
56
"net/http"
67
"net/url"
@@ -30,7 +31,6 @@ func TestProxy(t *testing.T) {
3031
u, _ := url.Parse(backendURL)
3132
proxy := NewProxy(u)
3233
proxy.Upgrader = upgrader
33-
proxy.Done = make(chan struct{})
3434

3535
mux := http.NewServeMux()
3636
mux.Handle("/proxy", proxy)
@@ -123,5 +123,5 @@ func TestProxy(t *testing.T) {
123123
t.Errorf("expecting: %s, got: %s", msg, string(p))
124124
}
125125

126-
close(proxy.Done)
126+
proxy.Shutdown(context.Background())
127127
}

0 commit comments

Comments
 (0)