Skip to content
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

Bugfix/read chunk loop #126

Merged
merged 10 commits into from
Jun 8, 2018
19 changes: 11 additions & 8 deletions vst/protocol/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,8 @@ var (

// dial opens a new connection to the server on the given address.
func dial(version Version, addr string, tlsConfig *tls.Config) (*Connection, error) {
var conn net.Conn
var err error
if tlsConfig != nil {
conn, err = tls.Dial("tcp", addr, tlsConfig)
} else {
conn, err = net.Dial("tcp", addr)
}
// Create TCP connection
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, driver.WithStack(err)
}
Expand All @@ -77,6 +72,12 @@ func dial(version Version, addr string, tlsConfig *tls.Config) (*Connection, err
tcpConn.SetNoDelay(true)
}

// Add TLS if needed
if tlsConfig != nil {
tlsConn := tls.Client(conn, tlsConfig)
conn = tlsConn
}

// Send protocol header
switch version {
case Version1_0:
Expand Down Expand Up @@ -209,6 +210,7 @@ func (c *Connection) sendChunk(deadline time.Time, chunk chunk) error {
// readChunkLoop reads chunks from the connection until it is closed.
func (c *Connection) readChunkLoop() {
recentErrors := 0
goodChunks := 0
for {
if c.IsClosed() {
// Closing, we're done
Expand All @@ -233,7 +235,7 @@ func (c *Connection) readChunkLoop() {
c.Close()
} else {
recentErrors++
fmt.Printf("readChunkLoop error: %#v\n", err)
fmt.Printf("readChunkLoop error: %#v (goodChunks=%d)\n", err, goodChunks)
if recentErrors > maxRecentErrors {
// When we get to many errors in a row, close this connection
c.Close()
Expand All @@ -246,6 +248,7 @@ func (c *Connection) readChunkLoop() {
} else {
// Process chunk
recentErrors = 0
goodChunks++
go c.processChunk(chunk)
}
}
Expand Down