Skip to content

Commit 76c8f8b

Browse files
committed
fix: remove unnessary blocking on creating transaction
1 parent 310477a commit 76c8f8b

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

sip/transaction_layer.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,35 +223,26 @@ func (txl *TransactionLayer) NewClientTransaction(ctx context.Context, req *Requ
223223
}
224224

225225
func (txl *TransactionLayer) clientTxRequest(ctx context.Context, req *Request, key string) (*ClientTx, error) {
226+
conn, err := txl.tpl.ClientRequestConnection(ctx, req)
227+
if err != nil {
228+
return nil, fmt.Errorf("client transcation failed to request connection: %w", err)
229+
}
230+
226231
txl.clientTransactions.lock()
227232
tx, exists := txl.clientTransactions.items[key]
228233
if exists {
229234
txl.clientTransactions.unlock()
235+
conn.TryClose()
230236
return nil, fmt.Errorf("client transaction %q already exists", key)
231237
}
232-
233-
tx, err := txl.clientTxCreate(ctx, req, key)
234-
if err != nil {
235-
txl.clientTransactions.unlock()
236-
return nil, fmt.Errorf("failed to create client transaction: %w", err)
237-
}
238+
tx = NewClientTx(key, req, conn, txl.log)
238239

239240
txl.clientTransactions.items[key] = tx
240241
tx.OnTerminate(txl.clientTxTerminate)
241242
txl.clientTransactions.unlock()
242243
return tx, nil
243244
}
244245

245-
func (txl *TransactionLayer) clientTxCreate(ctx context.Context, req *Request, key string) (*ClientTx, error) {
246-
conn, err := txl.tpl.ClientRequestConnection(ctx, req)
247-
if err != nil {
248-
return nil, err
249-
}
250-
251-
tx := NewClientTx(key, req, conn, txl.log)
252-
return tx, nil
253-
}
254-
255246
func (txl *TransactionLayer) Respond(res *Response) (*ServerTx, error) {
256247
key, err := MakeServerTxKey(res)
257248
if err != nil {

sip/transaction_layer_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync/atomic"
99
"testing"
1010

11+
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213
)
1314

@@ -98,4 +99,6 @@ func TestTransactionLayerClientTx(t *testing.T) {
9899
wg.Wait()
99100
// Only one transaction will be created and executed
100101
require.EqualValues(t, 1, atomic.LoadInt32(&count))
102+
require.Equal(t, 2, tp.udp.pool.Size())
103+
assert.True(t, tp.udp.pool.Get("127.0.0.1:9876") != nil)
101104
}

0 commit comments

Comments
 (0)