les: move execqueue into utilities package

execqueue is a util for executing queued functions
in a serial order which is used by both les server
and les client. Move it to common package.
This commit is contained in:
rjl493456442 2020-01-06 15:18:21 +08:00
parent 4ae18842d9
commit 9a9b2de7fa
4 changed files with 37 additions and 36 deletions

View file

@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/les/flowcontrol" "github.com/ethereum/go-ethereum/les/flowcontrol"
"github.com/ethereum/go-ethereum/les/utilities"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
@ -285,7 +286,7 @@ func (h *serverHandler) measure(setup *benchmarkSetup, count int) error {
clientPeer := newPeer(lpv2, NetworkId, false, p2p.NewPeer(id, "client", nil), clientMeteredPipe) clientPeer := newPeer(lpv2, NetworkId, false, p2p.NewPeer(id, "client", nil), clientMeteredPipe)
serverPeer := newPeer(lpv2, NetworkId, false, p2p.NewPeer(id, "server", nil), serverMeteredPipe) serverPeer := newPeer(lpv2, NetworkId, false, p2p.NewPeer(id, "server", nil), serverMeteredPipe)
serverPeer.sendQueue = newExecQueue(count) serverPeer.sendQueue = utilities.NewExecQueue(count)
serverPeer.announceType = announceTypeNone serverPeer.announceType = announceTypeNone
serverPeer.fcCosts = make(requestCostTable) serverPeer.fcCosts = make(requestCostTable)
c := &requestCosts{} c := &requestCosts{}

View file

@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/les/flowcontrol" "github.com/ethereum/go-ethereum/les/flowcontrol"
"github.com/ethereum/go-ethereum/les/utilities"
"github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/light"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
@ -91,7 +92,7 @@ type peer struct {
headInfo *announceData headInfo *announceData
lock sync.RWMutex lock sync.RWMutex
sendQueue *execQueue sendQueue *utilities.ExecQueue
errCh chan error errCh chan error
@ -213,7 +214,7 @@ func (p *peer) freezeServer(frozen bool) {
f = 1 f = 1
} }
if atomic.SwapUint32(&p.frozen, f) != f && frozen { if atomic.SwapUint32(&p.frozen, f) != f && frozen {
p.sendQueue.clear() p.sendQueue.Clear()
} }
} }
@ -224,11 +225,11 @@ func (p *peer) isFrozen() bool {
} }
func (p *peer) canQueue() bool { func (p *peer) canQueue() bool {
return p.sendQueue.canQueue() && !p.isFrozen() return p.sendQueue.CanQueue() && !p.isFrozen()
} }
func (p *peer) queueSend(f func()) { func (p *peer) queueSend(f func()) {
p.sendQueue.queue(f) p.sendQueue.Queue(f)
} }
// Info gathers and returns a collection of metadata known about a peer. // Info gathers and returns a collection of metadata known about a peer.
@ -817,7 +818,7 @@ func (ps *peerSet) Register(p *peer) error {
return errAlreadyRegistered return errAlreadyRegistered
} }
ps.peers[p.id] = p ps.peers[p.id] = p
p.sendQueue = newExecQueue(100) p.sendQueue = utilities.NewExecQueue(100)
peers := make([]peerSetNotify, len(ps.notifyList)) peers := make([]peerSetNotify, len(ps.notifyList))
copy(peers, ps.notifyList) copy(peers, ps.notifyList)
ps.lock.Unlock() ps.lock.Unlock()
@ -845,7 +846,7 @@ func (ps *peerSet) Unregister(id string) error {
n.unregisterPeer(p) n.unregisterPeer(p)
} }
p.sendQueue.quit() p.sendQueue.Quit()
p.Peer.Disconnect(p2p.DiscUselessPeer) p.Peer.Disconnect(p2p.DiscUselessPeer)
return nil return nil

View file

