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
e7d1b729
Commit
e7d1b729
authored
Mar 10, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add updateWriteDeadline
parent
d708dcdd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
30 deletions
+29
-30
change_log.md
change_log.md
+3
-2
conn.go
conn.go
+26
-28
No files found.
change_log.md
View file @
e7d1b729
...
@@ -15,8 +15,9 @@
...
@@ -15,8 +15,9 @@
> improvement
> improvement
*
1 rDeadline -> rTimeout
*
1 rDeadline -> rTimeout
*
2 wDeadline -> wTimeout
*
2 wDeadline -> wTimeout
*
3 disable readtimeout in func (w
*
gettyWSConn) read()
*
3 disable readtimeout in gettyWSConn:read
*
4 add gettyWSConn:updateWriteDeadline
-
2018/03/08
-
2018/03/08
> feature
> feature
*
1 add udp client and udp server
*
1 add udp client and udp server
...
...
conn.go
View file @
e7d1b729
...
@@ -375,24 +375,7 @@ func (w *gettyWSConn) SetCompressType(c CompressType) {
...
@@ -375,24 +375,7 @@ func (w *gettyWSConn) SetCompressType(c CompressType) {
}
}
func
(
w
*
gettyWSConn
)
handlePing
(
message
string
)
error
{
func
(
w
*
gettyWSConn
)
handlePing
(
message
string
)
error
{
var
(
err
:=
w
.
writePong
([]
byte
(
message
))
err
error
currentTime
time
.
Time
)
if
w
.
wTimeout
>
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
.
wTimeout
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
wTimeout
));
err
!=
nil
{
return
err
}
w
.
wLastDeadline
=
currentTime
}
}
err
=
w
.
conn
.
WriteMessage
(
websocket
.
PongMessage
,
[]
byte
(
message
))
if
err
==
websocket
.
ErrCloseSent
{
if
err
==
websocket
.
ErrCloseSent
{
err
=
nil
err
=
nil
}
else
if
e
,
ok
:=
err
.
(
net
.
Error
);
ok
&&
e
.
Temporary
()
{
}
else
if
e
,
ok
:=
err
.
(
net
.
Error
);
ok
&&
e
.
Temporary
()
{
...
@@ -426,18 +409,12 @@ func (w *gettyWSConn) read() ([]byte, error) {
...
@@ -426,18 +409,12 @@ func (w *gettyWSConn) read() ([]byte, error) {
return
b
,
e
return
b
,
e
}
}
// websocket connection write
func
(
w
*
gettyWSConn
)
updateWriteDeadline
()
error
{
func
(
w
*
gettyWSConn
)
Write
(
pkg
interface
{})
(
int
,
error
)
{
var
(
var
(
err
error
err
error
currentTime
time
.
Time
currentTime
time
.
Time
ok
bool
p
[]
byte
)
)
if
p
,
ok
=
pkg
.
([]
byte
);
!
ok
{
return
0
,
fmt
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
}
if
w
.
wTimeout
>
0
{
if
w
.
wTimeout
>
0
{
// Optimization: update write deadline only if more than 25%
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// of the last write deadline exceeded.
...
@@ -445,24 +422,45 @@ func (w *gettyWSConn) Write(pkg interface{}) (int, error) {
...
@@ -445,24 +422,45 @@ func (w *gettyWSConn) Write(pkg interface{}) (int, error) {
currentTime
=
wheel
.
Now
()
currentTime
=
wheel
.
Now
()
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
wTimeout
>>
2
)
{
if
currentTime
.
Sub
(
w
.
wLastDeadline
)
>
(
w
.
wTimeout
>>
2
)
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
wTimeout
));
err
!=
nil
{
if
err
=
w
.
conn
.
SetWriteDeadline
(
currentTime
.
Add
(
w
.
wTimeout
));
err
!=
nil
{
return
0
,
err
return
err
}
}
w
.
wLastDeadline
=
currentTime
w
.
wLastDeadline
=
currentTime
}
}
}
}
return
nil
}
// websocket connection write
func
(
w
*
gettyWSConn
)
Write
(
pkg
interface
{})
(
int
,
error
)
{
var
(
ok
bool
p
[]
byte
)
if
p
,
ok
=
pkg
.
([]
byte
);
!
ok
{
return
0
,
fmt
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
}
// atomic.AddUint32(&w.writeCount, 1)
// atomic.AddUint32(&w.writeCount, 1)
atomic
.
AddUint32
(
&
w
.
writeCount
,
(
uint32
)(
len
(
p
)))
atomic
.
AddUint32
(
&
w
.
writeCount
,
(
uint32
)(
len
(
p
)))
// w.conn.SetWriteTimeout(time.Now().Add(w.wTimeout)
)
w
.
updateWriteDeadline
(
)
return
len
(
p
),
w
.
conn
.
WriteMessage
(
websocket
.
BinaryMessage
,
p
)
return
len
(
p
),
w
.
conn
.
WriteMessage
(
websocket
.
BinaryMessage
,
p
)
}
}
func
(
w
*
gettyWSConn
)
writePing
()
error
{
func
(
w
*
gettyWSConn
)
writePing
()
error
{
w
.
updateWriteDeadline
()
return
w
.
conn
.
WriteMessage
(
websocket
.
PingMessage
,
[]
byte
{})
return
w
.
conn
.
WriteMessage
(
websocket
.
PingMessage
,
[]
byte
{})
}
}
func
(
w
*
gettyWSConn
)
writePong
(
message
[]
byte
)
error
{
w
.
updateWriteDeadline
()
return
w
.
conn
.
WriteMessage
(
websocket
.
PongMessage
,
message
)
}
// close websocket connection
// close websocket connection
func
(
w
*
gettyWSConn
)
close
(
waitSec
int
)
{
func
(
w
*
gettyWSConn
)
close
(
waitSec
int
)
{
w
.
updateWriteDeadline
()
w
.
conn
.
WriteMessage
(
websocket
.
CloseMessage
,
[]
byte
(
"bye-bye!!!"
))
w
.
conn
.
WriteMessage
(
websocket
.
CloseMessage
,
[]
byte
(
"bye-bye!!!"
))
conn
:=
w
.
conn
.
UnderlyingConn
()
conn
:=
w
.
conn
.
UnderlyingConn
()
if
tcpConn
,
ok
:=
conn
.
(
*
net
.
TCPConn
);
ok
{
if
tcpConn
,
ok
:=
conn
.
(
*
net
.
TCPConn
);
ok
{
...
@@ -503,7 +501,7 @@ func setUDPSocketOptions(conn *net.UDPConn) error {
...
@@ -503,7 +501,7 @@ func setUDPSocketOptions(conn *net.UDPConn) error {
// create gettyUDPConn
// create gettyUDPConn
func
newGettyUDPConn
(
conn
*
net
.
UDPConn
,
peerUDPAddr
*
net
.
UDPAddr
)
*
gettyUDPConn
{
func
newGettyUDPConn
(
conn
*
net
.
UDPConn
,
peerUDPAddr
*
net
.
UDPAddr
)
*
gettyUDPConn
{
if
conn
==
nil
{
if
conn
==
nil
{
panic
(
"newGetty
WS
Conn(conn):@conn is nil"
)
panic
(
"newGetty
UDP
Conn(conn):@conn is nil"
)
}
}
var
localAddr
,
peerAddr
string
var
localAddr
,
peerAddr
string
...
...
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