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
d708dcdd
Commit
d708dcdd
authored
Mar 10, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
disable readtimeout
parent
034b8c08
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
86 deletions
+83
-86
change_log.md
change_log.md
+8
-2
conn.go
conn.go
+54
-65
server.go
server.go
+1
-1
session.go
session.go
+20
-18
No files found.
change_log.md
View file @
d708dcdd
...
...
@@ -11,6 +11,12 @@
## develop history ##
---
-
2018/03/10
> improvement
*
1 rDeadline -> rTimeout
*
2 wDeadline -> wTimeout
*
3 disable readtimeout in func (w
*
gettyWSConn) read()
-
2018/03/08
> feature
*
1 add udp client and udp server
...
...
@@ -137,7 +143,7 @@
> 3 version: 0.4.0
-
2016/10/01
> 1 remark SetRead
Deadline & SetWriteDeadline
in session.go (ref: https://github.com/golang/go/issues/15133)
> 1 remark SetRead
Timeout & SetWriteTimeout
in session.go (ref: https://github.com/golang/go/issues/15133)
>
> 3 version: 0.3.14
...
...
@@ -222,7 +228,7 @@
>
> 2 add clause "this.attrs = nil" in session.go:(Session)Close
>
> 3 session.go:Session{*gettyConn, read
Deadline, writeDeadline} -> session.go:Session{gettyConn, rDeadline, wDeadline
}
> 3 session.go:Session{*gettyConn, read
Timeout, writeTimeout} -> session.go:Session{gettyConn, rTimeout, wTimeout
}
>
> 4 add session.go:(Session)Reset
>
...
...
conn.go
View file @
d708dcdd
...
...
@@ -63,12 +63,12 @@ type Connection interface {
UpdateActive
()
// get session's active time
GetActive
()
time
.
Time
read
Deadline
()
time
.
Duration
// SetRead
Deadline
sets deadline for the future read calls.
SetRead
Deadline
(
time
.
Duration
)
write
Deadline
()
time
.
Duration
// SetWrite
Deadlile
sets deadline for the future read calls.
SetWrite
Deadline
(
time
.
Duration
)
read
Timeout
()
time
.
Duration
// SetRead
Timeout
sets deadline for the future read calls.
SetRead
Timeout
(
time
.
Duration
)
write
Timeout
()
time
.
Duration
// SetWrite
Timeout
sets deadline for the future read calls.
SetWrite
Timeout
(
time
.
Duration
)
Write
(
interface
{})
(
int
,
error
)
// don't distinguish between tcp connection and websocket connection. Because
// gorilla/websocket/conn.go:(Conn)Close also invoke net.Conn.Close
...
...
@@ -93,8 +93,8 @@ type gettyConn struct {
readPkgCount
uint32
// send pkg count
writePkgCount
uint32
// recv pkg count
active
int64
// last active, in milliseconds
r
Deadline
time
.
Duration
// network current limiting
w
Deadline
time
.
Duration
r
Timeout
time
.
Duration
// network current limiting
w
Timeout
time
.
Duration
rLastDeadline
time
.
Time
// lastest network read time
wLastDeadline
time
.
Time
// lastest network write time
local
string
// local address
...
...
@@ -135,33 +135,33 @@ func (c *gettyConn) Write(interface{}) (int, error) {
func
(
c
*
gettyConn
)
close
(
int
)
{}
func
(
c
gettyConn
)
read
Deadline
()
time
.
Duration
{
return
c
.
r
Deadline
func
(
c
gettyConn
)
read
Timeout
()
time
.
Duration
{
return
c
.
r
Timeout
}
func
(
c
*
gettyConn
)
SetRead
Deadline
(
rDeadline
time
.
Duration
)
{
if
r
Deadline
<
1
{
panic
(
"@r
Deadline
< 1"
)
func
(
c
*
gettyConn
)
SetRead
Timeout
(
rTimeout
time
.
Duration
)
{
if
r
Timeout
<
1
{
panic
(
"@r
Timeout
< 1"
)
}
c
.
r
Deadline
=
rDeadline
if
c
.
w
Deadline
==
0
{
c
.
w
Deadline
=
rDeadline
c
.
r
Timeout
=
rTimeout
if
c
.
w
Timeout
==
0
{
c
.
w
Timeout
=
rTimeout
}
}
func
(
c
gettyConn
)
write
Deadline
()
time
.
Duration
{
return
c
.
w
Deadline
func
(
c
gettyConn
)
write
Timeout
()
time
.
Duration
{
return
c
.
w
Timeout
}
func
(
c
*
gettyConn
)
SetWrite
Deadline
(
wDeadline
time
.
Duration
)
{
if
w
Deadline
<
1
{
panic
(
"@w
Deadline
< 1"
)
func
(
c
*
gettyConn
)
SetWrite
Timeout
(
wTimeout
time
.
Duration
)
{
if
w
Timeout
<
1
{
panic
(
"@w
Timeout
< 1"
)
}
c
.
w
Deadline
=
wDeadline
if
c
.
r
Deadline
==
0
{
c
.
r
Deadline
=
wDeadline
c
.
w
Timeout
=
wTimeout
if
c
.
r
Timeout
==
0
{
c
.
r
Timeout
=
wTimeout
}
}
...
...
@@ -196,6 +196,8 @@ func newGettyTCPConn(conn net.Conn) *gettyTCPConn {
writer
:
io
.
Writer
(
conn
),
gettyConn
:
gettyConn
{
id
:
atomic
.
AddUint32
(
&
connID
,
1
),
rTimeout
:
netIOTimeout
,
wTimeout
:
netIOTimeout
,
local
:
localAddr
,
peer
:
peerAddr
,
compress
:
CompressNone
,
...
...
@@ -255,13 +257,13 @@ func (t *gettyTCPConn) read(p []byte) (int, error) {
length
int
)
if
t
.
r
Deadline
>
0
{
if
t
.
r
Timeout
>
0
{
// 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
()
if
currentTime
.
Sub
(
t
.
rLastDeadline
)
>
(
t
.
r
Deadline
>>
2
)
{
if
err
=
t
.
conn
.
SetReadDeadline
(
currentTime
.
Add
(
t
.
r
Deadline
));
err
!=
nil
{
if
currentTime
.
Sub
(
t
.
rLastDeadline
)
>
(
t
.
r
Timeout
>>
2
)
{
if
err
=
t
.
conn
.
SetReadDeadline
(
currentTime
.
Add
(
t
.
r
Timeout
));
err
!=
nil
{
return
0
,
err
}
t
.
rLastDeadline
=
currentTime
...
...
@@ -285,13 +287,13 @@ func (t *gettyTCPConn) Write(pkg interface{}) (int, error) {
if
p
,
ok
=
pkg
.
([]
byte
);
!
ok
{
return
0
,
fmt
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
}
if
t
.
w
Deadline
>
0
{
if
t
.
w
Timeout
>
0
{
// 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
()
if
currentTime
.
Sub
(
t
.
wLastDeadline
)
>
(
t
.
w
Deadline
>>
2
)
{
if
err
=
t
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
t
.
w
Deadline
));
err
!=
nil
{
if
currentTime
.
Sub
(
t
.
wLastDeadline
)
>
(
t
.
w
Timeout
>>
2
)
{
if
err
=
t
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
t
.
w
Timeout
));
err
!=
nil
{
return
0
,
err
}
t
.
wLastDeadline
=
currentTime
...
...
@@ -346,9 +348,11 @@ func newGettyWSConn(conn *websocket.Conn) *gettyWSConn {
gettyWSConn
:=
&
gettyWSConn
{
conn
:
conn
,
gettyConn
:
gettyConn
{
id
:
atomic
.
AddUint32
(
&
connID
,
1
),
local
:
localAddr
,
peer
:
peerAddr
,
id
:
atomic
.
AddUint32
(
&
connID
,
1
),
rTimeout
:
netIOTimeout
,
wTimeout
:
netIOTimeout
,
local
:
localAddr
,
peer
:
peerAddr
,
},
}
conn
.
EnableWriteCompression
(
false
)
...
...
@@ -375,13 +379,13 @@ func (w *gettyWSConn) handlePing(message string) error {
err
error
currentTime
time
.
Time
)
if
w
.
w
Deadline
>
0
{
if
w
.
w
Timeout
>
0
{
// 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
()
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
w
Deadline
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
w
Deadline
));
err
!=
nil
{
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
w
Timeout
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
w
Timeout
));
err
!=
nil
{
return
err
}
w
.
wLastDeadline
=
currentTime
...
...
@@ -408,27 +412,10 @@ func (w *gettyWSConn) handlePong(string) error {
// websocket connection read
func
(
w
*
gettyWSConn
)
read
()
([]
byte
,
error
)
{
var
(
err
error
currentTime
time
.
Time
)
if
w
.
rDeadline
>
0
{
// 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
()
if
currentTime
.
Sub
(
w
.
rLastDeadline
)
>
(
w
.
rDeadline
>>
2
)
{
if
err
=
w
.
conn
.
SetReadDeadline
(
currentTime
.
Add
(
w
.
rDeadline
));
err
!=
nil
{
return
nil
,
err
}
w
.
rLastDeadline
=
currentTime
}
}
// w.conn.SetReadDeadline(time.Now().Add(w.rDeadline))
// Pls do not set read deadline when using ReadMessage. AlexStocks 20180310
// gorilla/websocket/conn.go:NextReader will always fail when got a timeout error.
_
,
b
,
e
:=
w
.
conn
.
ReadMessage
()
// the first return value is message type.
if
e
==
nil
{
// atomic.AddUint32(&w.readCount, (uint32)(l))
atomic
.
AddUint32
(
&
w
.
readPkgCount
,
1
)
}
else
{
if
websocket
.
IsUnexpectedCloseError
(
e
,
websocket
.
CloseGoingAway
)
{
...
...
@@ -451,13 +438,13 @@ func (w *gettyWSConn) Write(pkg interface{}) (int, error) {
if
p
,
ok
=
pkg
.
([]
byte
);
!
ok
{
return
0
,
fmt
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
}
if
w
.
w
Deadline
>
0
{
if
w
.
w
Timeout
>
0
{
// 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
()
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
w
Deadline
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
w
Deadline
));
err
!=
nil
{
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
w
Timeout
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
w
Timeout
));
err
!=
nil
{
return
0
,
err
}
w
.
wLastDeadline
=
currentTime
...
...
@@ -466,7 +453,7 @@ func (w *gettyWSConn) Write(pkg interface{}) (int, error) {
// atomic.AddUint32(&w.writeCount, 1)
atomic
.
AddUint32
(
&
w
.
writeCount
,
(
uint32
)(
len
(
p
)))
// w.conn.SetWrite
Deadline(time.Now().Add(w.wDeadline
))
// w.conn.SetWrite
Timeout(time.Now().Add(w.wTimeout
))
return
len
(
p
),
w
.
conn
.
WriteMessage
(
websocket
.
BinaryMessage
,
p
)
}
...
...
@@ -537,6 +524,8 @@ func newGettyUDPConn(conn *net.UDPConn, peerUDPAddr *net.UDPAddr) *gettyUDPConn
peerAddr
:
peerUDPAddr
,
gettyConn
:
gettyConn
{
id
:
atomic
.
AddUint32
(
&
connID
,
1
),
rTimeout
:
netIOTimeout
,
wTimeout
:
netIOTimeout
,
local
:
localAddr
,
peer
:
peerAddr
,
compress
:
CompressNone
,
...
...
@@ -563,13 +552,13 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) {
addr
*
net
.
UDPAddr
)
if
u
.
r
Deadline
>
0
{
if
u
.
r
Timeout
>
0
{
// 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
()
if
currentTime
.
Sub
(
u
.
rLastDeadline
)
>
(
u
.
r
Deadline
>>
2
)
{
if
err
=
u
.
conn
.
SetReadDeadline
(
currentTime
.
Add
(
u
.
r
Deadline
));
err
!=
nil
{
if
currentTime
.
Sub
(
u
.
rLastDeadline
)
>
(
u
.
r
Timeout
>>
2
)
{
if
err
=
u
.
conn
.
SetReadDeadline
(
currentTime
.
Add
(
u
.
r
Timeout
));
err
!=
nil
{
return
0
,
nil
,
err
}
u
.
rLastDeadline
=
currentTime
...
...
@@ -604,13 +593,13 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
return
0
,
fmt
.
Errorf
(
"illegal @udpCtx{%#v} type"
,
udpCtx
)
}
if
u
.
w
Deadline
>
0
{
if
u
.
w
Timeout
>
0
{
// 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
()
if
currentTime
.
Sub
(
u
.
wLastDeadline
)
>
(
u
.
w
Deadline
>>
2
)
{
if
err
=
u
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
u
.
w
Deadline
));
err
!=
nil
{
if
currentTime
.
Sub
(
u
.
wLastDeadline
)
>
(
u
.
w
Timeout
>>
2
)
{
if
err
=
u
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
u
.
w
Timeout
));
err
!=
nil
{
return
0
,
err
}
u
.
wLastDeadline
=
currentTime
...
...
server.go
View file @
d708dcdd
...
...
@@ -140,7 +140,7 @@ func (s *Server) stop() {
s
.
streamListener
.
Close
()
s
.
streamListener
=
nil
}
if
s
.
pktListener
=
=
nil
{
if
s
.
pktListener
!
=
nil
{
s
.
pktListener
.
Close
()
s
.
pktListener
=
nil
}
...
...
session.go
View file @
d708dcdd
...
...
@@ -118,8 +118,8 @@ func NewSession() Session {
attrs
:
gxcontext
.
NewValuesContext
(
nil
),
}
session
.
SetWrite
Deadline
(
netIOTimeout
)
session
.
SetRead
Deadline
(
netIOTimeout
)
session
.
SetWrite
Timeout
(
netIOTimeout
)
session
.
SetRead
Timeout
(
netIOTimeout
)
return
session
}
...
...
@@ -134,8 +134,8 @@ func NewTCPSession(conn net.Conn) Session {
attrs
:
gxcontext
.
NewValuesContext
(
nil
),
}
session
.
SetWrite
Deadline
(
netIOTimeout
)
session
.
SetRead
Deadline
(
netIOTimeout
)
session
.
SetWrite
Timeout
(
netIOTimeout
)
session
.
SetRead
Timeout
(
netIOTimeout
)
return
session
}
...
...
@@ -150,8 +150,8 @@ func NewUDPSession(conn *net.UDPConn, peerAddr *net.UDPAddr) Session {
attrs
:
gxcontext
.
NewValuesContext
(
nil
),
}
session
.
SetWrite
Deadline
(
netIOTimeout
)
session
.
SetRead
Deadline
(
netIOTimeout
)
session
.
SetWrite
Timeout
(
netIOTimeout
)
session
.
SetRead
Timeout
(
netIOTimeout
)
return
session
}
...
...
@@ -166,8 +166,8 @@ func NewWSSession(conn *websocket.Conn) Session {
attrs
:
gxcontext
.
NewValuesContext
(
nil
),
}
session
.
SetWrite
Deadline
(
netIOTimeout
)
session
.
SetRead
Deadline
(
netIOTimeout
)
session
.
SetWrite
Timeout
(
netIOTimeout
)
session
.
SetRead
Timeout
(
netIOTimeout
)
return
session
}
...
...
@@ -182,8 +182,8 @@ func (s *session) Reset() {
s
.
attrs
=
gxcontext
.
NewValuesContext
(
nil
)
s
.
grNum
=
0
s
.
SetWrite
Deadline
(
netIOTimeout
)
s
.
SetRead
Deadline
(
netIOTimeout
)
s
.
SetWrite
Timeout
(
netIOTimeout
)
s
.
SetRead
Timeout
(
netIOTimeout
)
}
// func (s *session) SetConn(conn net.Conn) { s.gettyConn = newGettyConn(conn) }
...
...
@@ -354,7 +354,7 @@ func (s *session) WritePkg(pkg interface{}) error {
}
}()
var
d
=
s
.
write
Deadline
()
var
d
=
s
.
write
Timeout
()
if
d
>
netIOTimeout
{
d
=
netIOTimeout
}
...
...
@@ -363,7 +363,7 @@ func (s *session) WritePkg(pkg interface{}) error {
break
// for possible gen a new pkg
// default:
// case <-time.After(s.w
Deadline
):
// case <-time.After(s.w
Timeout
):
// case <-time.After(netIOTimeout):
case
<-
wheel
.
After
(
d
)
:
log
.
Warn
(
"%s, [session.WritePkg] wQ{len:%d, cap:%d}"
,
s
.
Stat
(),
len
(
s
.
wQ
),
cap
(
s
.
wQ
))
...
...
@@ -379,7 +379,7 @@ func (s *session) WriteBytes(pkg []byte) error {
return
ErrSessionClosed
}
// s.conn.SetWrite
Deadline(time.Now().Add(s.wDeadline
))
// s.conn.SetWrite
Timeout(time.Now().Add(s.wTimeout
))
_
,
err
:=
s
.
Connection
.
Write
(
pkg
)
return
err
}
...
...
@@ -389,7 +389,7 @@ func (s *session) WriteBytesArray(pkgs ...[]byte) error {
if
s
.
IsClosed
()
{
return
ErrSessionClosed
}
// s.conn.SetWrite
Deadline(time.Now().Add(s.wDeadline
))
// s.conn.SetWrite
Timeout(time.Now().Add(s.wTimeout
))
if
len
(
pkgs
)
==
1
{
// return s.Connection.Write(pkgs[0])
...
...
@@ -529,7 +529,6 @@ LOOP:
if
flag
{
if
wsFlag
{
err
=
wsConn
.
writePing
()
log
.
Debug
(
"wsConn.writePing() = error{%#v}"
,
err
)
if
err
!=
nil
{
log
.
Warn
(
"wsConn.writePing() = error{%#v}"
,
err
)
}
...
...
@@ -578,6 +577,8 @@ func (s *session) handlePackage() {
err
=
s
.
handleWSPackage
()
}
else
if
_
,
ok
:=
s
.
Connection
.
(
*
gettyUDPConn
);
ok
{
err
=
s
.
handleUDPPackage
()
}
else
{
panic
(
fmt
.
Sprintf
(
"unknown type session{%#v}"
,
s
))
}
}
...
...
@@ -608,7 +609,7 @@ func (s *session) handleTCPPackage() error {
bufLen
=
0
for
{
// for clause for the network timeout condition check
// s.conn.SetRead
Deadline(time.Now().Add(s.rDeadline
))
// s.conn.SetRead
Timeout(time.Now().Add(s.rTimeout
))
bufLen
,
err
=
conn
.
read
(
buf
)
if
err
!=
nil
{
if
nerr
,
ok
=
err
.
(
net
.
Error
);
ok
&&
nerr
.
Timeout
()
{
...
...
@@ -725,6 +726,7 @@ func (s *session) handleWSPackage() error {
}
pkg
,
err
=
conn
.
read
()
if
nerr
,
ok
=
err
.
(
net
.
Error
);
ok
&&
nerr
.
Timeout
()
{
continue
}
if
err
!=
nil
{
...
...
@@ -761,8 +763,8 @@ func (s *session) stop() {
// let read/Write timeout asap
now
:=
wheel
.
Now
()
if
conn
:=
s
.
Conn
();
conn
!=
nil
{
conn
.
SetReadDeadline
(
now
.
Add
(
s
.
read
Deadline
()))
conn
.
SetWriteDeadline
(
now
.
Add
(
s
.
write
Deadline
()))
conn
.
SetReadDeadline
(
now
.
Add
(
s
.
read
Timeout
()))
conn
.
SetWriteDeadline
(
now
.
Add
(
s
.
write
Timeout
()))
}
close
(
s
.
done
)
})
...
...
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