Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Disable compression for websocket dRPC transport #145

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

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Turn off compression to avoid race
  • Loading branch information
bryphe-coder committed Feb 2, 2022
commit eef4d3af9536226efb9b0a19e632bf2728a38c02
38 changes: 5 additions & 33 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"time"
Expand Down Expand Up @@ -47,36 +46,12 @@ func (api *api) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
render.JSON(rw, r, daemons)
}

type proxiedConn struct {
conn io.ReadWriteCloser
}

func (c *proxiedConn) Close() error {
return c.conn.Close()
}
func (c *proxiedConn) Write(p []byte) (int, error) {
// Copy the data to avoid a race condition
cpy := make([]byte, len(p))
copy(cpy, p)
return c.conn.Write(cpy)
}

func (c *proxiedConn) Read(p []byte) (int, error) {
// In theory, this could be deep-copied too - but it actually causes a failure.
//cpy := make([]byte, len(p))
//copy(cpy, p)
return c.conn.Read(p)
}

func createCopyOnReadProxy(pipesToProxy io.ReadWriteCloser) io.ReadWriteCloser {
return &proxiedConn{
conn: pipesToProxy,
}
}

// Serves the provisioner daemon protobuf API over a WebSocket.
func (api *api) provisionerDaemonsServe(rw http.ResponseWriter, r *http.Request) {
conn, err := websocket.Accept(rw, r, nil)
conn, err := websocket.Accept(rw, r, &websocket.AcceptOptions{
// Need to disable compression to avoid a data-race
CompressionMode: websocket.CompressionDisabled,
})
if err != nil {
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
Message: fmt.Sprintf("accept websocket: %s", err),
Expand All @@ -95,13 +70,10 @@ func (api *api) provisionerDaemonsServe(rw http.ResponseWriter, r *http.Request)
return
}

netConn := websocket.NetConn(r.Context(), conn, websocket.MessageBinary)
proxiedConn := createCopyOnReadProxy(netConn)

// Multiplexes the incoming connection using yamux.
// This allows multiple function calls to occur over
// the same connection.
session, err := yamux.Server(proxiedConn, nil)
session, err := yamux.Server(websocket.NetConn(r.Context(), conn, websocket.MessageBinary), nil)
if err != nil {
_ = conn.Close(websocket.StatusInternalError, fmt.Sprintf("multiplex server: %s", err))
return
Expand Down