Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions peer/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// The initialization overrides listener handles, and detaches
// the channel on open. The datachannel should not be manually
// mutated after being passed to this function.
func newChannel(conn *Conn, dc *webrtc.DataChannel, opts *ChannelOpts) *Channel {
func newChannel(conn *Conn, dc *webrtc.DataChannel, opts *ChannelOptions) *Channel {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider following up with a ruleguard linter rule that checks that types don't end in Opts, and suggesting Options instead

channel := &Channel{
opts: opts,
conn: conn,
Expand All @@ -41,7 +41,7 @@ func newChannel(conn *Conn, dc *webrtc.DataChannel, opts *ChannelOpts) *Channel
return channel
}

type ChannelOpts struct {
type ChannelOptions struct {
// ID is a channel ID that should be used when `Negotiated`
// is true.
ID uint16
Expand Down Expand Up @@ -72,7 +72,7 @@ type ChannelOpts struct {
// WebRTC PeerConnection failure. This is done to emulate TCP connections.
// This option can be changed in the options when creating a Channel.
type Channel struct {
opts *ChannelOpts
opts *ChannelOptions

conn *Conn
dc *webrtc.DataChannel
Expand Down
24 changes: 12 additions & 12 deletions peer/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ var (
)

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

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

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

// Enables preference to STUN.
Expand Down Expand Up @@ -90,7 +90,7 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
return conn, nil
}

type ConnOpts struct {
type ConnOptions struct {
Logger slog.Logger

// Enables customization on the underlying WebRTC connection.
Expand All @@ -103,7 +103,7 @@ type ConnOpts struct {
// concurrent-safe webrtc.DataChannel, and standardized errors for connection state.
type Conn struct {
rtc *webrtc.PeerConnection
opts *ConnOpts
opts *ConnOptions
// Determines whether this connection will send the offer or the answer.
offerrer bool

Expand Down Expand Up @@ -203,7 +203,7 @@ func (c *Conn) init() error {

func (c *Conn) pingChannel() (*Channel, error) {
c.pingOnce.Do(func() {
c.pingChan, c.pingError = c.dialChannel(context.Background(), "ping", &ChannelOpts{
c.pingChan, c.pingError = c.dialChannel(context.Background(), "ping", &ChannelOptions{
ID: c.pingChannelID,
Negotiated: true,
OpenOnDisconnect: true,
Expand All @@ -217,7 +217,7 @@ func (c *Conn) pingChannel() (*Channel, error) {

func (c *Conn) pingEchoChannel() (*Channel, error) {
c.pingEchoOnce.Do(func() {
c.pingEchoChan, c.pingEchoError = c.dialChannel(context.Background(), "echo", &ChannelOpts{
c.pingEchoChan, c.pingEchoError = c.dialChannel(context.Background(), "echo", &ChannelOptions{
ID: c.pingEchoChannelID,
Negotiated: true,
OpenOnDisconnect: true,
Expand Down Expand Up @@ -377,21 +377,21 @@ func (c *Conn) Accept(ctx context.Context) (*Channel, error) {
case dataChannel = <-c.dcOpenChannel:
}

return newChannel(c, dataChannel, &ChannelOpts{}), nil
return newChannel(c, dataChannel, &ChannelOptions{}), nil
}

// Dial creates a new DataChannel.
func (c *Conn) Dial(ctx context.Context, label string, opts *ChannelOpts) (*Channel, error) {
func (c *Conn) Dial(ctx context.Context, label string, opts *ChannelOptions) (*Channel, error) {
if opts == nil {
opts = &ChannelOpts{}
opts = &ChannelOptions{}
}
if opts.ID == c.pingChannelID || opts.ID == c.pingEchoChannelID {
return nil, xerrors.Errorf("datachannel id %d and %d are reserved for ping", c.pingChannelID, c.pingEchoChannelID)
}
return c.dialChannel(ctx, label, opts)
}

func (c *Conn) dialChannel(ctx context.Context, label string, opts *ChannelOpts) (*Channel, error) {
func (c *Conn) dialChannel(ctx context.Context, label string, opts *ChannelOptions) (*Channel, error) {
c.opts.Logger.Debug(ctx, "creating data channel", slog.F("label", label), slog.F("opts", opts))
var id *uint16
if opts.ID != 0 {
Expand Down
12 changes: 6 additions & 6 deletions peer/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestConn(t *testing.T) {
t.Run("Accept", func(t *testing.T) {
t.Parallel()
client, server, _ := createPair(t)
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOpts{})
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOptions{})
require.NoError(t, err)

sch, err := server.Accept(context.Background())
Expand All @@ -119,7 +119,7 @@ func TestConn(t *testing.T) {
t.Run("AcceptNetworkOffline", func(t *testing.T) {
t.Parallel()
client, server, wan := createPair(t)
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOpts{})
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOptions{})
require.NoError(t, err)
sch, err := server.Accept(context.Background())
require.NoError(t, err)
Expand All @@ -135,7 +135,7 @@ func TestConn(t *testing.T) {
t.Run("Buffering", func(t *testing.T) {
t.Parallel()
client, server, _ := createPair(t)
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOpts{})
cch, err := client.Dial(context.Background(), "hello", &peer.ChannelOptions{})
require.NoError(t, err)
sch, err := server.Accept(context.Background())
require.NoError(t, err)
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestConn(t *testing.T) {
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
var cch *peer.Channel
defaultTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
cch, err = client.Dial(ctx, "hello", &peer.ChannelOpts{})
cch, err = client.Dial(ctx, "hello", &peer.ChannelOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -271,7 +271,7 @@ func createPair(t *testing.T) (client *peer.Conn, server *peer.Conn, wan *vnet.R
c1SettingEngine.SetVNet(c1Net)
c1SettingEngine.SetPrflxAcceptanceMinWait(0)
c1SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval)
channel1, err := peer.Client([]webrtc.ICEServer{}, &peer.ConnOpts{
channel1, err := peer.Client([]webrtc.ICEServer{}, &peer.ConnOptions{
SettingEngine: c1SettingEngine,
Logger: slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug),
})
Expand All @@ -283,7 +283,7 @@ func createPair(t *testing.T) (client *peer.Conn, server *peer.Conn, wan *vnet.R
c2SettingEngine.SetVNet(c2Net)
c2SettingEngine.SetPrflxAcceptanceMinWait(0)
c2SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval)
channel2, err := peer.Server([]webrtc.ICEServer{}, &peer.ConnOpts{
channel2, err := peer.Server([]webrtc.ICEServer{}, &peer.ConnOptions{
SettingEngine: c2SettingEngine,
Logger: slogtest.Make(t, nil).Named("server").Leveled(slog.LevelDebug),
})
Expand Down
2 changes: 1 addition & 1 deletion peerbroker/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// Dial consumes the PeerBroker gRPC connection negotiation stream to produce a WebRTC peered connection.
func Dial(stream proto.DRPCPeerBroker_NegotiateConnectionClient, iceServers []webrtc.ICEServer, opts *peer.ConnOpts) (*peer.Conn, error) {
func Dial(stream proto.DRPCPeerBroker_NegotiateConnectionClient, iceServers []webrtc.ICEServer, opts *peer.ConnOptions) (*peer.Conn, error) {
// Convert WebRTC ICE servers to the protobuf type.
protoIceServers := make([]*proto.WebRTCICEServer, 0, len(iceServers))
for _, iceServer := range iceServers {
Expand Down
8 changes: 4 additions & 4 deletions peerbroker/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

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

mux := drpcmux.New()
err := proto.DRPCRegisterPeerBroker(mux, &peerBrokerService{
connOpts: opts,
connOptions: opts,

listener: listener,
})
Expand Down Expand Up @@ -99,13 +99,13 @@ func (l *Listener) isClosed() bool {
type peerBrokerService struct {
listener *Listener

connOpts *peer.ConnOpts
connOptions *peer.ConnOptions
}

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