github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/vntp2p/blacklist.go (about)

     1  package vntp2p
     2  
     3  import (
     4  	"github.com/libp2p/go-libp2p-peer"
     5  	"github.com/vntchain/go-vnt/log"
     6  	"github.com/bluele/gcache"
     7  	"time"
     8  )
     9  
    10  type BlackList struct {
    11  	cache	gcache.Cache
    12  	rwPid	chan peer.ID
    13  }
    14  
    15  func NewPeerBlackList() *BlackList {
    16  	blacklist := gcache.New(1024).LRU().Expiration(30 * time.Second).Build()
    17  
    18  	return &BlackList{
    19  		blacklist,
    20  		make(chan peer.ID),
    21  	}
    22  }
    23  
    24  var blacklist = NewPeerBlackList()
    25  
    26  func (b *BlackList) write(pid peer.ID) {
    27  	b.rwPid <- pid
    28  }
    29  
    30  func (b *BlackList) run() {
    31  	for {
    32  		select {
    33  		case pid := <- b.rwPid:
    34  			log.Info("Add to blacklist:", "pid", pid)
    35  			if !b.cache.Has(pid) {
    36  				b.cache.Set(pid, true)
    37  			}
    38  		}
    39  	}
    40  }
    41  
    42  func (b *BlackList) exists(pid peer.ID) bool {
    43  	return b.cache.Has(pid)
    44  }