Skip to content

Commit

Permalink
Merge pull request #1 from ntnn/better_channel_handling
Browse files Browse the repository at this point in the history
Return values from both data and error channels at once
  • Loading branch information
cb80 authored May 9, 2019
2 parents db68d93 + 22d8a4b commit 4abd9e1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 38 deletions.
9 changes: 2 additions & 7 deletions cgo/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,9 @@ func (conn *connection) execContext(ctx context.Context, query string) (*csComma
select {
case <-ctx.Done():
return nil, ctx.Err()
case cmd := <-recvCmd:
if cmd != nil {
return cmd, nil
}
case err := <-recvErr:
if err != nil {
return nil, err
}
cmd := <-recvCmd
return cmd, err
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions cgo/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewConnector(dsn libdsn.DsnInfo) (driver.Connector, error) {
driverCtx.drop()
return nil, fmt.Errorf("Failed to open connection: %v", err)
}

defer func() {
// In- and decrease connections count before and after closing
// connection to prevent the context being deallocated.
Expand Down Expand Up @@ -64,10 +65,9 @@ func (connector *connector) Connect(ctx context.Context) (driver.Conn, error) {
}
}()
return nil, ctx.Err()
case conn := <-connChan:
return conn, nil
case err := <-errChan:
return nil, err
conn := <-connChan
return conn, err
}
}

Expand Down
27 changes: 6 additions & 21 deletions cgo/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,9 @@ func (conn *connection) PrepareContext(ctx context.Context, query string) (drive
}
}()
return nil, ctx.Err()
case stmt := <-recvStmt:
if stmt != nil {
return stmt, nil
}
case err := <-recvErr:
if err != nil {
return nil, err
}
stmt := <-recvStmt
return stmt, err
}
}
}
Expand Down Expand Up @@ -291,14 +286,9 @@ func (stmt *statement) ExecContext(ctx context.Context, args []driver.NamedValue
select {
case <-ctx.Done():
return nil, ctx.Err()
case res := <-recvResult:
if res != nil {
return res, nil
}
case err := <-recvErr:
if err != nil {
return nil, err
}
res := <-recvResult
return res, err
}
}
}
Expand Down Expand Up @@ -343,14 +333,9 @@ func (stmt *statement) QueryContext(ctx context.Context, args []driver.NamedValu
}
}()
return nil, ctx.Err()
case rows := <-recvRows:
if rows != nil {
return rows, nil
}
case err := <-recvErr:
if err != nil {
return nil, err
}
rows := <-recvRows
return rows, err
}
}
}
9 changes: 2 additions & 7 deletions cgo/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,9 @@ func (conn *connection) BeginTx(ctx context.Context, opts driver.TxOptions) (dri
}
}()
return nil, ctx.Err()
case tx := <-recvTx:
if tx != nil {
return tx, nil
}
case err := <-recvErr:
if err != nil {
return nil, err
}
tx := <-recvTx
return tx, err
}
}
}
Expand Down

0 comments on commit 4abd9e1

Please sign in to comment.