@ -14,35 +14,35 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package les package utilities
import "sync" import "sync"
// execQueue implements a queue that executes function calls in a single thread, // ExecQueue implements a queue that executes function calls in a single thread,
// in the same order as they have been queued. // in the same order as they have been queued.
type execQueue struct { type ExecQueue struct {
mu sync.Mutex mu sync.Mutex
cond *sync.Cond cond *sync.Cond
funcs []func() funcs []func()
closeWait chan struct{} closeWait chan struct{}
} }
// newExecQueue creates a new execution queue. // NewExecQueue creates a new execution Queue.
func newExecQueue(capacity int) *execQueue { func NewExecQueue(capacity int) *ExecQueue {
q := &execQueue{funcs: make([]func(), 0, capacity)} q := &ExecQueue{funcs: make([]func(), 0, capacity)}
q.cond = sync.NewCond(&q.mu) q.cond = sync.NewCond(&q.mu)
go q.loop() go q.loop()
return q return q
} }
func (q *execQueue) loop() { func (q *ExecQueue) loop() {
for f := q.waitNext(false); f != nil; f = q.waitNext(true) { for f := q.waitNext(false); f != nil; f = q.waitNext(true) {
f() f()
} }
close(q.closeWait) close(q.closeWait)
} }
func (q *execQueue) waitNext(drop bool) (f func()) { func (q *ExecQueue) waitNext(drop bool) (f func()) {
q.mu.Lock() q.mu.Lock()
if drop && len(q.funcs) > 0 { if drop && len(q.funcs) > 0 {
// Remove the function that just executed. We do this here instead of when // Remove the function that just executed. We do this here instead of when
@ -60,20 +60,20 @@ func (q *execQueue) waitNext(drop bool) (f func()) {
return f return f
} }
func (q *execQueue) isClosed() bool { func (q *ExecQueue) isClosed() bool {
return q.closeWait != nil return q.closeWait != nil
} }
// canQueue returns true if more function calls can be added to the execution queue. // CanQueue returns true if more function calls can be added to the execution Queue.
func (q *execQueue) canQueue() bool { func (q *ExecQueue) CanQueue() bool {
q.mu.Lock() q.mu.Lock()
ok := !q.isClosed() && len(q.funcs) < cap(q.funcs) ok := !q.isClosed() && len(q.funcs) < cap(q.funcs)
q.mu.Unlock() q.mu.Unlock()
return ok return ok
} }
// queue adds a function call to the execution queue. Returns true if successful. // Queue adds a function call to the execution Queue. Returns true if successful.
func (q *execQueue) queue(f func()) bool { func (q *ExecQueue) Queue(f func()) bool {
q.mu.Lock() q.mu.Lock()
ok := !q.isClosed() && len(q.funcs) < cap(q.funcs) ok := !q.isClosed() && len(q.funcs) < cap(q.funcs)
if ok { if ok {
@ -84,16 +84,17 @@ func (q *execQueue) queue(f func()) bool {
return ok return ok
} }
// clear drops all queued functions // Clear drops all queued functions.
func (q *execQueue) clear() { func (q *ExecQueue) Clear() {
q.mu.Lock() q.mu.Lock()
q.funcs = q.funcs[:0] q.funcs = q.funcs[:0]
q.mu.Unlock() q.mu.Unlock()
} }
// quit stops the exec queue. // Quit stops the exec Queue.
// quit waits for the current execution to finish before returning. //
func (q *execQueue) quit() { // Quit waits for the current execution to finish before returning.
func (q *ExecQueue) Quit() {
q.mu.Lock() q.mu.Lock()
if !q.isClosed() { if !q.isClosed() {
q.closeWait = make(chan struct{}) q.closeWait = make(chan struct{})

View file

@ -14,21 +14,19 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package les package utilities
import ( import "testing"
"testing"
)
func TestExecQueue(t *testing.T) { func TestExecQueue(t *testing.T) {
var ( var (
N = 10000 N = 10000
q = newExecQueue(N) q = NewExecQueue(N)
counter int counter int
execd = make(chan int) execd = make(chan int)
testexit = make(chan struct{}) testexit = make(chan struct{})
) )
defer q.quit() defer q.Quit()
defer close(testexit) defer close(testexit)
check := func(state string, wantOK bool) { check := func(state string, wantOK bool) {
@ -40,11 +38,11 @@ func TestExecQueue(t *testing.T) {
case <-testexit: case <-testexit:
} }
} }
if q.canQueue() != wantOK { if q.CanQueue() != wantOK {
t.Fatalf("canQueue() == %t for %s", !wantOK, state) t.Fatalf("CanQueue() == %t for %s", !wantOK, state)
} }
if q.queue(qf) != wantOK { if q.Queue(qf) != wantOK {
t.Fatalf("canQueue() == %t for %s", !wantOK, state) t.Fatalf("Queue() == %t for %s", !wantOK, state)
} }
} }
@ -57,6 +55,6 @@ func TestExecQueue(t *testing.T) {
t.Fatal("execution out of order") t.Fatal("execution out of order")
} }
} }
q.quit() q.Quit()
check("closed queue", false) check("closed queue", false)
} }