Skip to content

Commit 0a9ff49

Browse files
authored
Merge branch 'main' into peerdebug
2 parents f655897 + 3e88f15 commit 0a9ff49

File tree

7 files changed

+44
-27
lines changed

7 files changed

+44
-27
lines changed

.github/workflows/coder.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ jobs:
159159
run:
160160
DB=true gotestsum --jsonfile="gotests.json" --packages="./..." --
161161
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
162-
-count=1 -race -parallel=1
162+
-count=1 -race -parallel=2
163163

164164
- uses: codecov/codecov-action@v2
165165
with:

database/postgres/postgres.go

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package postgres
33
import (
44
"database/sql"
55
"fmt"
6+
"io/ioutil"
7+
"os"
68
"time"
79

810
"github.com/ory/dockertest/v3"
@@ -16,15 +18,29 @@ func Open() (string, func(), error) {
1618
if err != nil {
1719
return "", nil, xerrors.Errorf("create pool: %w", err)
1820
}
21+
tempDir, err := ioutil.TempDir(os.TempDir(), "postgres")
22+
if err != nil {
23+
return "", nil, xerrors.Errorf("create tempdir: %w", err)
24+
}
1925
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
2026
Repository: "postgres",
2127
Tag: "11",
2228
Env: []string{
2329
"POSTGRES_PASSWORD=postgres",
2430
"POSTGRES_USER=postgres",
2531
"POSTGRES_DB=postgres",
32+
// The location for temporary database files!
33+
"PGDATA=/tmp",
2634
"listen_addresses = '*'",
2735
},
36+
Mounts: []string{
37+
// The postgres image has a VOLUME parameter in it's image.
38+
// If we don't mount at this point, Docker will allocate a
39+
// volume for this directory.
40+
//
41+
// This isn't used anyways, since we override PGDATA.
42+
fmt.Sprintf("%s:/var/lib/postgresql/data", tempDir),
43+
},
2844
}, func(config *docker.HostConfig) {
2945
// set AutoRemove to true so that stopped container goes away by itself
3046
config.AutoRemove = true
@@ -57,5 +73,6 @@ func Open() (string, func(), error) {
5773
}
5874
return dbURL, func() {
5975
_ = pool.Purge(resource)
76+
_ = os.RemoveAll(tempDir)
6077
}, nil
6178
}

peer/channel.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const (
2727
// The initialization overrides listener handles, and detaches
2828
// the channel on open. The datachannel should not be manually
2929
// mutated after being passed to this function.
30-
func newChannel(conn *Conn, dc *webrtc.DataChannel, opts *ChannelOpts) *Channel {
30+
func newChannel(conn *Conn, dc *webrtc.DataChannel, opts *ChannelOptions) *Channel {
3131
channel := &Channel{
3232
opts: opts,
3333
conn: conn,
@@ -41,7 +41,7 @@ func newChannel(conn *Conn, dc *webrtc.DataChannel, opts *ChannelOpts) *Channel
4141
return channel
4242
}
4343

44-
type ChannelOpts struct {
44+
type ChannelOptions struct {
4545
// ID is a channel ID that should be used when `Negotiated`
4646
// is true.
4747
ID uint16
@@ -72,7 +72,7 @@ type ChannelOpts struct {
7272
// WebRTC PeerConnection failure. This is done to emulate TCP connections.
7373
// This option can be changed in the options when creating a Channel.
7474
type Channel struct {
75-
opts *ChannelOpts
75+
opts *ChannelOptions
7676

7777
conn *Conn
7878
dc *webrtc.DataChannel

peer/conn.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ var (
3333
)
3434

3535
// Client creates a new client connection.
36-
func Client(servers []webrtc.ICEServer, opts *ConnOpts) (*Conn, error) {
36+
func Client(servers []webrtc.ICEServer, opts *ConnOptions) (*Conn, error) {
3737
return newWithClientOrServer(servers, true, opts)
3838
}
3939

4040
// Server creates a new server connection.
41-
func Server(servers []webrtc.ICEServer, opts *ConnOpts) (*Conn, error) {
41+
func Server(servers []webrtc.ICEServer, opts *ConnOptions) (*Conn, error) {
4242
return newWithClientOrServer(servers, false, opts)
4343
}
4444

4545
// newWithClientOrServer constructs a new connection with the client option.
4646
// nolint:revive
47-
func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOpts) (*Conn, error) {
47+
func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOptions) (*Conn, error) {
4848
if opts == nil {
49-
opts = &ConnOpts{}
49+
opts = &ConnOptions{}
5050
}
5151

5252
// Enables preference to STUN.
@@ -91,7 +91,7 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
9191
return conn, nil
9292
}
9393

94-
type ConnOpts struct {
94+
type ConnOptions struct {
9595
Logger slog.Logger
9696

9797
// Enables customization on the underlying WebRTC connection.
@@ -104,7 +104,7 @@ type ConnOpts struct {
104104
// concurrent-safe webrtc.DataChannel, and standardized errors for connection state.
105105
type Conn struct {
106106
rtc *webrtc.PeerConnection
107-
opts *ConnOpts
107+
opts *ConnOptions
108108
// Determines whether this connection will send the offer or the answer.
109109
offerrer bool
110110

@@ -231,7 +231,7 @@ func (c *Conn) init() error {
231231

232232
func (c *Conn) pingChannel() (*Channel, error) {
233233
c.pingOnce.Do(func() {
234-
c.pingChan, c.pingError = c.dialChannel(context.Background(), "ping", &ChannelOpts{
234+
c.pingChan, c.pingError = c.dialChannel(context.Background(), "ping", &ChannelOptions{
235235
ID: c.pingChannelID,
236236
Negotiated: true,
237237
OpenOnDisconnect: true,
@@ -245,7 +245,7 @@ func (c *Conn) pingChannel() (*Channel, error) {
245245

246246
func (c *Conn) pingEchoChannel() (*Channel, error) {
247247
c.pingEchoOnce.Do(func() {
248-
c.pingEchoChan, c.pingEchoError = c.dialChannel(context.Background(), "echo", &ChannelOpts{
248+
c.pingEchoChan, c.pingEchoError = c.dialChannel(context.Background(), "echo", &ChannelOptions{
249249
ID: c.pingEchoChannelID,
250250
Negotiated: true,
251251
OpenOnDisconnect: true,
@@ -416,21 +416,21 @@ func (c *Conn) Accept(ctx context.Context) (*Channel, error) {
416416
case dataChannel = <-c.dcOpenChannel:
417417
}
418418

419-
return newChannel(c, dataChannel, &ChannelOpts{}), nil
419+
return newChannel(c, dataChannel, &ChannelOptions{}), nil
420420
}
421421

422422
// Dial creates a new DataChannel.
423-
func (c *Conn) Dial(ctx context.Context, label string, opts *ChannelOpts) (*Channel, error) {
423+
func (c *Conn) Dial(ctx context.Context, label string, opts *ChannelOptions) (*Channel, error) {
424424
if opts == nil {
425-
opts = &ChannelOpts{}
425+
opts = &ChannelOptions{}
426426
}
427427
if opts.ID == c.pingChannelID || opts.ID == c.pingEchoChannelID {
428428
return nil, xerrors.Errorf("datachannel id %d and %d are reserved for ping", c.pingChannelID, c.pingEchoChannelID)
429429
}
430430
return c.dialChannel(ctx, label, opts)
431431
}
432432

433-
func (c *Conn) dialChannel(ctx context.Context, label string, opts *ChannelOpts) (*Channel, error) {
433+
func (c *Conn) dialChannel(ctx context.Context, label string, opts *ChannelOptions) (*Channel, error) {
434434
c.opts.Logger.Debug(ctx, "creating data channel", slog.F("label", label), slog.F("opts", opts))
435435
var id *uint16
436436
if opts.ID != 0 {

peer/conn_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestConn(t *testing.T) {
104104
t.Run("Accept", func(t *testing.T) {
105105
t.Parallel()
106106
client, server, _ := createPair(t)
107-
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOpts{})
107+
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOptions{})
108108
require.NoError(t, err)
109109

110110
sch, err := server.Accept(context.Background())
@@ -119,7 +119,7 @@ func TestConn(t *testing.T) {
119119
t.Run("AcceptNetworkOffline", func(t *testing.T) {
120120
t.Parallel()
121121
client, server, wan := createPair(t)
122-
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOpts{})
122+
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOptions{})
123123
require.NoError(t, err)
124124
sch, err := server.Accept(context.Background())
125125
require.NoError(t, err)
@@ -135,7 +135,7 @@ func TestConn(t *testing.T) {
135135
t.Run("Buffering", func(t *testing.T) {
136136
t.Parallel()
137137
client, server, _ := createPair(t)
138-
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOpts{})
138+
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOptions{})
139139
require.NoError(t, err)
140140
sch, err := server.Accept(context.Background())
141141
require.NoError(t, err)
@@ -186,7 +186,7 @@ func TestConn(t *testing.T) {
186186
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
187187
var cch *peer.Channel
188188
defaultTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
189-
cch, err = client.Dial(ctx, "hello", &peer.ChannelOpts{})
189+
cch, err = client.Dial(ctx, "hello", &peer.ChannelOptions{})
190190
if err != nil {
191191
return nil, err
192192
}
@@ -271,7 +271,7 @@ func createPair(t *testing.T) (client *peer.Conn, server *peer.Conn, wan *vnet.R
271271
c1SettingEngine.SetVNet(c1Net)
272272
c1SettingEngine.SetPrflxAcceptanceMinWait(0)
273273
c1SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval)
274-
channel1, err := peer.Client([]webrtc.ICEServer{}, &peer.ConnOpts{
274+
channel1, err := peer.Client([]webrtc.ICEServer{}, &peer.ConnOptions{
275275
SettingEngine: c1SettingEngine,
276276
Logger: slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug),
277277
})
@@ -283,7 +283,7 @@ func createPair(t *testing.T) (client *peer.Conn, server *peer.Conn, wan *vnet.R
283283
c2SettingEngine.SetVNet(c2Net)
284284
c2SettingEngine.SetPrflxAcceptanceMinWait(0)
285285
c2SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval)
286-
channel2, err := peer.Server([]webrtc.ICEServer{}, &peer.ConnOpts{
286+
channel2, err := peer.Server([]webrtc.ICEServer{}, &peer.ConnOptions{
287287
SettingEngine: c2SettingEngine,
288288
Logger: slogtest.Make(t, nil).Named("server").Leveled(slog.LevelDebug),
289289
})

peerbroker/dial.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// Dial consumes the PeerBroker gRPC connection negotiation stream to produce a WebRTC peered connection.
14-
func Dial(stream proto.DRPCPeerBroker_NegotiateConnectionClient, iceServers []webrtc.ICEServer, opts *peer.ConnOpts) (*peer.Conn, error) {
14+
func Dial(stream proto.DRPCPeerBroker_NegotiateConnectionClient, iceServers []webrtc.ICEServer, opts *peer.ConnOptions) (*peer.Conn, error) {
1515
// Convert WebRTC ICE servers to the protobuf type.
1616
protoIceServers := make([]*proto.WebRTCICEServer, 0, len(iceServers))
1717
for _, iceServer := range iceServers {

peerbroker/listen.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
// Listen consumes the transport as the server-side of the PeerBroker dRPC service.
2121
// The Accept function must be serviced, or new connections will hang.
22-
func Listen(transport drpc.Transport, opts *peer.ConnOpts) (*Listener, error) {
22+
func Listen(transport drpc.Transport, opts *peer.ConnOptions) (*Listener, error) {
2323
ctx, cancelFunc := context.WithCancel(context.Background())
2424
listener := &Listener{
2525
connectionChannel: make(chan *peer.Conn),
@@ -30,7 +30,7 @@ func Listen(transport drpc.Transport, opts *peer.ConnOpts) (*Listener, error) {
3030

3131
mux := drpcmux.New()
3232
err := proto.DRPCRegisterPeerBroker(mux, &peerBrokerService{
33-
connOpts: opts,
33+
connOptions: opts,
3434

3535
listener: listener,
3636
})
@@ -99,13 +99,13 @@ func (l *Listener) isClosed() bool {
9999
type peerBrokerService struct {
100100
listener *Listener
101101

102-
connOpts *peer.ConnOpts
102+
connOptions *peer.ConnOptions
103103
}
104104

105105
// NegotiateConnection negotiates a WebRTC connection.
106106
func (b *peerBrokerService) NegotiateConnection(stream proto.DRPCPeerBroker_NegotiateConnectionStream) error {
107107
// Start with no ICE servers. They can be sent by the client if provided.
108-
peerConn, err := peer.Server([]webrtc.ICEServer{}, b.connOpts)
108+
peerConn, err := peer.Server([]webrtc.ICEServer{}, b.connOptions)
109109
if err != nil {
110110
return xerrors.Errorf("create peer connection: %w", err)
111111
}

0 commit comments

Comments
 (0)