Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
8ce2ad39
Unverified
Commit
8ce2ad39
authored
Apr 18, 2023
by
Michael de Hoog
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Small refactor
parent
dcdeeaf9
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
27 deletions
+26
-27
queue.go
op-service/txmgr/queue.go
+26
-27
No files found.
op-service/txmgr/queue.go
View file @
8ce2ad39
...
@@ -49,23 +49,6 @@ func NewQueue[T any](txMgr TxManager, maxPending uint64, pendingChanged func(uin
...
@@ -49,23 +49,6 @@ func NewQueue[T any](txMgr TxManager, maxPending uint64, pendingChanged func(uin
}
}
}
}
func
(
q
*
Queue
[
T
])
groupContext
()
context
.
Context
{
q
.
groupLock
.
Lock
()
defer
q
.
groupLock
.
Unlock
()
if
q
.
groupCtx
!=
nil
&&
q
.
groupCtx
.
Err
()
==
nil
{
// a group exists and has no errors, nothing to do
return
q
.
groupCtx
}
q
.
Wait
()
// wait for existing processes in the group to stop (if any)
q
.
group
,
q
.
groupCtx
=
errgroup
.
WithContext
(
context
.
Background
())
if
q
.
maxPending
>
0
{
q
.
group
.
SetLimit
(
int
(
q
.
maxPending
))
}
else
{
q
.
group
.
SetLimit
(
-
1
)
}
return
q
.
groupCtx
}
// Wait waits for all pending txs to complete (or fail).
// Wait waits for all pending txs to complete (or fail).
func
(
q
*
Queue
[
T
])
Wait
()
{
func
(
q
*
Queue
[
T
])
Wait
()
{
if
q
.
group
==
nil
{
if
q
.
group
==
nil
{
...
@@ -81,8 +64,7 @@ func (q *Queue[T]) Wait() {
...
@@ -81,8 +64,7 @@ func (q *Queue[T]) Wait() {
// The actual tx sending is non-blocking, with the receipt returned on the
// The actual tx sending is non-blocking, with the receipt returned on the
// provided receipt channel.
// provided receipt channel.
func
(
q
*
Queue
[
T
])
Send
(
ctx
context
.
Context
,
factory
TxFactory
[
T
],
receiptCh
chan
TxReceipt
[
T
])
error
{
func
(
q
*
Queue
[
T
])
Send
(
ctx
context
.
Context
,
factory
TxFactory
[
T
],
receiptCh
chan
TxReceipt
[
T
])
error
{
groupCtx
:=
q
.
groupContext
()
ctx
,
cancel
:=
q
.
mergeWithGroupContext
(
ctx
)
ctx
,
cancel
:=
mergeContexts
(
ctx
,
groupCtx
)
factoryErrCh
:=
make
(
chan
error
)
factoryErrCh
:=
make
(
chan
error
)
q
.
group
.
Go
(
func
()
error
{
q
.
group
.
Go
(
func
()
error
{
defer
cancel
()
defer
cancel
()
...
@@ -102,8 +84,7 @@ func (q *Queue[T]) Send(ctx context.Context, factory TxFactory[T], receiptCh cha
...
@@ -102,8 +84,7 @@ func (q *Queue[T]) Send(ctx context.Context, factory TxFactory[T], receiptCh cha
// The actual tx sending is non-blocking, with the receipt returned on the
// The actual tx sending is non-blocking, with the receipt returned on the
// provided receipt channel.
// provided receipt channel.
func
(
q
*
Queue
[
T
])
TrySend
(
ctx
context
.
Context
,
factory
TxFactory
[
T
],
receiptCh
chan
TxReceipt
[
T
])
(
bool
,
error
)
{
func
(
q
*
Queue
[
T
])
TrySend
(
ctx
context
.
Context
,
factory
TxFactory
[
T
],
receiptCh
chan
TxReceipt
[
T
])
(
bool
,
error
)
{
groupCtx
:=
q
.
groupContext
()
ctx
,
cancel
:=
q
.
mergeWithGroupContext
(
ctx
)
ctx
,
cancel
:=
mergeContexts
(
ctx
,
groupCtx
)
factoryErrCh
:=
make
(
chan
error
)
factoryErrCh
:=
make
(
chan
error
)
started
:=
q
.
group
.
TryGo
(
func
()
error
{
started
:=
q
.
group
.
TryGo
(
func
()
error
{
defer
cancel
()
defer
cancel
()
...
@@ -122,7 +103,7 @@ func (q *Queue[T]) sendTx(ctx context.Context, factory TxFactory[T], factoryErro
...
@@ -122,7 +103,7 @@ func (q *Queue[T]) sendTx(ctx context.Context, factory TxFactory[T], factoryErro
factoryErrorCh
<-
err
factoryErrorCh
<-
err
if
err
!=
nil
{
if
err
!=
nil
{
// Factory returned an error which was returned in the channel. This means
// Factory returned an error which was returned in the channel. This means
// there
wa
s no tx to send, so return nil.
// there
i
s no tx to send, so return nil.
return
nil
return
nil
}
}
...
@@ -139,15 +120,33 @@ func (q *Queue[T]) sendTx(ctx context.Context, factory TxFactory[T], factoryErro
...
@@ -139,15 +120,33 @@ func (q *Queue[T]) sendTx(ctx context.Context, factory TxFactory[T], factoryErro
return
err
return
err
}
}
// mergeContexts creates a new Context that is canceled if either of the two
// mergeWithGroupContext creates a new Context that is canceled if either the given context is
// contexts are closed. The CancelFunc should be called once finished.
// Done, or the group context is canceled. The returned CancelFunc should be called once finished.
func
mergeContexts
(
ctx1
context
.
Context
,
ctx2
context
.
Context
)
(
context
.
Context
,
context
.
CancelFunc
)
{
//
ctx
,
cancel
:=
context
.
WithCancel
(
ctx1
)
// If the group context doesn't exist or has already been canceled, a new one is created after
// waiting for existing group threads to complete.
func
(
q
*
Queue
[
T
])
mergeWithGroupContext
(
ctx
context
.
Context
)
(
context
.
Context
,
context
.
CancelFunc
)
{
q
.
groupLock
.
Lock
()
defer
q
.
groupLock
.
Unlock
()
if
q
.
groupCtx
==
nil
||
q
.
groupCtx
.
Err
()
!=
nil
{
// no group exists, or the existing context has an error, so we need to wait
// for existing group threads to complete (if any) and create a new group
q
.
Wait
()
q
.
group
,
q
.
groupCtx
=
errgroup
.
WithContext
(
context
.
Background
())
if
q
.
maxPending
>
0
{
q
.
group
.
SetLimit
(
int
(
q
.
maxPending
))
}
else
{
q
.
group
.
SetLimit
(
-
1
)
}
}
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
cl
:=
make
(
chan
struct
{})
cl
:=
make
(
chan
struct
{})
groupContext
:=
q
.
groupCtx
go
func
()
{
go
func
()
{
defer
cancel
()
defer
cancel
()
select
{
select
{
case
<-
ctx2
.
Done
()
:
case
<-
groupContext
.
Done
()
:
case
<-
cl
:
case
<-
cl
:
}
}
}()
}()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment