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
5969f80f
Commit
5969f80f
authored
Apr 03, 2020
by
watermelo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix: some log format & update WriteBytesArray method
parent
db322363
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
9 additions
and
48 deletions
+9
-48
client_test.go
client_test.go
+0
-2
connection.go
connection.go
+2
-3
session.go
session.go
+7
-43
No files found.
client_test.go
View file @
5969f80f
...
...
@@ -275,8 +275,6 @@ func TestNewWSClient(t *testing.T) {
beforeWritePkgNum
:=
atomic
.
LoadUint32
(
&
conn
.
writePkgNum
)
err
=
ss
.
WriteBytes
([]
byte
(
"hello"
))
assert
.
Equal
(
t
,
beforeWritePkgNum
+
1
,
atomic
.
LoadUint32
(
&
conn
.
writePkgNum
))
err
=
ss
.
WriteBytesArray
([]
byte
(
"hello"
),
[]
byte
(
"hello"
))
assert
.
Equal
(
t
,
beforeWritePkgNum
+
3
,
atomic
.
LoadUint32
(
&
conn
.
writePkgNum
))
err
=
conn
.
writePing
()
assert
.
Nil
(
t
,
err
)
...
...
connection.go
View file @
5969f80f
...
...
@@ -284,7 +284,7 @@ func (t *gettyTCPConn) send(pkg interface{}) (int, error) {
atomic
.
AddUint32
(
&
t
.
writeBytes
,
(
uint32
)(
length
))
atomic
.
AddUint32
(
&
t
.
writePkgNum
,
(
uint32
)(
len
(
buffers
)))
}
log
.
Debug
(
"localAddr: %s, remoteAddr:%s, now:%s, length:%d, err:%s"
,
log
.
Debug
f
(
"localAddr: %s, remoteAddr:%s, now:%s, length:%d, err:%s"
,
t
.
conn
.
LocalAddr
(),
t
.
conn
.
RemoteAddr
(),
currentTime
,
length
,
err
)
return
int
(
length
),
perrors
.
WithStack
(
err
)
}
...
...
@@ -293,13 +293,12 @@ func (t *gettyTCPConn) send(pkg interface{}) (int, error) {
if
length
,
err
=
t
.
writer
.
Write
(
p
);
err
==
nil
{
atomic
.
AddUint32
(
&
t
.
writeBytes
,
(
uint32
)(
len
(
p
)))
}
log
.
Debug
(
"localAddr: %s, remoteAddr:%s, now:%s, length:%d, err:%s"
,
log
.
Debug
f
(
"localAddr: %s, remoteAddr:%s, now:%s, length:%d, err:%s"
,
t
.
conn
.
LocalAddr
(),
t
.
conn
.
RemoteAddr
(),
currentTime
,
length
,
err
)
return
length
,
perrors
.
WithStack
(
err
)
}
return
0
,
perrors
.
Errorf
(
"illegal @pkg{%#v} type"
,
pkg
)
//return length, err
}
// close tcp connection
...
...
session.go
View file @
5969f80f
...
...
@@ -301,7 +301,7 @@ func (s *session) SetWQLen(writeQLen int) {
s
.
lock
.
Lock
()
defer
s
.
lock
.
Unlock
()
s
.
wQ
=
make
(
chan
interface
{},
writeQLen
)
log
.
Debug
(
"%s, [session.SetWQLen] wQ{len:%d, cap:%d}"
,
s
.
Stat
(),
len
(
s
.
wQ
),
cap
(
s
.
wQ
))
log
.
Debug
f
(
"%s, [session.SetWQLen] wQ{len:%d, cap:%d}"
,
s
.
Stat
(),
len
(
s
.
wQ
),
cap
(
s
.
wQ
))
}
// set maximum wait time when session got error or got exit signal
...
...
@@ -404,7 +404,7 @@ func (s *session) WritePkg(pkg interface{}, timeout time.Duration) error {
}
_
,
err
=
s
.
Connection
.
send
(
pkg
)
if
err
!=
nil
{
log
.
Warn
(
"%s, [session.WritePkg] @s.Connection.Write(pkg:%#v) = err:%v"
,
s
.
Stat
(),
pkg
,
err
)
log
.
Warn
f
(
"%s, [session.WritePkg] @s.Connection.Write(pkg:%#v) = err:%v"
,
s
.
Stat
(),
pkg
,
err
)
return
perrors
.
WithStack
(
err
)
}
s
.
incWritePkgNum
()
...
...
@@ -449,46 +449,10 @@ func (s *session) WriteBytesArray(pkgs ...[]byte) error {
return
s
.
WriteBytes
(
pkgs
[
0
])
}
// reduce syscall and memcopy for multiple packages
if
_
,
ok
:=
s
.
Connection
.
(
*
gettyTCPConn
);
ok
{
if
_
,
err
:=
s
.
Connection
.
send
(
pkgs
);
err
!=
nil
{
return
perrors
.
Wrapf
(
err
,
"s.Connection.Write(pkgs num:%d)"
,
len
(
pkgs
))
}
}
// get len
var
(
l
int
err
error
length
int
arrp
*
[]
byte
arr
[]
byte
)
length
=
0
for
i
:=
0
;
i
<
len
(
pkgs
);
i
++
{
length
+=
len
(
pkgs
[
i
])
}
// merge the pkgs
// arr = make([]byte, length)
arrp
=
gxbytes
.
GetBytes
(
length
)
defer
gxbytes
.
PutBytes
(
arrp
)
arr
=
*
arrp
l
=
0
for
i
:=
0
;
i
<
len
(
pkgs
);
i
++
{
copy
(
arr
[
l
:
],
pkgs
[
i
])
l
+=
len
(
pkgs
[
i
])
}
if
err
=
s
.
WriteBytes
(
arr
);
err
!=
nil
{
return
perrors
.
WithStack
(
err
)
// TODO Currently, only TCP is supported.
if
_
,
err
:=
s
.
Connection
.
send
(
pkgs
);
err
!=
nil
{
return
perrors
.
Wrapf
(
err
,
"s.Connection.Write(pkgs num:%d)"
,
len
(
pkgs
))
}
num
:=
len
(
pkgs
)
-
1
for
i
:=
0
;
i
<
num
;
i
++
{
s
.
incWritePkgNum
()
}
return
nil
}
...
...
@@ -574,7 +538,7 @@ LOOP:
continue
}
if
!
flag
{
log
.
Warn
(
"[session.handleLoop] drop write out package %#v"
,
outPkg
)
log
.
Warn
f
(
"[session.handleLoop] drop write out package %#v"
,
outPkg
)
continue
}
...
...
@@ -965,6 +929,6 @@ func (s *session) gc() {
// or (session)handleLoop automatically. It's thread safe.
func
(
s
*
session
)
Close
()
{
s
.
stop
()
log
.
Info
(
"%s closed now. its current gr num is %d"
,
log
.
Info
f
(
"%s closed now. its current gr num is %d"
,
s
.
sessionToken
(),
atomic
.
LoadInt32
(
&
(
s
.
grNum
)))
}
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