Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
Isolate race condition further
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarbs committed Jun 15, 2021
1 parent 5ff4902 commit 0dc0e56
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
19 changes: 14 additions & 5 deletions wsnet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,25 @@ type conn struct {
rw datachannel.ReadWriteCloser

sendMore chan struct{}
closedMutex sync.Mutex
closedMutex sync.RWMutex
closed bool

writeMutex sync.Mutex
}

func (c *conn) init() {
c.sendMore = make(chan struct{})
c.sendMore = make(chan struct{}, 1)
c.dc.SetBufferedAmountLowThreshold(bufferedAmountLowThreshold)
c.dc.OnBufferedAmountLow(func() {
c.closedMutex.Lock()
defer c.closedMutex.Unlock()
c.closedMutex.RLock()
defer c.closedMutex.RUnlock()
if c.closed {
return
}
c.sendMore <- struct{}{}
select {
case c.sendMore <- struct{}{}:
default:
}
})
}

Expand All @@ -78,12 +83,16 @@ func (c *conn) Read(b []byte) (n int, err error) {
}

func (c *conn) Write(b []byte) (n int, err error) {
c.writeMutex.Lock()
defer c.writeMutex.Unlock()
if len(b) > maxMessageLength {
return 0, fmt.Errorf("outbound packet larger than maximum message size: %d", maxMessageLength)
}
if c.dc.BufferedAmount()+uint64(len(b)) >= maxBufferedAmount {
<-c.sendMore
}
// Uncomment this line for it to work.
// time.Sleep(time.Microsecond)
return c.rw.Write(b)
}

Expand Down
3 changes: 2 additions & 1 deletion wsnet/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ func BenchmarkThroughput(b *testing.B) {
sizes := []int64{
// 4,
// 16,
128,
// 256,
// 1024,
// 4096,
// 16384,
32768,
// 32768,
}

listener, err := net.Listen("tcp", "0.0.0.0:0")
Expand Down
2 changes: 1 addition & 1 deletion wsnet/rtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func newPeerConnection(servers []webrtc.ICEServer) (*webrtc.PeerConnection, erro
se.DetachDataChannels()
se.SetICETimeouts(time.Second*5, time.Second*5, time.Second*2)
lf := logging.NewDefaultLoggerFactory()
lf.DefaultLogLevel = logging.LogLevelDebug
lf.DefaultLogLevel = logging.LogLevelDisabled
se.LoggerFactory = lf

// If one server is provided and we know it's TURN, we can set the
Expand Down

0 comments on commit 0dc0e56

Please sign in to comment.