OSDN Git Service

Merge pull request #779 from Bytom/p2p_test
[bytom/bytom.git] / p2p / ip_range_counter.go
1 package p2p
2
3 import (
4         "strings"
5 )
6
7 // TODO Test
8 func AddToIPRangeCounts(counts map[string]int, ip string) map[string]int {
9         changes := make(map[string]int)
10         ipParts := strings.Split(ip, ":")
11         for i := 1; i < len(ipParts); i++ {
12                 prefix := strings.Join(ipParts[:i], ":")
13                 counts[prefix] += 1
14                 changes[prefix] = counts[prefix]
15         }
16         return changes
17 }
18
19 // TODO Test
20 func CheckIPRangeCounts(counts map[string]int, limits []int) bool {
21         for prefix, count := range counts {
22                 ipParts := strings.Split(prefix, ":")
23                 numParts := len(ipParts)
24                 if limits[numParts] < count {
25                         return false
26                 }
27         }
28         return true
29 }