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.
This commit is contained in:
Kyle Carberry
2022-01-29 19:27:44 -06:00
committed by GitHub
parent f9e594fbad
commit 3e88f1502a
5 changed files with 26 additions and 26 deletions

View File

@ -27,7 +27,7 @@ const (
// The initialization overrides listener handles, and detaches // The initialization overrides listener handles, and detaches
// the channel on open. The datachannel should not be manually // the channel on open. The datachannel should not be manually
// mutated after being passed to this function. // 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{ channel := &Channel{
opts: opts, opts: opts,
conn: conn, conn: conn,
@ -41,7 +41,7 @@ func newChannel(conn *Conn, dc *webrtc.DataChannel, opts *ChannelOpts) *Channel
return channel return channel
} }
type ChannelOpts struct { type ChannelOptions struct {
// ID is a channel ID that should be used when `Negotiated` // ID is a channel ID that should be used when `Negotiated`
// is true. // is true.
ID uint16 ID uint16
@ -72,7 +72,7 @@ type ChannelOpts struct {
// WebRTC PeerConnection failure. This is done to emulate TCP connections. // WebRTC PeerConnection failure. This is done to emulate TCP connections.
// This option can be changed in the options when creating a Channel. // This option can be changed in the options when creating a Channel.
type Channel struct { type Channel struct {
opts *ChannelOpts opts *ChannelOptions
conn *Conn conn *Conn
dc *webrtc.DataChannel dc *webrtc.DataChannel

View File

@ -32,20 +32,20 @@ var (
) )
// Client creates a new client connection. // 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) return newWithClientOrServer(servers, true, opts)
} }
// Server creates a new server connection. // 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) return newWithClientOrServer(servers, false, opts)
} }
// newWithClientOrServer constructs a new connection with the client option. // newWithClientOrServer constructs a new connection with the client option.
// nolint:revive // 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 { if opts == nil {
opts = &ConnOpts{} opts = &ConnOptions{}
} }
// Enables preference to STUN. // Enables preference to STUN.
@ -90,7 +90,7 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
return conn, nil return conn, nil
} }
type ConnOpts struct { type ConnOptions struct {
Logger slog.Logger Logger slog.Logger
// Enables customization on the underlying WebRTC connection. // Enables customization on the underlying WebRTC connection.
@ -103,7 +103,7 @@ type ConnOpts struct {
// concurrent-safe webrtc.DataChannel, and standardized errors for connection state. // concurrent-safe webrtc.DataChannel, and standardized errors for connection state.
type Conn struct { type Conn struct {
rtc *webrtc.PeerConnection rtc *webrtc.PeerConnection
opts *ConnOpts opts *ConnOptions
// Determines whether this connection will send the offer or the answer. // Determines whether this connection will send the offer or the answer.
offerrer bool offerrer bool
@ -203,7 +203,7 @@ func (c *Conn) init() error {
func (c *Conn) pingChannel() (*Channel, error) { func (c *Conn) pingChannel() (*Channel, error) {
c.pingOnce.Do(func() { 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, ID: c.pingChannelID,
Negotiated: true, Negotiated: true,
OpenOnDisconnect: true, OpenOnDisconnect: true,
@ -217,7 +217,7 @@ func (c *Conn) pingChannel() (*Channel, error) {
func (c *Conn) pingEchoChannel() (*Channel, error) { func (c *Conn) pingEchoChannel() (*Channel, error) {
c.pingEchoOnce.Do(func() { 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, ID: c.pingEchoChannelID,
Negotiated: true, Negotiated: true,
OpenOnDisconnect: true, OpenOnDisconnect: true,
@ -377,13 +377,13 @@ func (c *Conn) Accept(ctx context.Context) (*Channel, error) {
case dataChannel = <-c.dcOpenChannel: case dataChannel = <-c.dcOpenChannel:
} }
return newChannel(c, dataChannel, &ChannelOpts{}), nil return newChannel(c, dataChannel, &ChannelOptions{}), nil
} }
// Dial creates a new DataChannel. // 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 { if opts == nil {
opts = &ChannelOpts{} opts = &ChannelOptions{}
} }
if opts.ID == c.pingChannelID || opts.ID == c.pingEchoChannelID { 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 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) 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)) c.opts.Logger.Debug(ctx, "creating data channel", slog.F("label", label), slog.F("opts", opts))
var id *uint16 var id *uint16
if opts.ID != 0 { if opts.ID != 0 {

View File

@ -104,7 +104,7 @@ func TestConn(t *testing.T) {
t.Run("Accept", func(t *testing.T) { t.Run("Accept", func(t *testing.T) {
t.Parallel() t.Parallel()
client, server, _ := createPair(t) 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) require.NoError(t, err)
sch, err := server.Accept(context.Background()) sch, err := server.Accept(context.Background())
@ -119,7 +119,7 @@ func TestConn(t *testing.T) {
t.Run("AcceptNetworkOffline", func(t *testing.T) { t.Run("AcceptNetworkOffline", func(t *testing.T) {
t.Parallel() t.Parallel()
client, server, wan := createPair(t) 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) require.NoError(t, err)
sch, err := server.Accept(context.Background()) sch, err := server.Accept(context.Background())
require.NoError(t, err) require.NoError(t, err)
@ -135,7 +135,7 @@ func TestConn(t *testing.T) {
t.Run("Buffering", func(t *testing.T) { t.Run("Buffering", func(t *testing.T) {
t.Parallel() t.Parallel()
client, server, _ := createPair(t) 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) require.NoError(t, err)
sch, err := server.Accept(context.Background()) sch, err := server.Accept(context.Background())
require.NoError(t, err) require.NoError(t, err)
@ -186,7 +186,7 @@ func TestConn(t *testing.T) {
defaultTransport := http.DefaultTransport.(*http.Transport).Clone() defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
var cch *peer.Channel var cch *peer.Channel
defaultTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { 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 { if err != nil {
return nil, err 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.SetVNet(c1Net)
c1SettingEngine.SetPrflxAcceptanceMinWait(0) c1SettingEngine.SetPrflxAcceptanceMinWait(0)
c1SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval) c1SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval)
channel1, err := peer.Client([]webrtc.ICEServer{}, &peer.ConnOpts{ channel1, err := peer.Client([]webrtc.ICEServer{}, &peer.ConnOptions{
SettingEngine: c1SettingEngine, SettingEngine: c1SettingEngine,
Logger: slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug), 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.SetVNet(c2Net)
c2SettingEngine.SetPrflxAcceptanceMinWait(0) c2SettingEngine.SetPrflxAcceptanceMinWait(0)
c2SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval) c2SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval)
channel2, err := peer.Server([]webrtc.ICEServer{}, &peer.ConnOpts{ channel2, err := peer.Server([]webrtc.ICEServer{}, &peer.ConnOptions{
SettingEngine: c2SettingEngine, SettingEngine: c2SettingEngine,
Logger: slogtest.Make(t, nil).Named("server").Leveled(slog.LevelDebug), Logger: slogtest.Make(t, nil).Named("server").Leveled(slog.LevelDebug),
}) })

View File

@ -11,7 +11,7 @@ import (
) )
// Dial consumes the PeerBroker gRPC connection negotiation stream to produce a WebRTC peered connection. // 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. // Convert WebRTC ICE servers to the protobuf type.
protoIceServers := make([]*proto.WebRTCICEServer, 0, len(iceServers)) protoIceServers := make([]*proto.WebRTCICEServer, 0, len(iceServers))
for _, iceServer := range iceServers { for _, iceServer := range iceServers {

View File

@ -19,7 +19,7 @@ import (
// Listen consumes the transport as the server-side of the PeerBroker dRPC service. // Listen consumes the transport as the server-side of the PeerBroker dRPC service.
// The Accept function must be serviced, or new connections will hang. // 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()) ctx, cancelFunc := context.WithCancel(context.Background())
listener := &Listener{ listener := &Listener{
connectionChannel: make(chan *peer.Conn), connectionChannel: make(chan *peer.Conn),
@ -30,7 +30,7 @@ func Listen(transport drpc.Transport, opts *peer.ConnOpts) (*Listener, error) {
mux := drpcmux.New() mux := drpcmux.New()
err := proto.DRPCRegisterPeerBroker(mux, &peerBrokerService{ err := proto.DRPCRegisterPeerBroker(mux, &peerBrokerService{
connOpts: opts, connOptions: opts,
listener: listener, listener: listener,
}) })
@ -99,13 +99,13 @@ func (l *Listener) isClosed() bool {
type peerBrokerService struct { type peerBrokerService struct {
listener *Listener listener *Listener
connOpts *peer.ConnOpts connOptions *peer.ConnOptions
} }
// NegotiateConnection negotiates a WebRTC connection. // NegotiateConnection negotiates a WebRTC connection.
func (b *peerBrokerService) NegotiateConnection(stream proto.DRPCPeerBroker_NegotiateConnectionStream) error { func (b *peerBrokerService) NegotiateConnection(stream proto.DRPCPeerBroker_NegotiateConnectionStream) error {
// Start with no ICE servers. They can be sent by the client if provided. // 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 { if err != nil {
return xerrors.Errorf("create peer connection: %w", err) return xerrors.Errorf("create peer connection: %w", err)
} }