Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
600adac3
Commit
600adac3
authored
May 21, 2019
by
u0x01
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge from AlexStocks/getty
parent
533281db
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
164 additions
and
23 deletions
+164
-23
client.go
client.go
+9
-6
conn.go
conn.go
+5
-5
go.mod
go.mod
+8
-11
go.sum
go.sum
+133
-0
options.go
options.go
+8
-0
session.go
session.go
+1
-1
No files found.
client.go
View file @
600adac3
...
...
@@ -29,8 +29,8 @@ import (
)
const
(
connInterval
=
3e9
// 3
s
connectTimeout
=
5
e9
connInterval
=
5e8
// 500m
s
connectTimeout
=
3
e9
maxTimes
=
10
)
...
...
@@ -175,7 +175,7 @@ func (c *client) dialUDP() Session {
}
// check connection alive by write/read action
conn
.
SetWriteDeadline
(
wheel
.
Now
()
.
Add
(
1e9
))
conn
.
SetWriteDeadline
(
time
.
Now
()
.
Add
(
1e9
))
if
length
,
err
=
conn
.
Write
(
connectPingPackage
[
:
]);
err
!=
nil
{
conn
.
Close
()
log
.
Warn
(
"conn.Write(%s) = {length:%d, err:%s}"
,
string
(
connectPingPackage
),
length
,
jerrors
.
ErrorStack
(
err
))
...
...
@@ -183,7 +183,7 @@ func (c *client) dialUDP() Session {
<-
wheel
.
After
(
connInterval
)
continue
}
conn
.
SetReadDeadline
(
wheel
.
Now
()
.
Add
(
1e9
))
conn
.
SetReadDeadline
(
time
.
Now
()
.
Add
(
1e9
))
length
,
err
=
conn
.
Read
(
buf
)
if
netErr
,
ok
:=
jerrors
.
Cause
(
err
)
.
(
net
.
Error
);
ok
&&
netErr
.
Timeout
()
{
err
=
nil
...
...
@@ -361,6 +361,10 @@ func (c *client) connect() {
// ss.RunEventLoop()
ss
.
(
*
session
)
.
run
()
c
.
Lock
()
if
c
.
ssMap
==
nil
{
c
.
Unlock
()
break
}
c
.
ssMap
[
ss
]
=
struct
{}{}
c
.
Unlock
()
ss
.
SetAttribute
(
sessionClientKey
,
c
)
...
...
@@ -408,8 +412,7 @@ func (c *client) reConnect() {
if
maxTimes
<
times
{
times
=
maxTimes
}
// time.Sleep(time.Duration(int64(times) * connInterval))
<-
wheel
.
After
(
time
.
Duration
(
int64
(
times
)
*
connInterval
))
time
.
Sleep
(
time
.
Duration
(
int64
(
times
)
*
int64
(
c
.
reconnectInterval
)))
}
}
...
...
conn.go
View file @
600adac3
...
...
@@ -240,7 +240,7 @@ func (t *gettyTCPConn) read(p []byte) (int, error) {
// Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime
=
wheel
.
Now
()
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
t
.
rLastDeadline
)
>
(
t
.
rTimeout
>>
2
)
{
if
err
=
t
.
conn
.
SetReadDeadline
(
currentTime
.
Add
(
t
.
rTimeout
));
err
!=
nil
{
return
0
,
jerrors
.
Trace
(
err
)
...
...
@@ -273,7 +273,7 @@ func (t *gettyTCPConn) Write(pkg interface{}) (int, error) {
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime
=
wheel
.
Now
()
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
t
.
wLastDeadline
)
>
(
t
.
wTimeout
>>
2
)
{
if
err
=
t
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
t
.
wTimeout
));
err
!=
nil
{
return
0
,
jerrors
.
Trace
(
err
)
...
...
@@ -390,7 +390,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) {
// Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime
=
wheel
.
Now
()
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
u
.
rLastDeadline
)
>
(
u
.
rTimeout
>>
2
)
{
if
err
=
u
.
conn
.
SetReadDeadline
(
currentTime
.
Add
(
u
.
rTimeout
));
err
!=
nil
{
return
0
,
nil
,
jerrors
.
Trace
(
err
)
...
...
@@ -438,7 +438,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime
=
wheel
.
Now
()
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
u
.
wLastDeadline
)
>
(
u
.
wTimeout
>>
2
)
{
if
err
=
u
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
u
.
wTimeout
));
err
!=
nil
{
return
0
,
jerrors
.
Trace
(
err
)
...
...
@@ -564,7 +564,7 @@ func (w *gettyWSConn) updateWriteDeadline() error {
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details.
currentTime
=
wheel
.
Now
()
currentTime
=
time
.
Now
()
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
wTimeout
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
wTimeout
));
err
!=
nil
{
return
jerrors
.
Trace
(
err
)
...
...
go.mod
View file @
600adac3
module github.com/dubbogo/getty
require (
github.com/AlexStocks/getty v1.0.4
github.com/AlexStocks/goext v0.3.2
github.com/AlexStocks/log4go v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/AlexStocks/log4go v1.0.2
github.com/dubbogo/log4go v0.0.0-20190406152735-41c57e1073e9
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gogo/protobuf v1.2.1
github.com/golang/snappy v0.0.1
github.com/gorilla/websocket v1.4.0
github.com/juju/errors v0.0.0-20190207033735-e65537c515d7
github.com/juju/loggo v0.0.0-20190212223446-d976af380377 // indirect
github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7
go.etcd.io/etcd v3.3.13+incompatible // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
gopkg.in/yaml.v2 v2.2.2
)
go.sum
View file @
600adac3
This diff is collapsed.
Click to expand it.
options.go
View file @
600adac3
...
...
@@ -70,6 +70,7 @@ type ClientOption func(*ClientOptions)
type
ClientOptions
struct
{
addr
string
number
int
reconnectInterval
int
// reConnect Interval
// the cert file of wss server which may contain server domain, server ip, the starting effective date, effective
// duration, the hash alg, the len of the private key.
...
...
@@ -84,6 +85,13 @@ func WithServerAddress(addr string) ClientOption {
}
}
// @reconnectInterval is server address.
func
WithReconnectInterval
(
reconnectInterval
int
)
ClientOption
{
return
func
(
o
*
ClientOptions
)
{
o
.
reconnectInterval
=
reconnectInterval
}
}
// @num is connection number.
func
WithConnectionNumber
(
num
int
)
ClientOption
{
return
func
(
o
*
ClientOptions
)
{
...
...
session.go
View file @
600adac3
...
...
@@ -776,7 +776,7 @@ func (s *session) stop() {
default
:
s
.
once
.
Do
(
func
()
{
// let read/Write timeout asap
now
:=
wheel
.
Now
()
now
:=
time
.
Now
()
if
conn
:=
s
.
Conn
();
conn
!=
nil
{
conn
.
SetReadDeadline
(
now
.
Add
(
s
.
readTimeout
()))
conn
.
SetWriteDeadline
(
now
.
Add
(
s
.
writeTimeout
()))
...
...
Write
Preview
Markdown
is supported
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