mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-07 18:50:27 +08:00
优化代码
This commit is contained in:
@@ -169,6 +169,10 @@ func (this *IPList) sortItems() {
|
|||||||
|
|
||||||
// 不加锁的情况下查找Item
|
// 不加锁的情况下查找Item
|
||||||
func (this *IPList) lookupIP(ip uint64) *IPItem {
|
func (this *IPList) lookupIP(ip uint64) *IPItem {
|
||||||
|
if len(this.sortedItems) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var count = len(this.sortedItems)
|
var count = len(this.sortedItems)
|
||||||
var resultIndex = -1
|
var resultIndex = -1
|
||||||
sort.Search(count, func(i int) bool {
|
sort.Search(count, func(i int) bool {
|
||||||
|
|||||||
@@ -302,16 +302,20 @@ func TestTooManyLists(t *testing.T) {
|
|||||||
func BenchmarkIPList_Contains(b *testing.B) {
|
func BenchmarkIPList_Contains(b *testing.B) {
|
||||||
runtime.GOMAXPROCS(1)
|
runtime.GOMAXPROCS(1)
|
||||||
|
|
||||||
list := NewIPList()
|
var list = NewIPList()
|
||||||
for i := 1; i < 194; i++ {
|
for i := 1; i < 200_000; i++ {
|
||||||
list.Add(&IPItem{
|
list.AddDelay(&IPItem{
|
||||||
Id: int64(i),
|
Id: int64(i),
|
||||||
IPFrom: utils.IP2Long(strconv.Itoa(i%255) + "." + strconv.Itoa(i%255) + ".0.1"),
|
IPFrom: utils.IP2Long(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||||
IPTo: utils.IP2Long(strconv.Itoa(i%255) + "." + strconv.Itoa(i%255) + ".0.1"),
|
IPTo: utils.IP2Long(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||||
ExpiredAt: time.Now().Unix() + 60,
|
ExpiredAt: time.Now().Unix() + 60,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
list.Sort()
|
||||||
|
|
||||||
b.Log(len(list.itemsMap), "ip")
|
b.Log(len(list.itemsMap), "ip")
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_ = list.Contains(utils.IP2Long("192.168.1.100"))
|
_ = list.Contains(utils.IP2Long("192.168.1.100"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,8 +50,10 @@ func (this *ClientListener) Accept() (net.Conn, error) {
|
|||||||
ip, _, err := net.SplitHostPort(conn.RemoteAddr().String())
|
ip, _, err := net.SplitHostPort(conn.RemoteAddr().String())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
canGoNext, _ := iplibrary.AllowIP(ip, 0)
|
canGoNext, _ := iplibrary.AllowIP(ip, 0)
|
||||||
if !canGoNext || (!waf.SharedIPWhiteList.Contains(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, ip) &&
|
|
||||||
waf.SharedIPBlackList.Contains(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, ip)) {
|
if !canGoNext ||
|
||||||
|
(!waf.SharedIPWhiteList.Contains(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, ip) &&
|
||||||
|
waf.SharedIPBlackList.Contains(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, ip)) {
|
||||||
tcpConn, ok := conn.(*net.TCPConn)
|
tcpConn, ok := conn.(*net.TCPConn)
|
||||||
if ok {
|
if ok {
|
||||||
_ = tcpConn.SetLinger(0)
|
_ = tcpConn.SetLinger(0)
|
||||||
|
|||||||
Reference in New Issue
Block a user