Skip to content

Commit

Permalink
les: added client event hook
Browse files Browse the repository at this point in the history
  • Loading branch information
zsfelfoldi committed Sep 27, 2019
1 parent 37e133a commit 0c7474b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
24 changes: 20 additions & 4 deletions les/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions les/clientpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 0c7474b

Please sign in to comment.