From 3e88f1502ae320631cd79cc68df72f9aa9c1263c Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Sat, 29 Jan 2022 19:27:44 -0600 Subject: [PATCH] refactor: Remove "Opts" abbreviation (#92) Having a mixture of abbreviations in the codebase reduces clarity. Although opts is common for options, I'd rather set a precedent of clarifying verbosity. --- peer/channel.go | 6 +++--- peer/conn.go | 24 ++++++++++++------------ peer/conn_test.go | 12 ++++++------ peerbroker/dial.go | 2 +- peerbroker/listen.go | 8 ++++---- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/peer/channel.go b/peer/channel.go index 2c95d495c4..150398f4d4 100644 --- a/peer/channel.go +++ b/peer/channel.go @@ -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 { channel := &Channel{ opts: opts, conn: conn, @@ -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 @@ -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 diff --git a/peer/conn.go b/peer/conn.go index fa6081c1ab..800699f2f7 100644 --- a/peer/conn.go +++ b/peer/conn.go @@ -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. @@ -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. @@ -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 @@ -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, @@ -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, @@ -377,13 +377,13 @@ 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) @@ -391,7 +391,7 @@ func (c *Conn) Dial(ctx context.Context, label string, opts *ChannelOpts) (*Chan 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 { diff --git a/peer/conn_test.go b/peer/conn_test.go index a1fd4d7a4c..cc59e0cfbc 100644 --- a/peer/conn_test.go +++ b/peer/conn_test.go @@ -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()) @@ -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) @@ -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) @@ -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 } @@ -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), }) @@ -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), }) diff --git a/peerbroker/dial.go b/peerbroker/dial.go index 10511eed5c..9af9c663be 100644 --- a/peerbroker/dial.go +++ b/peerbroker/dial.go @@ -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 { diff --git a/peerbroker/listen.go b/peerbroker/listen.go index 03f7f00b59..db218a209f 100644 --- a/peerbroker/listen.go +++ b/peerbroker/listen.go @@ -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), @@ -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, }) @@ -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) }