From 0c7474bdd8246b388c4d3cb86f1e8aa3996df500 Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Fri, 27 Sep 2019 07:39:32 +0200 Subject: [PATCH] les: added client event hook --- les/api.go | 24 ++++++++++++++++++++---- les/clientpool.go | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/les/api.go b/les/api.go index ae5f1c642abc..f9212513cc5e 100644 --- a/les/api.go +++ b/les/api.go @@ -57,10 +57,14 @@ type PrivateLightServerAPI struct { // NewPrivateLightServerAPI creates a new LES light server API. func NewPrivateLightServerAPI(server *LesServer) *PrivateLightServerAPI { - return &PrivateLightServerAPI{ - server: server, - subs: make(map[*eventSub]struct{}), + api := &PrivateLightServerAPI{ + server: server, + defaultPosFactors: server.clientPool.defaultPosFactors, + defaultNegFactors: server.clientPool.defaultNegFactors, + subs: make(map[*eventSub]struct{}), } + server.clientPool.eventHook = api.sendEvent + return api } // ServerInfo returns global server parameters @@ -82,6 +86,12 @@ func (api *PrivateLightServerAPI) ClientInfo(ids []enode.ID) map[enode.ID]map[st return res } +// PriorityClientInfo returns information about clients with a positive balance +// in the given ID range (stop excluded). If stop is null then the iterator stops +// only at the end of the ID space. MaxCount limits the number of results returned. +// If maxCount limit is applied but there are more potential results then the ID +// of the next potential result is included in the map with an empty structure +// assigned to it. func (api *PrivateLightServerAPI) PriorityClientInfo(start, stop enode.ID, maxCount int) map[enode.ID]map[string]interface{} { res := make(map[enode.ID]map[string]interface{}) ids := api.server.clientPool.getPosBalanceIDs(start, stop, maxCount+1) @@ -137,6 +147,8 @@ func (api *PrivateLightServerAPI) sendEvent(clientEvent string, client *clientIn } } +// setParams either sets the given parameters for a single client (if ID is specified) +// or the default parameters applicable to clients connected in the future func (api *PrivateLightServerAPI) setParams(params map[string]interface{}, client *clientInfo, id enode.ID, posFactors, negFactors *priceFactors) (updateFactors bool, err error) { if client != nil { posFactors, negFactors = &client.posFactors, &client.negFactors @@ -223,11 +235,14 @@ loop: return updateFactors, err } +// UpdateBalance updates the balance of a client (either overwrites it or adds to it). +// It also updates the balance meta info string. func (api *PrivateLightServerAPI) UpdateBalance(id enode.ID, value int64, add bool, meta string) error { return api.server.clientPool.updateBalance(id, value, add, meta) } -// SetClientParams sets client parameters for all clients listed in the ids list or matching the given tags +// SetClientParams sets client parameters for all clients listed in the ids list +// or all connected clients if the list is empty func (api *PrivateLightServerAPI) SetClientParams(ids []enode.ID, params map[string]interface{}) error { var finalErr error api.server.clientPool.forClients(ids, func(client *clientInfo, id enode.ID) { @@ -242,6 +257,7 @@ func (api *PrivateLightServerAPI) SetClientParams(ids []enode.ID, params map[str return finalErr } +// SetDefaultParams sets the default parameters applicable to clients connected in the future func (api *PrivateLightServerAPI) SetDefaultParams(params map[string]interface{}) error { update, err := api.setParams(params, nil, enode.ID{}, &api.defaultPosFactors, &api.defaultNegFactors) if update { diff --git a/les/clientpool.go b/les/clientpool.go index 3985e1a6eb92..19fe321ccb42 100644 --- a/les/clientpool.go +++ b/les/clientpool.go @@ -66,6 +66,7 @@ type clientPool struct { stopCh chan chan struct{} closed bool removePeer func(enode.ID) + eventHook func(string, *clientInfo) queueLimit, countLimit int freeClientCap, capacityLimit, connectedCapacity, priorityConnected uint64 @@ -293,6 +294,9 @@ func (f *clientPool) connect(peer clientPeer, capacity uint64) bool { } clientConnectedMeter.Mark(1) log.Debug("Client accepted", "address", freeID) + if f.eventHook != nil { + f.eventHook("connected", e) + } return true } @@ -361,10 +365,16 @@ func (f *clientPool) dropClient(e *clientInfo, now mclock.AbsTime, kick bool) { if kick { clientKickedMeter.Mark(1) log.Debug("Client kicked out", "address", e.address) + if f.eventHook != nil { + f.eventHook("kicked", e) + } f.removePeer(e.id) } else { clientDisconnectedMeter.Mark(1) log.Debug("Client disconnected", "address", e.address) + if f.eventHook != nil { + f.eventHook("disconnected", e) + } } } @@ -417,6 +427,9 @@ func (f *clientPool) balanceExhausted(id enode.ID) { c.capacity = f.freeClientCap c.peer.updateCapacity(c.capacity) } + if f.eventHook != nil { + f.eventHook("balanceExhausted", c) + } } // setConnLimit sets the maximum number and total capacity of connected clients, @@ -435,6 +448,9 @@ func (f *clientPool) setLimits(count int, totalCap uint64) { return f.connectedCapacity > f.capacityLimit || f.connectedQueue.Size() > f.countLimit }) } + if f.eventHook != nil { + f.eventHook("capacityUpdate", nil) + } } // setCapacity sets the assigned capacity of a connected client