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 2 commits
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
33 changes: 32 additions & 1 deletion coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"time"
Expand Down Expand Up @@ -46,6 +47,33 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do 1 line slice copies as well!

Suggested change
cpy := make([]byte, len(p))
copy(cpy, p)
return c.conn.Write(cpy)
return c.conn.Write(append(([]byte)(nil), p...))

}

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)
Expand All @@ -67,10 +95,13 @@ 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(websocket.NetConn(r.Context(), conn, websocket.MessageBinary), nil)
session, err := yamux.Server(proxiedConn, nil)
if err != nil {
_ = conn.Close(websocket.StatusInternalError, fmt.Sprintf("multiplex server: %s", err))
return
Expand Down