Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-ws-transport
Commits
2c77fd7a
Unverified
Commit
2c77fd7a
authored
Apr 22, 2020
by
Steven Allen
Committed by
GitHub
Apr 22, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #85 from libp2p/fix/deadline-race
fix: add read/write locks
parents
b2d49c9d
f7e5cea2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
0 deletions
+16
-0
conn_native.go
conn_native.go
+16
-0
No files found.
conn_native.go
View file @
2c77fd7a
...
...
@@ -17,9 +17,14 @@ type Conn struct {
DefaultMessageType
int
reader
io
.
Reader
closeOnce
sync
.
Once
readLock
,
writeLock
sync
.
Mutex
}
func
(
c
*
Conn
)
Read
(
b
[]
byte
)
(
int
,
error
)
{
c
.
readLock
.
Lock
()
defer
c
.
readLock
.
Unlock
()
if
c
.
reader
==
nil
{
if
err
:=
c
.
prepNextReader
();
err
!=
nil
{
return
0
,
err
...
...
@@ -67,6 +72,9 @@ func (c *Conn) prepNextReader() error {
}
func
(
c
*
Conn
)
Write
(
b
[]
byte
)
(
n
int
,
err
error
)
{
c
.
writeLock
.
Lock
()
defer
c
.
writeLock
.
Unlock
()
if
err
:=
c
.
Conn
.
WriteMessage
(
c
.
DefaultMessageType
,
b
);
err
!=
nil
{
return
0
,
err
}
...
...
@@ -113,10 +121,18 @@ func (c *Conn) SetDeadline(t time.Time) error {
}
func
(
c
*
Conn
)
SetReadDeadline
(
t
time
.
Time
)
error
{
// Don't lock when setting the read deadline. That would prevent us from
// interrupting an in-progress read.
return
c
.
Conn
.
SetReadDeadline
(
t
)
}
func
(
c
*
Conn
)
SetWriteDeadline
(
t
time
.
Time
)
error
{
// Unlike the read deadline, we need to lock when setting the write
// deadline.
c
.
writeLock
.
Lock
()
defer
c
.
writeLock
.
Unlock
()
return
c
.
Conn
.
SetWriteDeadline
(
t
)
}
...
...
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