Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Nomad
Commits
3ee692cb
Commit
3ee692cb
authored
6 years ago
by
Chris Baker
Browse files
Options
Download
Email Patches
Plain Diff
some changes for more idiomatic code
parent
33b9f9d3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
nomad/rpc.go
+25
-19
nomad/rpc.go
with
25 additions
and
19 deletions
+25
-19
nomad/rpc.go
+
25
-
19
View file @
3ee692cb
...
...
@@ -49,9 +49,8 @@ const (
type
rpcHandler
struct
{
*
Server
logger
log
.
Logger
gologger
*
golog
.
Logger
acceptLoopDelay
time
.
Duration
logger
log
.
Logger
gologger
*
golog
.
Logger
}
func
newRpcHandler
(
s
*
Server
)
*
rpcHandler
{
...
...
@@ -85,6 +84,8 @@ type RPCContext struct {
// listen is used to listen for incoming RPC connections
func
(
r
*
rpcHandler
)
listen
(
ctx
context
.
Context
)
{
defer
close
(
r
.
listenerCh
)
var
acceptLoopDelay
time
.
Duration
for
{
select
{
case
<-
ctx
.
Done
()
:
...
...
@@ -99,41 +100,46 @@ func (r *rpcHandler) listen(ctx context.Context) {
if
r
.
shutdown
{
return
}
r
.
handleAcceptErr
(
err
,
ctx
)
r
.
handleAcceptErr
(
ctx
,
err
,
&
acceptLoopDelay
)
continue
}
// No error, reset loop delay
r
.
acceptLoopDelay
=
0
acceptLoopDelay
=
0
go
r
.
handleConn
(
ctx
,
conn
,
&
RPCContext
{
Conn
:
conn
})
metrics
.
IncrCounter
([]
string
{
"nomad"
,
"rpc"
,
"accept_conn"
},
1
)
}
}
// Sleep to avoid spamming the log, with a maximum delay according to whether or not the error is temporary
func
(
r
*
rpcHandler
)
handleAcceptErr
(
err
error
,
ctx
context
.
Context
)
{
const
baseAcceptLoopDelay
=
5
*
time
.
Millisecond
const
maxAcceptLoopDelay
=
5
*
time
.
Second
const
maxAcceptLoopDelayTemporaryError
=
1
*
time
.
Second
// handleAcceptErr sleeps to avoid spamming the log,
// with a maximum delay according to whether or not the error is temporary
func
(
r
*
rpcHandler
)
handleAcceptErr
(
ctx
context
.
Context
,
err
error
,
loopDelay
*
time
.
Duration
)
{
const
baseDelay
=
5
*
time
.
Millisecond
const
maxDelayPerm
=
5
*
time
.
Second
const
maxDelayTemp
=
1
*
time
.
Second
if
r
.
acceptL
oopDelay
==
0
{
r
.
acceptL
oopDelay
=
base
AcceptLoop
Delay
if
*
l
oopDelay
==
0
{
*
l
oopDelay
=
baseDelay
}
else
{
r
.
acceptL
oopDelay
*=
2
*
l
oopDelay
*=
2
}
temporaryError
:=
false
if
ne
,
ok
:=
err
.
(
net
.
Error
);
ok
&&
ne
.
Temporary
()
{
temporaryError
=
true
}
if
temporaryError
&&
r
.
acceptLoopDelay
>
maxAcceptLoopDelayTemporaryError
{
r
.
acceptLoopDelay
=
maxAcceptLoopDelayTemporaryError
}
else
if
r
.
acceptLoopDelay
>
maxAcceptLoopDelay
{
r
.
acceptLoopDelay
=
maxAcceptLoopDelay
if
temporaryError
&&
*
loopDelay
>
maxDelayTemp
{
*
loopDelay
=
maxDelayTemp
}
else
if
*
loopDelay
>
maxDelayPerm
{
*
loopDelay
=
maxDelayPerm
}
r
.
logger
.
Error
(
"failed to accept RPC conn"
,
"error"
,
err
,
"delay"
,
r
.
acceptLoopDelay
)
r
.
logger
.
Error
(
"failed to accept RPC conn"
,
"error"
,
err
,
"delay"
,
*
loopDelay
)
select
{
case
<-
ctx
.
Done
()
:
case
<-
time
.
After
(
r
.
acceptL
oopDelay
)
:
case
<-
time
.
After
(
*
l
oopDelay
)
:
}
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
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