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
101ed7a9
Commit
101ed7a9
authored
Mar 11, 2022
by
Jake Taylor
Browse files
Options
Downloads
Patches
Plain Diff
add configurable flag for lazy connections to hostParams.go
parent
7c497413
No related branches found
No related tags found
2 merge requests
!39
Merge release into master
,
!27
add configurable flag for lazy connections to hostParams.go
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
connect/host.go
+28
-20
28 additions, 20 deletions
connect/host.go
connect/hostParams.go
+5
-0
5 additions, 0 deletions
connect/hostParams.go
connect/transmit.go
+1
-1
1 addition, 1 deletion
connect/transmit.go
with
34 additions
and
21 deletions
connect/host.go
+
28
−
20
View file @
101ed7a9
...
...
@@ -31,7 +31,7 @@ import (
"time"
)
//
I
nformation used to describe a connection
to a host
//
Host i
nformation used to describe a
remote
connection
type
Host
struct
{
// System-wide ID of the Host
id
*
id
.
ID
...
...
@@ -53,6 +53,9 @@ type Host struct {
// GRPC connection object
connection
*
grpc
.
ClientConn
connectionCount
uint64
// lock which ensures only a single thread is connecting at a time and
// that connections do not interrupt sends
connectionMux
sync
.
RWMutex
// TLS credentials object used to establish the connection
credentials
credentials
.
TransportCredentials
...
...
@@ -63,10 +66,6 @@ type Host struct {
// State tracking for host metric
metrics
*
Metric
// lock which ensures only a single thread is connecting at a time and
// that connections do not interrupt sends
connectionMux
sync
.
RWMutex
coolOffBucket
*
rateLimiting
.
Bucket
inCoolOff
bool
...
...
@@ -78,7 +77,7 @@ type Host struct {
windowSize
*
int32
}
//
C
reates a new Host object
//
NewHost c
reates a new Host object
func
NewHost
(
id
*
id
.
ID
,
address
string
,
cert
[]
byte
,
params
HostParams
)
(
host
*
Host
,
err
error
)
{
windowSize
:=
int32
(
0
)
...
...
@@ -108,16 +107,25 @@ func NewHost(id *id.ID, address string, cert []byte, params HostParams) (host *H
// Configure the host credentials
err
=
host
.
setCredentials
()
if
err
!=
nil
{
return
}
// the amount of data, when streaming, that a sender can send before receiving an ACK
// Connect immediately if configured to do so
if
!
params
.
LazyConnection
{
// No mutex required
err
=
host
.
connect
()
}
return
}
// SetWindowSize sets the amount of data, when streaming, that a sender can send before receiving an ACK
// keep at zero to use the default GRPC algorithm to determine
func
(
h
*
Host
)
SetWindowSize
(
size
int32
)
{
atomic
.
StoreInt32
(
h
.
windowSize
,
size
)
}
//
S
imple getter for the public key
//
GetPubKey s
imple getter for the public key
func
(
h
*
Host
)
GetPubKey
()
*
rsa
.
PublicKey
{
return
h
.
rsaPublicKey
}
...
...
@@ -132,7 +140,7 @@ func (h *Host) Connected() (bool, uint64) {
}
// connectedUnsafe checks if the given Host's connection is alive without taking
// a connection lock. Only use if already under a connection lock. The
the
uint is
// a connection lock. Only use if already under a connection lock. The uint is
//the connection count, it increments every time a reconnect occurs
func
(
h
*
Host
)
connectedUnsafe
()
(
bool
,
uint64
)
{
return
h
.
isAlive
()
&&
!
h
.
authenticationRequired
(),
h
.
connectionCount
...
...
@@ -143,7 +151,7 @@ func (h *Host) GetMessagingContext() (context.Context, context.CancelFunc) {
return
h
.
GetMessagingContextWithTimeout
(
h
.
params
.
SendTimeout
)
}
// GetMessagingContext returns a context object for message sending configured according to HostParams
// GetMessagingContext
WithTimeout
returns a context object for message sending configured according to HostParams
func
(
h
*
Host
)
GetMessagingContextWithTimeout
(
timeout
time
.
Duration
)
(
context
.
Context
,
context
.
CancelFunc
)
{
return
newContext
(
timeout
)
...
...
@@ -189,7 +197,7 @@ func (h *Host) isExcludedMetricError(err string) bool {
return
false
}
// Sets the host metrics to an arbitrary value. Used for testing
// Set
MetricsTesting set
s the host metrics to an arbitrary value. Used for testing
// purposes only
func
(
h
*
Host
)
SetMetricsTesting
(
m
*
Metric
,
face
interface
{})
{
// Ensure that this function is only run in testing environments
...
...
@@ -204,17 +212,17 @@ func (h *Host) SetMetricsTesting(m *Metric, face interface{}) {
}
// Disconnect closes
a
the Host connection under the write lock
// Due to asynch
o
rous connection handling, this may result in
// Disconnect closes the Host connection under the write lock
// Due to asynchr
on
ous connection handling, this may result in
// killing a good connection and could result in an immediate
// reconnection by a sep
e
rate thread
// reconnection by a sep
a
rate thread
func
(
h
*
Host
)
Disconnect
()
{
h
.
connectionMux
.
Lock
()
defer
h
.
connectionMux
.
Unlock
()
h
.
disconnect
()
}
// ConditionalDisconnect closes
a
the Host connection under the write lock only
// ConditionalDisconnect closes the Host connection under the write lock only
// if the connection count has not increased
func
(
h
*
Host
)
conditionalDisconnect
(
count
uint64
)
{
if
count
==
h
.
connectionCount
{
...
...
@@ -222,7 +230,7 @@ func (h *Host) conditionalDisconnect(count uint64) {
}
}
//
R
eturns whether
or not
the Host is able to be contacted
//
IsOnline r
eturns whether the Host is able to be contacted
// before the timeout by attempting to dial a tcp connection
// Returns how long the ping took, and whether it was successful
func
(
h
*
Host
)
IsOnline
()
(
time
.
Duration
,
bool
)
{
...
...
@@ -281,7 +289,7 @@ func (h *Host) connect() error {
}
// authenticationRequired Checks if new authentication is required with
// the remote. This is used exclusively under the lock in protocom
s
.transmit so
// the remote. This is used exclusively under the lock in protocom
m
.transmit so
// no lock is needed
func
(
h
*
Host
)
authenticationRequired
()
bool
{
return
h
.
params
.
AuthEnabled
&&
!
h
.
transmissionToken
.
Has
()
...
...
@@ -298,10 +306,10 @@ func (h *Host) isAlive() bool {
state
==
connectivity
.
Ready
}
// disconnect closes
a
the Host connection while not under a write lock.
// disconnect closes the Host connection while not under a write lock.
// undefined behavior if the caller has not taken the write lock
func
(
h
*
Host
)
disconnect
()
{
// its possible to close a host which never sent so it never made a
// it
'
s possible to close a host which never sent so
that
it never made a
// connection. In that case, we should not close a connection which does not
// exist
if
h
.
connection
!=
nil
{
...
...
@@ -442,7 +450,7 @@ func (h *Host) String() string {
h
.
id
,
addr
)
}
// Stringer interface for connection
// String
Verbose string
er interface for connection
func
(
h
*
Host
)
StringVerbose
()
string
{
return
fmt
.
Sprintf
(
"%s
\t
CERTIFICATE: %s"
,
h
,
h
.
certificate
)
}
...
...
This diff is collapsed.
Click to expand it.
connect/hostParams.go
+
5
−
0
View file @
101ed7a9
...
...
@@ -41,6 +41,10 @@ type HostParams struct {
// If set, metric handling will be enabled on this host
EnableMetrics
bool
// If true, a connection will only be established when a comm is sent
// else, a connection will be established immediately upon host creation
LazyConnection
bool
// List of sending errors that are deemed unimportant
// Reception of these errors will not update the Metric state
ExcludeMetricErrors
[]
string
...
...
@@ -61,6 +65,7 @@ func GetDefaultHostParams() HostParams {
SendTimeout
:
2
*
time
.
Minute
,
PingTimeout
:
5
*
time
.
Second
,
EnableMetrics
:
false
,
LazyConnection
:
true
,
ExcludeMetricErrors
:
make
([]
string
,
0
),
KaClientOpts
:
keepalive
.
ClientParameters
{
// Send keepAlive every Time interval
...
...
This diff is collapsed.
Click to expand it.
connect/transmit.go
+
1
−
1
View file @
101ed7a9
...
...
@@ -88,7 +88,7 @@ func (c *ProtoComms) connect(host *Host, count uint64) (uint64, error) {
}
//if the connection is alive return, it is possible for another transmission
//to connect between releasing the read lock and taking the write l
i
ck
//to connect between releasing the read lock and taking the write l
o
ck
if
!
host
.
isAlive
()
{
//connect to host
jww
.
INFO
.
Printf
(
"Host %s not connected, attempting to connect..."
,
...
...
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