Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
lbry
ytsync
Commits
43f745ae
Commit
43f745ae
authored
Jul 12, 2019
by
ci
Browse files
improve ip shuffling (prevent ips in use from being dealt)
parent
8c2a8262
Changes
1
Hide whitespace changes
Inline
Side-by-side
ipManager/throttle.go
View file @
43f745ae
...
...
@@ -18,6 +18,7 @@ const unbanTimeout = 3 * time.Hour
var
ipv6Pool
[]
string
var
ipv4Pool
[]
string
var
throttledIPs
map
[
string
]
bool
var
ipInUse
map
[
string
]
bool
var
ipLastUsed
map
[
string
]
time
.
Time
var
ipMutex
sync
.
Mutex
var
stopper
=
stop
.
New
()
...
...
@@ -27,6 +28,7 @@ func GetNextIP(ipv6 bool) (string, error) {
defer
ipMutex
.
Unlock
()
if
len
(
ipv4Pool
)
<
1
||
len
(
ipv6Pool
)
<
1
{
throttledIPs
=
make
(
map
[
string
]
bool
)
ipInUse
=
make
(
map
[
string
]
bool
)
ipLastUsed
=
make
(
map
[
string
]
time
.
Time
)
addrs
,
err
:=
net
.
InterfaceAddrs
()
if
err
!=
nil
{
...
...
@@ -37,8 +39,10 @@ func GetNextIP(ipv6 bool) (string, error) {
if
ipnet
,
ok
:=
address
.
(
*
net
.
IPNet
);
ok
&&
!
ipnet
.
IP
.
IsLoopback
()
{
if
ipnet
.
IP
.
To16
()
!=
nil
&&
govalidator
.
IsIPv6
(
ipnet
.
IP
.
String
())
{
ipv6Pool
=
append
(
ipv6Pool
,
ipnet
.
IP
.
String
())
ipLastUsed
[
ipnet
.
IP
.
String
()]
=
time
.
Now
()
.
Add
(
-
IPCooldownPeriod
)
}
else
if
ipnet
.
IP
.
To4
()
!=
nil
&&
govalidator
.
IsIPv4
(
ipnet
.
IP
.
String
())
{
ipv4Pool
=
append
(
ipv4Pool
,
ipnet
.
IP
.
String
())
ipLastUsed
[
ipnet
.
IP
.
String
()]
=
time
.
Now
()
.
Add
(
-
IPCooldownPeriod
)
}
}
}
...
...
@@ -54,21 +58,32 @@ func GetNextIP(ipv6 bool) (string, error) {
}
lastUse
:=
ipLastUsed
[
nextIP
]
if
time
.
Since
(
lastUse
)
<
IPCooldownPeriod
{
log
.
Debugf
(
"The IP %s is too hot, waiting for %.1f seconds before continuing"
,
nextIP
,
(
IPCooldownPeriod
-
time
.
Since
(
lastUse
))
.
Seconds
())
time
.
Sleep
(
IPCooldownPeriod
-
time
.
Since
(
lastUse
))
}
ip
Last
Use
d
[
nextIP
]
=
t
ime
.
Now
()
ip
In
Use
[
nextIP
]
=
t
rue
return
nextIP
,
nil
}
func
releaseIP
(
ip
string
)
{
ipMutex
.
Lock
()
defer
ipMutex
.
Unlock
()
ipLastUsed
[
ip
]
=
time
.
Now
()
ipInUse
[
ip
]
=
false
}
func
getLeastUsedIP
(
ipPool
[]
string
)
string
{
nextIP
:=
""
veryLastUse
:=
time
.
Now
()
for
_
,
ip
:=
range
ipPool
{
isThrottled
,
_
:=
throttledIPs
[
ip
]
isThrottled
:=
throttledIPs
[
ip
]
if
isThrottled
{
continue
}
inUse
:=
ipInUse
[
ip
]
if
inUse
{
continue
}
lastUse
:=
ipLastUsed
[
ip
]
if
lastUse
.
Before
(
veryLastUse
)
{
nextIP
=
ip
...
...
@@ -81,7 +96,7 @@ func getLeastUsedIP(ipPool []string) string {
func
SetIpThrottled
(
ip
string
,
stopGrp
*
stop
.
Group
)
{
ipMutex
.
Lock
()
defer
ipMutex
.
Unlock
()
isThrottled
,
_
:=
throttledIPs
[
ip
]
isThrottled
:=
throttledIPs
[
ip
]
if
isThrottled
{
return
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment