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

fix: close transactionCommitter and transactionRollbacker with sync.Once #9

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 3 additions & 12 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,9 @@ func (c *connection) BeginTx(ctx context.Context, opts driver.TxOptions) (driver
select {
default:
// do nothing
case inout, ok := <-txStmtCh:
if !ok {
continue
}
case inout := <-txStmtCh:
inouts = append(inouts, inout)
case _, ok := <-commitCh:
if !ok {
continue
}
case <-commitCh:
var inputs []types.ParameterizedStatement
for _, inout := range inouts {
inputs = append(inputs, inout.input)
Expand All @@ -233,10 +227,7 @@ func (c *connection) BeginTx(ctx context.Context, opts driver.TxOptions) (driver
inouts[i].output = resp.Item
}
return
case _, ok := <-rollbackCh:
if !ok {
continue
}
case <-rollbackCh:
return
case <-ctx.Done():
return
Expand Down
11 changes: 8 additions & 3 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type transactionInOut struct {

// transationStatementPublisher publishes statements in a transaction.
type transactionStatementPublisher struct {
ch chan *transactionInOut
ch chan *transactionInOut
closeOnce sync.Once
}

// publish publishes a statement.
Expand All @@ -54,7 +55,9 @@ func (p *transactionStatementPublisher) publish(inout *transactionInOut) {

// close closes the channel.
func (p *transactionStatementPublisher) close() {
close(p.ch)
p.closeOnce.Do(func() {
close(p.ch)
})
}

// transactionCommitter commits a transaction.
Expand All @@ -75,7 +78,9 @@ func (c *transactionCommitter) commit() {

// close closes the channel.
func (c *transactionCommitter) close() {
close(c.ch)
c.closeOnce.Do(func() {
close(c.ch)
})
}

// transactionRollbacker rolls back a transaction.
Expand Down
Loading