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

feat: Durability test RTC connections #329

Merged
merged 19 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix race conditions
  • Loading branch information
kylecarbs committed May 3, 2021
commit 00318a085c2e7154b5b25e0fe42ff9e609655864
24 changes: 16 additions & 8 deletions wsnet/rtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net"
"strings"
"sync"
"time"

"github.com/pion/dtls/v2"
Expand Down Expand Up @@ -168,27 +169,34 @@ func newPeerConnection(servers []webrtc.ICEServer) (*webrtc.PeerConnection, erro

// Proxies ICE candidates using the protocol to a writer.
func proxyICECandidates(conn *webrtc.PeerConnection, w io.Writer) func() {
queue := make([]*webrtc.ICECandidate, 0)
flushed := false
write := func(i *webrtc.ICECandidate) {
b, _ := json.Marshal(&protoMessage{
Candidate: i.ToJSON().Candidate,
})
_, _ = w.Write(b)
}
var (
mut sync.Mutex
queue = []*webrtc.ICECandidate{}
flushed = false
write = func(i *webrtc.ICECandidate) {
b, _ := json.Marshal(&protoMessage{
Candidate: i.ToJSON().Candidate,
})
_, _ = w.Write(b)
}
)

conn.OnICECandidate(func(i *webrtc.ICECandidate) {
if i == nil {
return
}
if !flushed {
mut.Lock()
defer mut.Unlock()
queue = append(queue, i)
return
}

write(i)
})
return func() {
mut.Lock()
defer mut.Unlock()
for _, i := range queue {
write(i)
}
Expand Down
14 changes: 10 additions & 4 deletions wsnet/wsnet_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build !race

package wsnet

import (
Expand All @@ -15,6 +13,7 @@ import (
"math/big"
"net"
"net/http"
"sync"
"testing"
"time"

Expand All @@ -35,14 +34,19 @@ func createDumbBroker(t *testing.T) (connectAddr string, listenAddr string) {
t.Cleanup(func() {
listener.Close()
})
mux := http.NewServeMux()
var sess *yamux.Session
var (
mux = http.NewServeMux()
sess *yamux.Session
mut sync.Mutex
)
mux.HandleFunc("/listen", func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, nil)
if err != nil {
t.Error(err)
}
nc := websocket.NetConn(context.Background(), c, websocket.MessageBinary)
mut.Lock()
defer mut.Unlock()
sess, err = yamux.Client(nc, nil)
if err != nil {
t.Error(err)
Expand All @@ -54,6 +58,8 @@ func createDumbBroker(t *testing.T) (connectAddr string, listenAddr string) {
t.Error(err)
}
nc := websocket.NetConn(context.Background(), c, websocket.MessageBinary)
mut.Lock()
defer mut.Unlock()
oc, err := sess.Open()
if err != nil {
t.Error(err)
Expand Down