les: renamed vip to priority

This commit is contained in:
Zsolt Felfoldi 2019-01-16 15:44:21 +01:00
parent 8fcb060f10
commit 2b2fe5350f
2 changed files with 59 additions and 59 deletions

View file

@ -33,21 +33,21 @@ var (
type PrivateLesServerAPI struct { type PrivateLesServerAPI struct {
server *LesServer server *LesServer
pm *ProtocolManager pm *ProtocolManager
vip *vipClientPool priority *priorityClientPool
} }
// NewPublicLesServerAPI creates a new les server API. // NewPublicLesServerAPI creates a new les server API.
func NewPrivateLesServerAPI(server *LesServer) *PrivateLesServerAPI { func NewPrivateLesServerAPI(server *LesServer) *PrivateLesServerAPI {
vip := &vipClientPool{ priority := &priorityClientPool{
clients: make(map[enode.ID]vipClientInfo), clients: make(map[enode.ID]priorityClientInfo),
totalCap: server.totalCapacity, totalCap: server.totalCapacity,
pm: server.protocolManager, pm: server.protocolManager,
} }
server.protocolManager.vipClientPool = vip server.protocolManager.priorityClientPool = priority
return &PrivateLesServerAPI{ return &PrivateLesServerAPI{
server: server, server: server,
pm: server.protocolManager, pm: server.protocolManager,
vip: vip, priority: priority,
} }
} }
@ -61,18 +61,18 @@ func (api *PrivateLesServerAPI) MinimumCapacity() hexutil.Uint64 {
return hexutil.Uint64(api.server.minCapacity) return hexutil.Uint64(api.server.minCapacity)
} }
// vipClientPool stores information about prioritized clients // priorityClientPool stores information about prioritized clients
type vipClientPool struct { type priorityClientPool struct {
lock sync.Mutex lock sync.Mutex
pm *ProtocolManager pm *ProtocolManager
clients map[enode.ID]vipClientInfo clients map[enode.ID]priorityClientInfo
totalCap, totalVipCap, totalConnectedCap uint64 totalCap, totalpriorityCap, totalConnectedCap uint64
vipCount int priorityCount int
} }
// vipClientInfo entries exist for all prioritized clients and currently connected free clients // priorityClientInfo entries exist for all prioritized clients and currently connected free clients
type vipClientInfo struct { type priorityClientInfo struct {
cap uint64 // zero for non-vip clients cap uint64 // zero for non-priority clients
connected bool connected bool
updateCap func(uint64) updateCap func(uint64)
} }
@ -89,45 +89,45 @@ func (api *PrivateLesServerAPI) SetClientCapacity(id enode.ID, cap uint64) error
return ErrMinCap return ErrMinCap
} }
api.vip.lock.Lock() api.priority.lock.Lock()
defer api.vip.lock.Unlock() defer api.priority.lock.Unlock()
c := api.vip.clients[id] c := api.priority.clients[id]
if api.vip.totalVipCap+cap > api.vip.totalCap+c.cap { if api.priority.totalpriorityCap+cap > api.priority.totalCap+c.cap {
return ErrTotalCap return ErrTotalCap
} }
api.vip.totalVipCap += cap - c.cap api.priority.totalpriorityCap += cap - c.cap
if c.updateCap != nil && cap != 0 { if c.updateCap != nil && cap != 0 {
c.updateCap(cap) c.updateCap(cap)
} }
if c.connected { if c.connected {
if c.cap != 0 { if c.cap != 0 {
api.vip.vipCount-- api.priority.priorityCount--
} }
if cap != 0 { if cap != 0 {
api.vip.vipCount++ api.priority.priorityCount++
} }
api.vip.totalConnectedCap += cap - c.cap api.priority.totalConnectedCap += cap - c.cap
api.pm.clientPool.setConnLimit(api.pm.maxFreePeers(api.vip.vipCount, api.vip.totalConnectedCap)) api.pm.clientPool.setConnLimit(api.pm.maxFreePeers(api.priority.priorityCount, api.priority.totalConnectedCap))
} }
if c.updateCap != nil && cap == 0 { if c.updateCap != nil && cap == 0 {
c.updateCap(cap) c.updateCap(cap)
} }
if cap != 0 || c.connected { if cap != 0 || c.connected {
c.cap = cap c.cap = cap
api.vip.clients[id] = c api.priority.clients[id] = c
} else { } else {
delete(api.vip.clients, id) delete(api.priority.clients, id)
} }
return nil return nil
} }
// GetClientCapacity returns the capacity assigned to a given client // GetClientCapacity returns the capacity assigned to a given client
func (api *PrivateLesServerAPI) GetClientCapacity(id enode.ID) hexutil.Uint64 { func (api *PrivateLesServerAPI) GetClientCapacity(id enode.ID) hexutil.Uint64 {
api.vip.lock.Lock() api.priority.lock.Lock()
defer api.vip.lock.Unlock() defer api.priority.lock.Unlock()
return hexutil.Uint64(api.vip.clients[id].cap) return hexutil.Uint64(api.priority.clients[id].cap)
} }
// connect should be called when a new client is connected. The callback function // connect should be called when a new client is connected. The callback function
@ -135,10 +135,10 @@ func (api *PrivateLesServerAPI) GetClientCapacity(id enode.ID) hexutil.Uint64 {
// It returns the priority capacity or zero if the client is not prioritized. // It returns the priority capacity or zero if the client is not prioritized.
// It also returns whether the client can be accepted. // It also returns whether the client can be accepted.
// //
// Note: vipClientPool also stores a record about free clients while they are // Note: priorityClientPool also stores a record about free clients while they are
// connected in order to be able to assign priority to them later with the callback // connected in order to be able to assign priority to them later with the callback
// function if necessary. // function if necessary.
func (v *vipClientPool) connect(id enode.ID, updateCap func(uint64)) (uint64, bool) { func (v *priorityClientPool) connect(id enode.ID, updateCap func(uint64)) (uint64, bool) {
v.lock.Lock() v.lock.Lock()
defer v.lock.Unlock() defer v.lock.Unlock()
@ -150,16 +150,16 @@ func (v *vipClientPool) connect(id enode.ID, updateCap func(uint64)) (uint64, bo
c.updateCap = updateCap c.updateCap = updateCap
v.clients[id] = c v.clients[id] = c
if c.cap != 0 { if c.cap != 0 {
v.vipCount++ v.priorityCount++
} }
v.totalConnectedCap += c.cap v.totalConnectedCap += c.cap
v.pm.clientPool.setConnLimit(v.pm.maxFreePeers(v.vipCount, v.totalConnectedCap)) v.pm.clientPool.setConnLimit(v.pm.maxFreePeers(v.priorityCount, v.totalConnectedCap))
return c.cap, true return c.cap, true
} }
// disconnect should be called when a client is disconnected. // disconnect should be called when a client is disconnected.
// It should be called for all clients accepted by connect even if not prioritized. // It should be called for all clients accepted by connect even if not prioritized.
func (v *vipClientPool) disconnect(id enode.ID) { func (v *priorityClientPool) disconnect(id enode.ID) {
v.lock.Lock() v.lock.Lock()
defer v.lock.Unlock() defer v.lock.Unlock()
@ -167,10 +167,10 @@ func (v *vipClientPool) disconnect(id enode.ID) {
c.connected = false c.connected = false
if c.cap != 0 { if c.cap != 0 {
v.clients[id] = c v.clients[id] = c
v.vipCount-- v.priorityCount--
} else { } else {
delete(v.clients, id) delete(v.clients, id)
} }
v.totalConnectedCap -= c.cap v.totalConnectedCap -= c.cap
v.pm.clientPool.setConnLimit(v.pm.maxFreePeers(v.vipCount, v.totalConnectedCap)) v.pm.clientPool.setConnLimit(v.pm.maxFreePeers(v.priorityCount, v.totalConnectedCap))
} }

View file

@ -104,7 +104,7 @@ type ProtocolManager struct {
serverPool *serverPool serverPool *serverPool
clientPool *freeClientPool clientPool *freeClientPool
freeClientCap uint64 freeClientCap uint64
vipClientPool *vipClientPool priorityClientPool *priorityClientPool
lesTopic discv5.Topic lesTopic discv5.Topic
reqDist *requestDistributor reqDist *requestDistributor
retriever *retrieveManager retriever *retrieveManager
@ -342,8 +342,8 @@ func (pm *ProtocolManager) handle(p *peer) error {
} }
var ( var (
free, vip bool free, priority bool
lock sync.Mutex // lock protects access to the free and vip flags lock sync.Mutex // lock protects access to the free and priority flags
) )
defer func() { defer func() {
@ -358,16 +358,16 @@ func (pm *ProtocolManager) handle(p *peer) error {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()
if !vip && cap != 0 { if !priority && cap != 0 {
// switch to vip mode // switch to priority mode
if free { if free {
pm.clientPool.disconnect(freeId) pm.clientPool.disconnect(freeId)
free = false free = false
} }
vip = true priority = true
p.updateCapacity(cap) p.updateCapacity(cap)
} }
if vip { if priority {
if cap == 0 { if cap == 0 {
// priority revoked; switch to free client mode or drop // priority revoked; switch to free client mode or drop
if freeId != "" { if freeId != "" {
@ -377,33 +377,33 @@ func (pm *ProtocolManager) handle(p *peer) error {
} }
free = true free = true
} }
vip = false priority = false
p.updateCapacity(pm.freeClientCap) p.updateCapacity(pm.freeClientCap)
} else { } else {
// just update vip capacity // just update priority capacity
p.updateCapacity(cap) p.updateCapacity(cap)
} }
} }
} }
lock.Lock() lock.Lock()
if pm.vipClientPool != nil { if pm.priorityClientPool != nil {
// the vip client pool registers currently connected non-vip clients too // the priority client pool registers currently connected non-priority clients too
// in order to be able to notify them if they get priority while connected // in order to be able to notify them if they get priority while connected
vipCap, ok := pm.vipClientPool.connect(p.ID(), updateCap) priorityCap, ok := pm.priorityClientPool.connect(p.ID(), updateCap)
if !ok { if !ok {
lock.Unlock() lock.Unlock()
return p2p.DiscAlreadyConnected return p2p.DiscAlreadyConnected
} }
// always unregister // always unregister
defer pm.vipClientPool.disconnect(p.ID()) defer pm.priorityClientPool.disconnect(p.ID())
if vipCap != 0 { if priorityCap != 0 {
vip = true priority = true
p.updateCapacity(vipCap) p.updateCapacity(priorityCap)
} }
} }
if !vip && freeId != "" { if !priority && freeId != "" {
// if freeId == "" then we are in test mode and let the client connect // if freeId == "" then we are in test mode and let the client connect
// without entering the free client pool // without entering the free client pool
if !pm.clientPool.connect(freeId, func() { go pm.removePeer(p.id) }) { if !pm.clientPool.connect(freeId, func() { go pm.removePeer(p.id) }) {