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
019ce89c
Unverified
Commit
019ce89c
authored
Apr 11, 2023
by
Michael de Hoog
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cond -> Semaphore
parent
74029794
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
26 deletions
+36
-26
go.mod
go.mod
+1
-1
queue.go
op-service/txmgr/queue.go
+35
-25
No files found.
go.mod
View file @
019ce89c
...
@@ -34,6 +34,7 @@ require (
...
@@ -34,6 +34,7 @@ require (
github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa
github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa
golang.org/x/crypto v0.6.0
golang.org/x/crypto v0.6.0
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb
golang.org/x/sync v0.1.0
golang.org/x/term v0.5.0
golang.org/x/term v0.5.0
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af
)
)
...
@@ -176,7 +177,6 @@ require (
...
@@ -176,7 +177,6 @@ require (
go.uber.org/zap v1.24.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/tools v0.6.0 // indirect
...
...
op-service/txmgr/queue.go
View file @
019ce89c
...
@@ -2,9 +2,12 @@ package txmgr
...
@@ -2,9 +2,12 @@ package txmgr
import
(
import
(
"context"
"context"
"math"
"sync"
"sync"
"sync/atomic"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/types"
"golang.org/x/sync/semaphore"
)
)
type
TxReceipt
[
T
any
]
struct
{
type
TxReceipt
[
T
any
]
struct
{
...
@@ -17,10 +20,9 @@ type TxFactory[T any] func(ctx context.Context) (*TxCandidate, T, error)
...
@@ -17,10 +20,9 @@ type TxFactory[T any] func(ctx context.Context) (*TxCandidate, T, error)
type
Queue
[
T
any
]
struct
{
type
Queue
[
T
any
]
struct
{
txMgr
TxManager
txMgr
TxManager
maxPending
uint64
pendingChanged
func
(
uint64
)
pendingChanged
func
(
uint64
)
pending
u
int64
pending
atomic
.
U
int64
cond
*
sync
.
Con
d
semaphore
*
semaphore
.
Weighte
d
wg
sync
.
WaitGroup
wg
sync
.
WaitGroup
}
}
...
@@ -29,11 +31,19 @@ type Queue[T any] struct {
...
@@ -29,11 +31,19 @@ type Queue[T any] struct {
// - pendingChanged: called whenever a job starts or finishes. The
// - pendingChanged: called whenever a job starts or finishes. The
// number of currently pending txs is passed as a parameter.
// number of currently pending txs is passed as a parameter.
func
NewQueue
[
T
any
](
txMgr
TxManager
,
maxPending
uint64
,
pendingChanged
func
(
uint64
))
*
Queue
[
T
]
{
func
NewQueue
[
T
any
](
txMgr
TxManager
,
maxPending
uint64
,
pendingChanged
func
(
uint64
))
*
Queue
[
T
]
{
if
maxPending
>
math
.
MaxInt64
{
// ensure we don't overflow as semaphore only accepts int64; in reality this will never be an issue
maxPending
=
math
.
MaxInt64
}
var
s
*
semaphore
.
Weighted
if
maxPending
>
0
{
// only create a semaphore for limited-size queues
s
=
semaphore
.
NewWeighted
(
int64
(
maxPending
))
}
return
&
Queue
[
T
]{
return
&
Queue
[
T
]{
txMgr
:
txMgr
,
txMgr
:
txMgr
,
maxPending
:
maxPending
,
pendingChanged
:
pendingChanged
,
pendingChanged
:
pendingChanged
,
cond
:
sync
.
NewCond
(
&
sync
.
Mutex
{})
,
semaphore
:
s
,
}
}
}
}
...
@@ -46,10 +56,11 @@ func (q *Queue[T]) Wait() {
...
@@ -46,10 +56,11 @@ func (q *Queue[T]) Wait() {
// and then send the next tx. The TxFactory should return `nil` if the next
// and then send the next tx. The TxFactory should return `nil` if the next
// tx does not exist. Returns the error returned from the TxFactory (if any).
// tx does not exist. Returns the error returned from the TxFactory (if any).
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
{
q
.
cond
.
L
.
Lock
()
if
q
.
semaphore
!=
nil
{
defer
q
.
cond
.
L
.
Unlock
()
err
:=
q
.
semaphore
.
Acquire
(
ctx
,
1
)
for
q
.
full
()
{
if
err
!=
nil
{
q
.
cond
.
Wait
()
return
err
}
}
}
return
q
.
trySend
(
ctx
,
factory
,
receiptCh
)
return
q
.
trySend
(
ctx
,
factory
,
receiptCh
)
}
}
...
@@ -60,34 +71,37 @@ func (q *Queue[T]) Send(ctx context.Context, factory TxFactory[T], receiptCh cha
...
@@ -60,34 +71,37 @@ func (q *Queue[T]) Send(ctx context.Context, factory TxFactory[T], receiptCh cha
// The TxFactory should return `nil` if the next tx does not exist. Returns
// The TxFactory should return `nil` if the next tx does not exist. Returns
// the error returned from the TxFactory (if any).
// the error returned from the TxFactory (if any).
func
(
q
*
Queue
[
T
])
TrySend
(
ctx
context
.
Context
,
factory
TxFactory
[
T
],
receiptCh
chan
TxReceipt
[
T
])
error
{
func
(
q
*
Queue
[
T
])
TrySend
(
ctx
context
.
Context
,
factory
TxFactory
[
T
],
receiptCh
chan
TxReceipt
[
T
])
error
{
q
.
cond
.
L
.
Lock
()
if
q
.
semaphore
!=
nil
{
defer
q
.
cond
.
L
.
Unlock
()
if
!
q
.
semaphore
.
TryAcquire
(
1
)
{
return
nil
}
}
return
q
.
trySend
(
ctx
,
factory
,
receiptCh
)
return
q
.
trySend
(
ctx
,
factory
,
receiptCh
)
}
}
func
(
q
*
Queue
[
T
])
trySend
(
ctx
context
.
Context
,
factory
TxFactory
[
T
],
receiptCh
chan
TxReceipt
[
T
])
error
{
func
(
q
*
Queue
[
T
])
trySend
(
ctx
context
.
Context
,
factory
TxFactory
[
T
],
receiptCh
chan
TxReceipt
[
T
])
error
{
if
q
.
full
()
{
return
nil
}
candidate
,
data
,
err
:=
factory
(
ctx
)
candidate
,
data
,
err
:=
factory
(
ctx
)
release
:=
func
()
{
if
q
.
semaphore
!=
nil
{
q
.
semaphore
.
Release
(
1
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
release
()
return
err
return
err
}
}
if
candidate
==
nil
{
if
candidate
==
nil
{
release
()
return
nil
return
nil
}
}
q
.
pending
++
q
.
pendingChanged
(
q
.
pending
.
Add
(
1
))
q
.
pendingChanged
(
q
.
pending
)
q
.
wg
.
Add
(
1
)
q
.
wg
.
Add
(
1
)
go
func
()
{
go
func
()
{
defer
func
()
{
defer
func
()
{
q
.
cond
.
L
.
Lock
()
release
()
q
.
pending
--
q
.
pendingChanged
(
q
.
pending
.
Add
(
^
uint64
(
0
)))
// -1
q
.
pendingChanged
(
q
.
pending
)
q
.
wg
.
Done
()
q
.
wg
.
Done
()
q
.
cond
.
L
.
Unlock
()
q
.
cond
.
Broadcast
()
}()
}()
receipt
,
err
:=
q
.
txMgr
.
Send
(
ctx
,
*
candidate
)
receipt
,
err
:=
q
.
txMgr
.
Send
(
ctx
,
*
candidate
)
receiptCh
<-
TxReceipt
[
T
]{
receiptCh
<-
TxReceipt
[
T
]{
...
@@ -98,7 +112,3 @@ func (q *Queue[T]) trySend(ctx context.Context, factory TxFactory[T], receiptCh
...
@@ -98,7 +112,3 @@ func (q *Queue[T]) trySend(ctx context.Context, factory TxFactory[T], receiptCh
}()
}()
return
nil
return
nil
}
}
func
(
q
*
Queue
[
T
])
full
()
bool
{
return
q
.
maxPending
>
0
&&
q
.
pending
>=
q
.
maxPending
}
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