good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
bor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
bor
Commits
32bb2801
Commit
32bb2801
authored
May 2, 2016
by
Felix Lange
Browse files
Options
Downloads
Patches
Plain Diff
p2p: improve readability of dial task scheduling code
parent
1c20313a
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
p2p/server.go
+28
-29
28 additions, 29 deletions
p2p/server.go
p2p/server_test.go
+50
-0
50 additions, 0 deletions
p2p/server_test.go
with
78 additions
and
29 deletions
p2p/server.go
+
28
−
29
View file @
32bb2801
...
...
@@ -400,10 +400,9 @@ func (srv *Server) run(dialstate dialer) {
var
(
peers
=
make
(
map
[
discover
.
NodeID
]
*
Peer
)
trusted
=
make
(
map
[
discover
.
NodeID
]
bool
,
len
(
srv
.
TrustedNodes
))
tasks
[]
task
pendingTasks
[]
task
taskdone
=
make
(
chan
task
,
maxActiveDialTasks
)
runningTasks
[]
task
queuedTasks
[]
task
// tasks that can't run yet
)
// Put trusted nodes into a map to speed up checks.
// Trusted peers are loaded on startup and cannot be
...
...
@@ -412,39 +411,39 @@ func (srv *Server) run(dialstate dialer) {
trusted
[
n
.
ID
]
=
true
}
//
Some task list helpers.
//
removes t from runningTasks
delTask
:=
func
(
t
task
)
{
for
i
:=
range
t
asks
{
if
t
asks
[
i
]
==
t
{
t
asks
=
append
(
t
asks
[
:
i
],
t
asks
[
i
+
1
:
]
...
)
for
i
:=
range
runningT
asks
{
if
runningT
asks
[
i
]
==
t
{
runningT
asks
=
append
(
runningT
asks
[
:
i
],
runningT
asks
[
i
+
1
:
]
...
)
break
}
}
}
scheduleTasks
:=
func
(
new
[]
task
)
{
pt
:=
append
(
pendingTasks
,
new
...
)
start
:=
maxActiveDialTasks
-
len
(
tasks
)
if
len
(
pt
)
<
start
{
start
=
len
(
pt
)
}
if
start
>
0
{
tasks
=
append
(
tasks
,
pt
[
:
start
]
...
)
for
_
,
t
:=
range
pt
[
:
start
]
{
t
:=
t
// starts until max number of active tasks is satisfied
startTasks
:=
func
(
ts
[]
task
)
(
rest
[]
task
)
{
i
:=
0
for
;
len
(
runningTasks
)
<
maxActiveDialTasks
&&
i
<
len
(
ts
);
i
++
{
t
:=
ts
[
i
]
glog
.
V
(
logger
.
Detail
)
.
Infoln
(
"new task:"
,
t
)
go
func
()
{
t
.
Do
(
srv
);
taskdone
<-
t
}()
runningTasks
=
append
(
runningTasks
,
t
)
}
return
ts
[
i
:
]
}
copy
(
pt
,
pt
[
start
:
])
pendingTasks
=
pt
[
:
len
(
pt
)
-
start
]
scheduleTasks
:=
func
()
{
// Start from queue first.
queuedTasks
=
append
(
queuedTasks
[
:
0
],
startTasks
(
queuedTasks
)
...
)
// Query dialer for new tasks and start as many as possible now.
if
len
(
runningTasks
)
<
maxActiveDialTasks
{
nt
:=
dialstate
.
newTasks
(
len
(
runningTasks
)
+
len
(
queuedTasks
),
peers
,
time
.
Now
())
queuedTasks
=
append
(
queuedTasks
,
startTasks
(
nt
)
...
)
}
}
running
:
for
{
// Query the dialer for new tasks and launch them.
now
:=
time
.
Now
()
nt
:=
dialstate
.
newTasks
(
len
(
pendingTasks
)
+
len
(
tasks
),
peers
,
now
)
scheduleTasks
(
nt
)
scheduleTasks
()
select
{
case
<-
srv
.
quit
:
...
...
@@ -466,7 +465,7 @@ running:
// can update its state and remove it from the active
// tasks list.
glog
.
V
(
logger
.
Detail
)
.
Infoln
(
"<-taskdone:"
,
t
)
dialstate
.
taskDone
(
t
,
now
)
dialstate
.
taskDone
(
t
,
time
.
Now
()
)
delTask
(
t
)
case
c
:=
<-
srv
.
posthandshake
:
// A connection has passed the encryption handshake so
...
...
@@ -513,7 +512,7 @@ running:
// Wait for peers to shut down. Pending connections and tasks are
// not handled here and will terminate soon-ish because srv.quit
// is closed.
glog
.
V
(
logger
.
Detail
)
.
Infof
(
"ignoring %d pending tasks at spindown"
,
len
(
t
asks
))
glog
.
V
(
logger
.
Detail
)
.
Infof
(
"ignoring %d pending tasks at spindown"
,
len
(
runningT
asks
))
for
len
(
peers
)
>
0
{
p
:=
<-
srv
.
delpeer
glog
.
V
(
logger
.
Detail
)
.
Infoln
(
"<-delpeer (spindown):"
,
p
)
...
...
This diff is collapsed.
Click to expand it.
p2p/server_test.go
+
50
−
0
View file @
32bb2801
...
...
@@ -235,6 +235,56 @@ func TestServerTaskScheduling(t *testing.T) {
}
}
// This test checks that Server doesn't drop tasks,
// even if newTasks returns more than the maximum number of tasks.
func
TestServerManyTasks
(
t
*
testing
.
T
)
{
alltasks
:=
make
([]
task
,
300
)
for
i
:=
range
alltasks
{
alltasks
[
i
]
=
&
testTask
{
index
:
i
}
}
var
(
srv
=
&
Server
{
quit
:
make
(
chan
struct
{}),
ntab
:
fakeTable
{},
running
:
true
}
done
=
make
(
chan
*
testTask
)
start
,
end
=
0
,
0
)
defer
srv
.
Stop
()
srv
.
loopWG
.
Add
(
1
)
go
srv
.
run
(
taskgen
{
newFunc
:
func
(
running
int
,
peers
map
[
discover
.
NodeID
]
*
Peer
)
[]
task
{
start
,
end
=
end
,
end
+
maxActiveDialTasks
+
10
if
end
>
len
(
alltasks
)
{
end
=
len
(
alltasks
)
}
return
alltasks
[
start
:
end
]
},
doneFunc
:
func
(
tt
task
)
{
done
<-
tt
.
(
*
testTask
)
},
})
doneset
:=
make
(
map
[
int
]
bool
)
timeout
:=
time
.
After
(
2
*
time
.
Second
)
for
len
(
doneset
)
<
len
(
alltasks
)
{
select
{
case
tt
:=
<-
done
:
if
doneset
[
tt
.
index
]
{
t
.
Errorf
(
"task %d got done more than once"
,
tt
.
index
)
}
else
{
doneset
[
tt
.
index
]
=
true
}
case
<-
timeout
:
t
.
Errorf
(
"%d of %d tasks got done within 2s"
,
len
(
doneset
),
len
(
alltasks
))
for
i
:=
0
;
i
<
len
(
alltasks
);
i
++
{
if
!
doneset
[
i
]
{
t
.
Logf
(
"task %d not done"
,
i
)
}
}
return
}
}
}
type
taskgen
struct
{
newFunc
func
(
running
int
,
peers
map
[
discover
.
NodeID
]
*
Peer
)
[]
task
doneFunc
func
(
task
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment