github.com/TeaOSLab/EdgeNode@v1.3.8/internal/iplibrary/server_list_manager.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package iplibrary
     4  
     5  import "sync"
     6  
     7  var SharedServerListManager = NewServerListManager()
     8  
     9  // ServerListManager 服务相关名单
    10  type ServerListManager struct {
    11  	whiteMap map[int64]*IPList // serverId => *List
    12  	blackMap map[int64]*IPList // serverId => *List
    13  
    14  	locker sync.RWMutex
    15  }
    16  
    17  func NewServerListManager() *ServerListManager {
    18  	return &ServerListManager{
    19  		whiteMap: map[int64]*IPList{},
    20  		blackMap: map[int64]*IPList{},
    21  	}
    22  }
    23  
    24  func (this *ServerListManager) FindWhiteList(serverId int64, autoCreate bool) *IPList {
    25  	this.locker.RLock()
    26  	list, ok := this.whiteMap[serverId]
    27  	this.locker.RUnlock()
    28  	if ok {
    29  		return list
    30  	}
    31  
    32  	if autoCreate {
    33  		list = NewIPList()
    34  		this.locker.Lock()
    35  		this.whiteMap[serverId] = list
    36  		this.locker.Unlock()
    37  
    38  		return list
    39  	}
    40  	return nil
    41  }
    42  
    43  func (this *ServerListManager) FindBlackList(serverId int64, autoCreate bool) *IPList {
    44  	this.locker.RLock()
    45  	list, ok := this.blackMap[serverId]
    46  	this.locker.RUnlock()
    47  	if ok {
    48  		return list
    49  	}
    50  
    51  	if autoCreate {
    52  		list = NewIPList()
    53  		this.locker.Lock()
    54  		this.blackMap[serverId] = list
    55  		this.locker.Unlock()
    56  
    57  		return list
    58  	}
    59  
    60  	return nil
    61  }
    62  
    63  func (this *ServerListManager) BlackMap() map[int64]*IPList {
    64  	return this.blackMap
    65  }