Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
comms
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
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
xx network
comms
Commits
72b66ed2
Commit
72b66ed2
authored
Sep 11, 2020
by
Benjamin Wenger
Browse files
Options
Downloads
Patches
Plain Diff
fixed improper access of the address field of host
parent
7cf66475
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
connect/host.go
+20
-13
20 additions, 13 deletions
connect/host.go
connect/host_test.go
+5
-4
5 additions, 4 deletions
connect/host_test.go
connect/manager.go
+2
-0
2 additions, 0 deletions
connect/manager.go
connect/manager_test.go
+0
-1
0 additions, 1 deletion
connect/manager_test.go
with
27 additions
and
18 deletions
connect/host.go
+
20
−
13
View file @
72b66ed2
...
...
@@ -24,6 +24,7 @@ import (
"math"
"net"
"sync"
"sync/atomic"
"testing"
"time"
)
...
...
@@ -45,7 +46,7 @@ type Host struct {
id
*
id
.
ID
// address:Port being connected to
address
string
address
Atomic
atomic
.
Value
// PEM-format TLS Certificate
certificate
[]
byte
...
...
@@ -89,13 +90,14 @@ func NewHost(id *id.ID, address string, cert []byte, disableTimeout,
// Initialize the Host object
host
=
&
Host
{
id
:
id
,
address
:
address
,
certificate
:
cert
,
enableAuth
:
enableAuth
,
transmissionToken
:
token
.
NewLive
(),
receptionToken
:
token
.
NewLive
(),
}
host
.
UpdateAddress
(
address
)
// Set the max number of retries for establishing a connection
if
disableTimeout
{
host
.
maxRetries
=
math
.
MaxInt32
...
...
@@ -155,12 +157,16 @@ func (h *Host) GetId() *id.ID {
// GetAddress returns the address of the host.
func
(
h
*
Host
)
GetAddress
()
string
{
return
h
.
address
a
:=
h
.
addressAtomic
.
Load
()
if
a
==
nil
{
return
""
}
return
a
.
(
string
)
}
// UpdateAddress updates the address of the host
func
(
h
*
Host
)
UpdateAddress
(
address
string
)
{
h
.
address
=
address
h
.
address
Atomic
.
Store
(
address
)
}
// Disconnect closes a the Host connection under the write lock
...
...
@@ -256,7 +262,7 @@ func (h *Host) disconnect() {
err
:=
h
.
connection
.
Close
()
if
err
!=
nil
{
jww
.
ERROR
.
Printf
(
"Unable to close connection to %s: %+v"
,
h
.
a
ddress
,
errors
.
New
(
err
.
Error
()))
h
.
GetA
ddress
()
,
errors
.
New
(
err
.
Error
()))
}
else
{
h
.
connection
=
nil
}
...
...
@@ -275,19 +281,19 @@ func (h *Host) connectHelper() (err error) {
securityDial
=
grpc
.
WithTransportCredentials
(
h
.
credentials
)
}
else
{
// Create the gRPC client without TLS
jww
.
WARN
.
Printf
(
"Connecting to %v without TLS!"
,
h
.
a
ddress
)
jww
.
WARN
.
Printf
(
"Connecting to %v without TLS!"
,
h
.
GetA
ddress
()
)
securityDial
=
grpc
.
WithInsecure
()
}
jww
.
DEBUG
.
Printf
(
"Attempting to establish connection to %s using"
+
" credentials: %+v"
,
h
.
a
ddress
,
securityDial
)
" credentials: %+v"
,
h
.
GetA
ddress
()
,
securityDial
)
// Attempt to establish a new connection
for
numRetries
:=
0
;
numRetries
<
h
.
maxRetries
&&
!
h
.
isAlive
();
numRetries
++
{
h
.
disconnect
()
jww
.
INFO
.
Printf
(
"Connecting to %+v. Attempt number %+v of %+v"
,
h
.
a
ddress
,
numRetries
,
h
.
maxRetries
)
h
.
GetA
ddress
()
,
numRetries
,
h
.
maxRetries
)
// If timeout is enabled, the max wait time becomes
// ~14 seconds (with maxRetries=100)
...
...
@@ -298,13 +304,13 @@ func (h *Host) connectHelper() (err error) {
ctx
,
cancel
:=
ConnectionContext
(
time
.
Duration
(
backoffTime
))
// Create the connection
h
.
connection
,
err
=
grpc
.
DialContext
(
ctx
,
h
.
a
ddress
,
h
.
connection
,
err
=
grpc
.
DialContext
(
ctx
,
h
.
GetA
ddress
()
,
securityDial
,
grpc
.
WithBlock
(),
grpc
.
WithKeepaliveParams
(
KaClientOpts
))
if
err
!=
nil
{
jww
.
ERROR
.
Printf
(
"Attempt number %+v to connect to %s failed: %+v
\n
"
,
numRetries
,
h
.
a
ddress
,
errors
.
New
(
err
.
Error
()))
numRetries
,
h
.
GetA
ddress
()
,
errors
.
New
(
err
.
Error
()))
}
cancel
()
}
...
...
@@ -313,11 +319,12 @@ func (h *Host) connectHelper() (err error) {
if
!
h
.
isAlive
()
{
h
.
disconnect
()
return
errors
.
New
(
fmt
.
Sprintf
(
"Last try to connect to %s failed. Giving up"
,
h
.
address
))
"Last try to connect to %s failed. Giving up"
,
h
.
GetAddress
()))
}
// Add the successful connection to the Manager
jww
.
INFO
.
Printf
(
"Successfully connected to %v"
,
h
.
a
ddress
)
jww
.
INFO
.
Printf
(
"Successfully connected to %v"
,
h
.
GetA
ddress
()
)
return
}
...
...
@@ -361,7 +368,7 @@ func (h *Host) setCredentials() error {
func
(
h
*
Host
)
String
()
string
{
h
.
sendMux
.
RLock
()
defer
h
.
sendMux
.
RUnlock
()
addr
:=
h
.
a
ddress
addr
:=
h
.
GetA
ddress
()
actualConnection
:=
h
.
connection
creds
:=
h
.
credentials
...
...
This diff is collapsed.
Click to expand it.
connect/host_test.go
+
5
−
4
View file @
72b66ed2
...
...
@@ -24,7 +24,7 @@ func TestHost_address(t *testing.T) {
return
}
if
host
.
a
ddress
!=
testAddress
{
if
host
.
GetA
ddress
()
!=
testAddress
{
t
.
Errorf
(
"Expected addresses to match"
)
}
}
...
...
@@ -33,7 +33,6 @@ func TestHost_GetCertificate(t *testing.T) {
testCert
:=
[]
byte
(
"TEST"
)
host
:=
Host
{
address
:
""
,
certificate
:
testCert
,
maxRetries
:
0
,
connection
:
nil
,
...
...
@@ -65,7 +64,8 @@ func TestHost_GetId(t *testing.T) {
func
TestHost_GetAddress
(
t
*
testing
.
T
)
{
// Test values
testAddress
:=
"192.167.1.1:8080"
testHost
:=
Host
{
address
:
testAddress
}
testHost
:=
Host
{}
testHost
.
UpdateAddress
(
testAddress
)
// Test function
if
testHost
.
GetAddress
()
!=
testAddress
{
...
...
@@ -78,7 +78,8 @@ func TestHost_GetAddress(t *testing.T) {
func
TestHost_UpdateAddress
(
t
*
testing
.
T
)
{
testAddress
:=
"192.167.1.1:8080"
testUpdatedAddress
:=
"192.167.1.1:8080"
testHost
:=
Host
{
address
:
testAddress
}
testHost
:=
Host
{}
testHost
.
UpdateAddress
(
testAddress
)
// Test function
if
testHost
.
GetAddress
()
!=
testAddress
{
...
...
This diff is collapsed.
Click to expand it.
connect/manager.go
+
2
−
0
View file @
72b66ed2
...
...
@@ -81,6 +81,8 @@ func (m *Manager) RemoveHost(hid *id.ID) {
// Closes all client connections and removes them from Manager
func
(
m
*
Manager
)
DisconnectAll
()
{
m
.
mux
.
RLock
()
defer
m
.
mux
.
RUnlock
()
for
_
,
host
:=
range
m
.
connections
{
host
.
Disconnect
()
}
...
...
This diff is collapsed.
Click to expand it.
connect/manager_test.go
+
0
−
1
View file @
72b66ed2
...
...
@@ -42,7 +42,6 @@ func TestMain(m *testing.M) {
func
TestSetCredentials_InvalidCert
(
t
*
testing
.
T
)
{
host
:=
&
Host
{
address
:
""
,
certificate
:
[]
byte
(
"test"
),
}
err
:=
host
.
setCredentials
()
...
...
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