Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e88f15

Browse files
authored
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.
1 parent f9e594f commit 3e88f15

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

peer/channel.go

Lines changed: 3 additions & 3 deletions
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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ var (
3232
)
3333

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

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

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

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

93-
type ConnOpts struct {
93+
type ConnOptions struct {
9494
Logger slog.Logger
9595

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

@@ -203,7 +203,7 @@ func (c *Conn) init() error {
203203

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

218218
func (c *Conn) pingEchoChannel() (*Channel, error) {
219219
c.pingEchoOnce.Do(func() {
220-
c.pingEchoChan, c.pingEchoError = c.dialChannel(context.Background(), "echo", &ChannelOpts{
220+
c.pingEchoChan, c.pingEchoError = c.dialChannel(context.Background(), "echo", &ChannelOptions{
221221
ID: c.pingEchoChannelID,
222222
Negotiated: true,
223223
OpenOnDisconnect: true,
@@ -377,21 +377,21 @@ func (c *Conn) Accept(ctx context.Context) (*Channel, error) {
377377
case dataChannel = <-c.dcOpenChannel:
378378
}
379379

380-
return newChannel(c, dataChannel, &ChannelOpts{}), nil
380+
return newChannel(c, dataChannel, &ChannelOptions{}), nil
381381
}
382382

383383
// Dial creates a new DataChannel.
384-
func (c *Conn) Dial(ctx context.Context, label string, opts *ChannelOpts) (*Channel, error) {
384+
func (c *Conn) Dial(ctx context.Context, label string, opts *ChannelOptions) (*Channel, error) {
385385
if opts == nil {
386-
opts = &ChannelOpts{}
386+
opts = &ChannelOptions{}
387387
}
388388
if opts.ID == c.pingChannelID || opts.ID == c.pingEchoChannelID {
389389
return nil, xerrors.Errorf("datachannel id %d and %d are reserved for ping", c.pingChannelID, c.pingEchoChannelID)
390390
}
391391
return c.dialChannel(ctx, label, opts)
392392
}
393393

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

peer/conn_test.go

Lines changed: 6 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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)