diff --git a/les/api.go b/les/api.go index 1b5c14365e..5998ca7462 100644 --- a/les/api.go +++ b/les/api.go @@ -31,23 +31,23 @@ var ( // PublicLesServerAPI provides an API to access the les server. // It offers only methods that operate on public data that is freely available to anyone. type PrivateLesServerAPI struct { - server *LesServer - pm *ProtocolManager - vip *vipClientPool + server *LesServer + pm *ProtocolManager + priority *priorityClientPool } // NewPublicLesServerAPI creates a new les server API. func NewPrivateLesServerAPI(server *LesServer) *PrivateLesServerAPI { - vip := &vipClientPool{ - clients: make(map[enode.ID]vipClientInfo), + priority := &priorityClientPool{ + clients: make(map[enode.ID]priorityClientInfo), totalCap: server.totalCapacity, - pm: server.protocolManager, + pm: server.protocolManager, } - server.protocolManager.vipClientPool = vip + server.protocolManager.priorityClientPool = priority return &PrivateLesServerAPI{ - server: server, - pm: server.protocolManager, - vip: vip, + server: server, + pm: server.protocolManager, + priority: priority, } } @@ -61,20 +61,20 @@ func (api *PrivateLesServerAPI) MinimumCapacity() hexutil.Uint64 { return hexutil.Uint64(api.server.minCapacity) } -// vipClientPool stores information about prioritized clients -type vipClientPool struct { - lock sync.Mutex - pm *ProtocolManager - clients map[enode.ID]vipClientInfo - totalCap, totalVipCap, totalConnectedCap uint64 - vipCount int +// priorityClientPool stores information about prioritized clients +type priorityClientPool struct { + lock sync.Mutex + pm *ProtocolManager + clients map[enode.ID]priorityClientInfo + totalCap, totalpriorityCap, totalConnectedCap uint64 + priorityCount int } -// vipClientInfo entries exist for all prioritized clients and currently connected free clients -type vipClientInfo struct { - cap uint64 // zero for non-vip clients +// priorityClientInfo entries exist for all prioritized clients and currently connected free clients +type priorityClientInfo struct { + cap uint64 // zero for non-priority clients connected bool - updateCap func(uint64) + updateCap func(uint64) } // SetClientCapacity sets the priority capacity assigned to a given client. @@ -89,45 +89,45 @@ func (api *PrivateLesServerAPI) SetClientCapacity(id enode.ID, cap uint64) error return ErrMinCap } - api.vip.lock.Lock() - defer api.vip.lock.Unlock() + api.priority.lock.Lock() + defer api.priority.lock.Unlock() - c := api.vip.clients[id] - if api.vip.totalVipCap+cap > api.vip.totalCap+c.cap { + c := api.priority.clients[id] + if api.priority.totalpriorityCap+cap > api.priority.totalCap+c.cap { return ErrTotalCap } - api.vip.totalVipCap += cap - c.cap + api.priority.totalpriorityCap += cap - c.cap if c.updateCap != nil && cap != 0 { c.updateCap(cap) } if c.connected { if c.cap != 0 { - api.vip.vipCount-- + api.priority.priorityCount-- } if cap != 0 { - api.vip.vipCount++ + api.priority.priorityCount++ } - api.vip.totalConnectedCap += cap - c.cap - api.pm.clientPool.setConnLimit(api.pm.maxFreePeers(api.vip.vipCount, api.vip.totalConnectedCap)) + api.priority.totalConnectedCap += cap - c.cap + api.pm.clientPool.setConnLimit(api.pm.maxFreePeers(api.priority.priorityCount, api.priority.totalConnectedCap)) } if c.updateCap != nil && cap == 0 { c.updateCap(cap) } if cap != 0 || c.connected { c.cap = cap - api.vip.clients[id] = c + api.priority.clients[id] = c } else { - delete(api.vip.clients, id) + delete(api.priority.clients, id) } return nil } // GetClientCapacity returns the capacity assigned to a given client func (api *PrivateLesServerAPI) GetClientCapacity(id enode.ID) hexutil.Uint64 { - api.vip.lock.Lock() - defer api.vip.lock.Unlock() + api.priority.lock.Lock() + 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 @@ -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 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 // 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() defer v.lock.Unlock() @@ -150,16 +150,16 @@ func (v *vipClientPool) connect(id enode.ID, updateCap func(uint64)) (uint64, bo c.updateCap = updateCap v.clients[id] = c if c.cap != 0 { - v.vipCount++ + v.priorityCount++ } 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 } // disconnect should be called when a client is disconnected. // 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() defer v.lock.Unlock() @@ -167,10 +167,10 @@ func (v *vipClientPool) disconnect(id enode.ID) { c.connected = false if c.cap != 0 { v.clients[id] = c - v.vipCount-- + v.priorityCount-- } else { delete(v.clients, id) } 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)) } diff --git a/les/handler.go b/les/handler.go index 907aea2a31..c6314d67bf 100644 --- a/les/handler.go +++ b/les/handler.go @@ -104,7 +104,7 @@ type ProtocolManager struct { serverPool *serverPool clientPool *freeClientPool freeClientCap uint64 - vipClientPool *vipClientPool + priorityClientPool *priorityClientPool lesTopic discv5.Topic reqDist *requestDistributor retriever *retrieveManager @@ -342,8 +342,8 @@ func (pm *ProtocolManager) handle(p *peer) error { } var ( - free, vip bool - lock sync.Mutex // lock protects access to the free and vip flags + free, priority bool + lock sync.Mutex // lock protects access to the free and priority flags ) defer func() { @@ -358,16 +358,16 @@ func (pm *ProtocolManager) handle(p *peer) error { lock.Lock() defer lock.Unlock() - if !vip && cap != 0 { - // switch to vip mode + if !priority && cap != 0 { + // switch to priority mode if free { pm.clientPool.disconnect(freeId) free = false } - vip = true + priority = true p.updateCapacity(cap) } - if vip { + if priority { if cap == 0 { // priority revoked; switch to free client mode or drop if freeId != "" { @@ -377,33 +377,33 @@ func (pm *ProtocolManager) handle(p *peer) error { } free = true } - vip = false + priority = false p.updateCapacity(pm.freeClientCap) } else { - // just update vip capacity + // just update priority capacity p.updateCapacity(cap) } } } lock.Lock() - if pm.vipClientPool != nil { - // the vip client pool registers currently connected non-vip clients too + if pm.priorityClientPool != nil { + // 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 - vipCap, ok := pm.vipClientPool.connect(p.ID(), updateCap) + priorityCap, ok := pm.priorityClientPool.connect(p.ID(), updateCap) if !ok { lock.Unlock() return p2p.DiscAlreadyConnected } // always unregister - defer pm.vipClientPool.disconnect(p.ID()) - if vipCap != 0 { - vip = true - p.updateCapacity(vipCap) + defer pm.priorityClientPool.disconnect(p.ID()) + if priorityCap != 0 { + priority = true + p.updateCapacity(priorityCap) } } - if !vip && freeId != "" { + if !priority && freeId != "" { // if freeId == "" then we are in test mode and let the client connect // without entering the free client pool if !pm.clientPool.connect(freeId, func() { go pm.removePeer(p.id) }) {