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
a6db1787
Commit
a6db1787
authored
Sep 2, 2022
by
Jonah Husson
Browse files
Options
Downloads
Patches
Plain Diff
Add separate isonline func for webconn
parent
e3272eb0
No related branches found
No related tags found
3 merge requests
!39
Merge release into master
,
!36
Add separate isonline func for webconn
,
!32
Project/channels
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
connect/connection.go
+2
-0
2 additions, 0 deletions
connect/connection.go
connect/grpcConn.go
+20
-0
20 additions, 0 deletions
connect/grpcConn.go
connect/host.go
+1
-17
1 addition, 17 deletions
connect/host.go
connect/webConn.go
+57
-0
57 additions, 0 deletions
connect/webConn.go
with
80 additions
and
17 deletions
connect/connection.go
+
2
−
0
View file @
a6db1787
...
...
@@ -4,6 +4,7 @@ import (
"git.xx.network/elixxir/grpc-web-go-client/grpcweb"
jww
"github.com/spf13/jwalterweatherman"
"google.golang.org/grpc"
"time"
)
const
(
...
...
@@ -49,6 +50,7 @@ type Connection interface {
// Close closes the underlying connection
Close
()
error
IsOnline
()
(
time
.
Duration
,
bool
)
clientConnHelpers
}
...
...
This diff is collapsed.
Click to expand it.
connect/grpcConn.go
+
20
−
0
View file @
a6db1787
...
...
@@ -8,6 +8,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"math"
"net"
"sync/atomic"
"time"
)
...
...
@@ -148,3 +149,22 @@ func (gc *grpcConn) isAlive() bool {
return
state
==
connectivity
.
Idle
||
state
==
connectivity
.
Connecting
||
state
==
connectivity
.
Ready
}
func
(
gc
*
grpcConn
)
IsOnline
()
(
time
.
Duration
,
bool
)
{
addr
:=
gc
.
h
.
GetAddress
()
start
:=
time
.
Now
()
conn
,
err
:=
net
.
DialTimeout
(
"tcp"
,
addr
,
gc
.
h
.
params
.
PingTimeout
)
if
err
!=
nil
{
// If we cannot connect, mark the connection as failed
jww
.
DEBUG
.
Printf
(
"Failed to verify connectivity for address %s"
,
addr
)
return
0
,
false
}
// Attempt to close the connection
if
conn
!=
nil
{
errClose
:=
conn
.
Close
()
if
errClose
!=
nil
{
jww
.
DEBUG
.
Printf
(
"Failed to close connection for address %s"
,
addr
)
}
}
return
time
.
Since
(
start
),
true
}
This diff is collapsed.
Click to expand it.
connect/host.go
+
1
−
17
View file @
a6db1787
...
...
@@ -22,7 +22,6 @@ import (
"gitlab.com/xx_network/primitives/rateLimiting"
"google.golang.org/grpc/credentials"
"math"
"net"
"strings"
"sync"
"sync/atomic"
...
...
@@ -251,22 +250,7 @@ func (h *Host) conditionalDisconnect(count uint64) {
// 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
)
{
addr
:=
h
.
GetAddress
()
start
:=
time
.
Now
()
conn
,
err
:=
net
.
DialTimeout
(
"tcp"
,
addr
,
h
.
params
.
PingTimeout
)
if
err
!=
nil
{
// If we cannot connect, mark the connection as failed
jww
.
DEBUG
.
Printf
(
"Failed to verify connectivity for address %s"
,
addr
)
return
0
,
false
}
// Attempt to close the connection
if
conn
!=
nil
{
errClose
:=
conn
.
Close
()
if
errClose
!=
nil
{
jww
.
DEBUG
.
Printf
(
"Failed to close connection for address %s"
,
addr
)
}
}
return
time
.
Since
(
start
),
true
return
h
.
connection
.
IsOnline
()
}
// send checks that the host has a connection and sends if it does.
...
...
This diff is collapsed.
Click to expand it.
connect/webConn.go
+
57
−
0
View file @
a6db1787
package
connect
import
(
"crypto/tls"
"fmt"
"git.xx.network/elixxir/grpc-web-go-client/grpcweb"
"github.com/pkg/errors"
jww
"github.com/spf13/jwalterweatherman"
"google.golang.org/grpc"
"net/http"
"net/http/httptrace"
"strings"
"time"
)
...
...
@@ -163,3 +167,56 @@ func (wc *webConn) isAlive() bool {
}
return
wc
.
connection
.
IsAlive
()
}
func
(
wc
*
webConn
)
IsOnline
()
(
time
.
Duration
,
bool
)
{
addr
:=
wc
.
h
.
GetAddress
()
start
:=
time
.
Now
()
tr
:=
&
http
.
Transport
{
TLSClientConfig
:
&
tls
.
Config
{
InsecureSkipVerify
:
true
,
},
}
client
:=
http
.
Client
{
Transport
:
tr
,
Timeout
:
wc
.
h
.
params
.
PingTimeout
,
}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
addr
,
nil
)
if
err
!=
nil
{
fmt
.
Print
(
"Failed to initiate request "
,
err
)
}
trace
:=
&
httptrace
.
ClientTrace
{
DNSDone
:
func
(
dnsInfo
httptrace
.
DNSDoneInfo
)
{
fmt
.
Println
(
"DNS Info: %+v
\n
"
,
dnsInfo
)
},
GotConn
:
func
(
connInfo
httptrace
.
GotConnInfo
)
{
fmt
.
Println
(
"Got Conn: %+v
\n
"
,
connInfo
)
},
GotFirstResponseByte
:
func
()
{
fmt
.
Println
(
"Got first byte!"
)
},
}
// IMPORTANT - enables better HTTP(S) discovery, because many browsers block CORS by default.
req
.
Header
.
Add
(
"js.fetch:mode"
,
"no-cors"
)
fmt
.
Println
(
"(GO request): "
,
fmt
.
Sprintf
(
"%+v"
,
req
))
req
=
req
.
WithContext
(
httptrace
.
WithClientTrace
(
req
.
Context
(),
trace
))
if
_
,
err
:=
client
.
Do
(
req
);
err
!=
nil
{
fmt
.
Println
(
err
)
fmt
.
Println
(
"(GO error): "
,
err
.
Error
())
// TODO: Get more exception strings for major browsers
errString
:=
strings
.
ToLower
(
err
.
Error
())
if
strings
.
Contains
(
errString
,
"exceeded while awaiting"
)
||
strings
.
Contains
(
errString
,
"ssl"
)
||
strings
.
Contains
(
errString
,
"cors"
)
||
strings
.
Contains
(
errString
,
"invalid"
)
||
strings
.
Contains
(
errString
,
"protocol"
)
{
return
time
.
Since
(
start
),
true
}
else
{
return
time
.
Since
(
start
),
false
}
}
return
time
.
Since
(
start
),
true
}
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