Skip to content

Commit

Permalink
switch to bytes.HasPrefix to avoid allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman committed Sep 17, 2019
1 parent 9c48d0d commit c6f3d0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
9 changes: 2 additions & 7 deletions tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// transmit the tunnel UUID.
const InternalDataOpcode = ""

var internalOpcodeIns = fmt.Sprint(len(InternalDataOpcode), ".", InternalDataOpcode)
var internalOpcodeIns = []byte(fmt.Sprint(len(InternalDataOpcode), ".", InternalDataOpcode))

// InstructionReader provides reading functionality to a Stream
type InstructionReader interface {
Expand Down Expand Up @@ -97,12 +97,7 @@ func (t *SimpleTunnel) HasQueuedWriterThreads() bool {

// Close closes the underlying stream
func (t *SimpleTunnel) Close() (err error) {
if t.stream != nil {
err = t.stream.Close()
} else {
err = ErrConnectionClosed.NewError("Closed")
}
return
return t.stream.Close()
}

// GetUUID returns the tunnel's UUID
Expand Down
25 changes: 19 additions & 6 deletions wsServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/sirupsen/logrus"
"io"
"net/http"
"strings"
"sync"
)

Expand Down Expand Up @@ -44,14 +43,22 @@ func (s *WebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logrus.Error("Failed to upgrade websocket", err)
return
}
defer ws.Close()
defer func() {
if err = ws.Close(); err != nil {
logrus.Errorln("Error closing websocket", err)
}
}()

logrus.Debug("Connecting to tunnel")
tunnel, e := s.connect(r)
if e != nil {
return
}
defer tunnel.Close()
defer func() {
if err = tunnel.Close(); err != nil {
logrus.Errorln("Error closing tunnel", err)
}
}()
logrus.Debug("Connected to tunnel")

id := tunnel.ConnectionID()
Expand Down Expand Up @@ -105,11 +112,14 @@ func wsToGuacd(ws MessageReader, guacd io.Writer) {
for {
_, data, err := ws.ReadMessage()
if err != nil {
if err.Error() == "close 1005 (no status)" || err.Error() == "use of closed network connection" {
return
}
logrus.Errorln("Error reading message from ws", err)
return
}

if strings.HasPrefix(string(data), internalOpcodeIns) {
if bytes.HasPrefix(data, internalOpcodeIns) {
// TODO handle custom ping (need to use InstructionReader)

// messages starting with the InternalDataOpcode are never sent to guacd
Expand All @@ -133,11 +143,11 @@ func guacdToWs(ws MessageWriter, guacd InstructionReader) {
for {
ins, err := guacd.ReadSome()
if err != nil {
logrus.Errorln("Error reading from guacd ", err)
logrus.Errorln("Error reading from guacd", err)
return
}

if strings.HasPrefix(string(ins), internalOpcodeIns) {
if bytes.HasPrefix(ins, internalOpcodeIns) {
// TODO handle custom ping (need to use InstructionReader)

// messages starting with the InternalDataOpcode are never sent to guacd
Expand All @@ -152,6 +162,9 @@ func guacdToWs(ws MessageWriter, guacd InstructionReader) {

if !guacd.Available() || buf.Len() >= maxGuacMessage {
if err = ws.WriteMessage(1, buf.Bytes()); err != nil {
if err == websocket.ErrCloseSent {
return
}
logrus.Errorln("Failed sending message to ws", err)
return
}
Expand Down

0 comments on commit c6f3d0f

Please sign in to comment.