sdk: remove unnecessary rd prefix
Kim committed
Nov 18, 2025 at 10:45 UTC
fa9fdbdb1a9b3822820db38e6d01b7613a19e4b7
4 files changed
+92
-92
cmd/demo-app/main.go
+1
-1
@@ -84,7 +84,7 @@ func runDemo() error {
84
cred := sdk.NewCredential()
85
86
// 2) Create SDK client and connect to relay(s)
87
- client, err := sdk.NewClient(func(c *sdk.RDClientConfig) {
87
+ client, err := sdk.NewClient(func(c *sdk.ClientConfig) {
88
c.BootstrapServers = []string{flagServerURL}
89
})
90
if err != nil {
cmd/portal-tunnel/main.go
+1
-1
@@ -219,7 +219,7 @@ func runServiceTunnel(ctx context.Context, relayDir *RelayDirectory, service *Se
219
log.Info().Str("service", serviceName).Msgf(" Relays: %s", strings.Join(bootstrapServers, ", "))
220
log.Info().Str("service", serviceName).Msgf(" Lease ID: %s", leaseID)
221
222
- client, err := sdk.NewClient(func(c *sdk.RDClientConfig) {
222
+ client, err := sdk.NewClient(func(c *sdk.ClientConfig) {
223
c.BootstrapServers = bootstrapServers
224
})
225
if err != nil {
sdk/sdk.go
+84
-84
@@ -19,7 +19,7 @@ import (
19
"gosuda.org/portal/portal/core/proto/rdverb"
20
)
21
22
-type RDClientConfig struct {
22
+type ClientConfig struct {
23
BootstrapServers []string
24
Dialer func(context.Context, string) (io.ReadWriteCloser, error)
25
HealthCheckInterval time.Duration // Interval for health checks (default: 10 seconds)
@@ -27,39 +27,39 @@ type RDClientConfig struct {
27
ReconnectInterval time.Duration // Interval between reconnection attempts (default: 5 seconds)
28
}
29
30
-type Option func(*RDClientConfig)
30
+type Option func(*ClientConfig)
31
32
func WithBootstrapServers(servers []string) Option {
33
- return func(c *RDClientConfig) {
33
+ return func(c *ClientConfig) {
34
c.BootstrapServers = servers
35
}
36
}
37
38
func WithDialer(dialer func(context.Context, string) (io.ReadWriteCloser, error)) Option {
39
- return func(c *RDClientConfig) {
39
+ return func(c *ClientConfig) {
40
c.Dialer = dialer
41
}
42
}
43
44
func WithHealthCheckInterval(interval time.Duration) Option {
45
- return func(c *RDClientConfig) {
45
+ return func(c *ClientConfig) {
46
c.HealthCheckInterval = interval
47
}
48
}
49
50
func WithReconnectMaxRetries(retries int) Option {
51
- return func(c *RDClientConfig) {
51
+ return func(c *ClientConfig) {
52
c.ReconnectMaxRetries = retries
53
}
54
}
55
56
func WithReconnectInterval(interval time.Duration) Option {
57
- return func(c *RDClientConfig) {
57
+ return func(c *ClientConfig) {
58
c.ReconnectInterval = interval
59
}
60
}
61
62
-type rdRelay struct {
62
+type connRelay struct {
63
addr string
64
client *portal.RelayClient
65
dialer func(context.Context, string) (io.ReadWriteCloser, error)
@@ -68,68 +68,68 @@ type rdRelay struct {
68
mu sync.Mutex
69
}
70
71
-var _ net.Conn = (*RDConnection)(nil)
71
+var _ net.Conn = (*connection)(nil)
72
73
-type RDConnection struct {
74
- via *rdRelay
73
+type connection struct {
74
+ via *connRelay
75
localAddr string
76
remoteAddr string
77
conn *cryptoops.SecureConnection
78
}
79
80
-// Implement net.Conn interface for RDConnection
81
-func (r *RDConnection) Read(b []byte) (n int, err error) {
80
+func (r *connection) Read(b []byte) (n int, err error) {
81
return r.conn.Read(b)
82
}
83
85
-func (r *RDConnection) Write(b []byte) (n int, err error) {
84
+func (r *connection) Write(b []byte) (n int, err error) {
85
return r.conn.Write(b)
86
}
87
89
-func (r *RDConnection) Close() error {
88
+func (r *connection) Close() error {
89
return r.conn.Close()
90
}
91
93
-func (r *RDConnection) LocalAddr() net.Addr {
94
- return rdAddr(r.localAddr)
92
+func (r *connection) LocalAddr() net.Addr {
93
+ return addr(r.localAddr)
94
}
95
97
-func (r *RDConnection) RemoteAddr() net.Addr {
98
- return rdAddr(r.remoteAddr)
96
+func (r *connection) RemoteAddr() net.Addr {
97
+ return addr(r.remoteAddr)
98
}
99
101
-func (r *RDConnection) SetDeadline(t time.Time) error {
100
+func (r *connection) SetDeadline(t time.Time) error {
101
return r.conn.SetDeadline(t)
102
}
103
105
-func (r *RDConnection) SetReadDeadline(t time.Time) error {
104
+func (r *connection) SetReadDeadline(t time.Time) error {
105
return r.conn.SetReadDeadline(t)
106
}
107
109
-func (r *RDConnection) SetWriteDeadline(t time.Time) error {
108
+func (r *connection) SetWriteDeadline(t time.Time) error {
109
return r.conn.SetWriteDeadline(t)
110
}
111
113
-// rdAddr implements net.Addr
114
-type rdAddr string
112
+var _ net.Addr = (*addr)(nil)
113
116
-func (a rdAddr) Network() string {
114
+type addr string
115
+
116
+func (a addr) Network() string {
117
return "portal"
118
}
119
120
-func (a rdAddr) String() string {
120
+func (a addr) String() string {
121
return string(a)
122
}
123
124
-type RDListener struct {
124
+type Listener struct {
125
mu sync.Mutex
126
127
cred *cryptoops.Credential
128
lease *rdverb.Lease
129
130
- conns map[*RDConnection]struct{}
130
+ conns map[*connection]struct{}
131
132
- connCh chan *RDConnection
132
+ connCh chan *connection
133
closed bool
134
}
135
@@ -180,12 +180,12 @@ func WithHide(hide bool) MetadataOption {
180
}
181
}
182
183
-type RDClient struct {
183
+type Client struct {
184
mu sync.Mutex
185
186
- relays map[string]*rdRelay
187
- listeners map[string]*RDListener
188
- config *RDClientConfig
186
+ relays map[string]*connRelay
187
+ listeners map[string]*Listener
188
+ config *ClientConfig
189
190
stopch chan struct{}
191
stopOnce sync.Once // Ensure stopch is closed only once
@@ -203,10 +203,10 @@ var (
203
ErrInvalidMetadata = errors.New("invalid metadata")
204
)
205
206
-func NewClient(opt ...Option) (*RDClient, error) {
207
- log.Debug().Msg("[SDK] Creating new RDClient")
206
+func NewClient(opt ...Option) (*Client, error) {
207
+ log.Debug().Msg("[SDK] Creating new Client")
208
209
- config := &RDClientConfig{
209
+ config := &ClientConfig{
210
Dialer: newWebSocketDialer(),
211
HealthCheckInterval: 10 * time.Second,
212
ReconnectMaxRetries: 0,
@@ -217,9 +217,9 @@ func NewClient(opt ...Option) (*RDClient, error) {
217
o(config)
218
}
219
220
- client := &RDClient{
221
- relays: make(map[string]*rdRelay),
222
- listeners: make(map[string]*RDListener),
220
+ client := &Client{
221
+ relays: make(map[string]*connRelay),
222
+ listeners: make(map[string]*Listener),
223
config: config,
224
stopch: make(chan struct{}),
225
}
@@ -258,17 +258,17 @@ func NewClient(opt ...Option) (*RDClient, error) {
258
return nil, fmt.Errorf("failed to connect to any bootstrap servers: %v", connectionErrors)
259
}
260
261
- log.Debug().Int("relay_count", len(client.relays)).Msg("[SDK] RDClient created successfully")
261
+ log.Debug().Int("relay_count", len(client.relays)).Msg("[SDK] Client created successfully")
262
return client, nil
263
}
264
265
-func (g *RDClient) Dial(cred *cryptoops.Credential, leaseID string, alpn string) (*RDConnection, error) {
265
+func (g *Client) Dial(cred *cryptoops.Credential, leaseID string, alpn string) (*connection, error) {
266
log.Debug().
267
Str("lease_id", leaseID).
268
Str("alpn", alpn).
269
Msg("[SDK] Dialing to lease")
270
271
- var relays []*rdRelay
271
+ var relays []*connRelay
272
273
g.mu.Lock()
274
for _, server := range g.relays {
@@ -280,11 +280,11 @@ func (g *RDClient) Dial(cred *cryptoops.Credential, leaseID string, alpn string)
280
281
var wg sync.WaitGroup
282
var availableRelaysMu sync.Mutex
283
- var availableRelays []*rdRelay
283
+ var availableRelays []*connRelay
284
285
for _, relay := range relays {
286
wg.Add(1)
287
- go func(relay *rdRelay) {
287
+ go func(relay *connRelay) {
288
defer wg.Done()
289
info, err := relay.client.GetRelayInfo()
290
if err != nil {
@@ -329,14 +329,14 @@ func (g *RDClient) Dial(cred *cryptoops.Credential, leaseID string, alpn string)
329
Str("local", conn.LocalID()).
330
Str("remote", conn.RemoteID()).
331
Msg("[SDK] Connection established successfully")
332
- return &RDConnection{via: relay, conn: conn, localAddr: conn.LocalID(), remoteAddr: conn.RemoteID()}, nil
332
+ return &connection{via: relay, conn: conn, localAddr: conn.LocalID(), remoteAddr: conn.RemoteID()}, nil
333
}
334
335
log.Warn().Str("lease_id", leaseID).Msg("[SDK] All connection attempts failed")
336
return nil, ErrNoAvailableRelay
337
}
338
339
-func (g *RDClient) Listen(cred *cryptoops.Credential, name string, alpns []string, options ...MetadataOption) (*RDListener, error) {
339
+func (g *Client) Listen(cred *cryptoops.Credential, name string, alpns []string, options ...MetadataOption) (*Listener, error) {
340
log.Debug().
341
Str("lease_id", cred.ID()).
342
Str("name", name).
@@ -395,11 +395,11 @@ func (g *RDClient) Listen(cred *cryptoops.Credential, name string, alpns []strin
395
}
396
397
// Create listener with lease metadata for re-registration
398
- listener := &RDListener{
398
+ listener := &Listener{
399
cred: cred,
400
lease: lease,
401
- conns: make(map[*RDConnection]struct{}),
402
- connCh: make(chan *RDConnection, 100),
401
+ conns: make(map[*connection]struct{}),
402
+ connCh: make(chan *connection, 100),
403
closed: false,
404
}
405
@@ -413,7 +413,7 @@ func (g *RDClient) Listen(cred *cryptoops.Credential, name string, alpns []strin
413
414
// Register lease with all available relays
415
for _, relay := range g.relays {
416
- go func(r *rdRelay) {
416
+ go func(r *connRelay) {
417
err := r.client.RegisterLease(cred, listener.lease)
418
if err != nil {
419
log.Error().Err(err).Str("relay", r.addr).Msg("[SDK] Failed to register lease")
@@ -437,7 +437,7 @@ func (g *RDClient) Listen(cred *cryptoops.Credential, name string, alpns []strin
437
return listener, nil
438
}
439
440
-func (g *RDClient) listenerWorker(server *rdRelay) {
440
+func (g *Client) listenerWorker(server *connRelay) {
441
defer g.waitGroup.Done()
442
log.Debug().Str("relay", server.addr).Msg("[SDK] Listener worker started")
443
@@ -446,18 +446,18 @@ func (g *RDClient) listenerWorker(server *rdRelay) {
446
case <-server.stop:
447
log.Debug().Str("relay", server.addr).Msg("[SDK] Listener worker stopped")
448
return
449
- case conn, ok := <-server.client.IncomingConnection():
449
+ case incoming, ok := <-server.client.IncomingConnection():
450
if !ok {
451
log.Debug().Str("relay", server.addr).Msg("[SDK] Incoming connection channel closed")
452
return // Channel closed
453
}
454
455
- lease := conn.LeaseID()
455
+ lease := incoming.LeaseID()
456
log.Debug().
457
Str("relay", server.addr).
458
Str("lease_id", lease).
459
- Str("local", conn.LocalID()).
460
- Str("remote", conn.RemoteID()).
459
+ Str("local", incoming.LocalID()).
460
+ Str("remote", incoming.RemoteID()).
461
Msg("[SDK] Received incoming connection")
462
463
g.mu.Lock()
@@ -466,15 +466,15 @@ func (g *RDClient) listenerWorker(server *rdRelay) {
466
467
if !exists {
468
log.Warn().Str("lease_id", lease).Msg("[SDK] No listener found for lease, closing connection")
469
- conn.SecureConnection.Close() // Close unused connection
469
+ incoming.SecureConnection.Close() // Close unused connection
470
continue
471
}
472
473
- rdConn := &RDConnection{
473
+ conn := &connection{
474
via: server,
475
- conn: conn.SecureConnection,
476
- localAddr: conn.LocalID(),
477
- remoteAddr: conn.RemoteID(),
475
+ conn: incoming.SecureConnection,
476
+ localAddr: incoming.LocalID(),
477
+ remoteAddr: incoming.RemoteID(),
478
}
479
480
listener.mu.Lock()
@@ -482,31 +482,31 @@ func (g *RDClient) listenerWorker(server *rdRelay) {
482
if listener.closed {
483
log.Debug().Str("lease_id", lease).Msg("[SDK] Listener closed, rejecting connection")
484
listener.mu.Unlock()
485
- rdConn.Close()
485
+ conn.Close()
486
continue
487
}
488
- listener.conns[rdConn] = struct{}{}
488
+ listener.conns[conn] = struct{}{}
489
listener.mu.Unlock()
490
491
// Send connection to listener (non-blocking)
492
select {
493
- case listener.connCh <- rdConn:
493
+ case listener.connCh <- conn:
494
log.Debug().Str("lease_id", lease).Msg("[SDK] Connection sent to listener channel")
495
// Connection sent successfully
496
default:
497
// Channel full, close connection
498
log.Warn().Str("lease_id", lease).Msg("[SDK] Listener channel full, closing connection")
499
listener.mu.Lock()
500
- delete(listener.conns, rdConn)
500
+ delete(listener.conns, conn)
501
listener.mu.Unlock()
502
- rdConn.Close()
502
+ conn.Close()
503
}
504
}
505
}
506
}
507
508
-func (g *RDClient) Close() error {
509
- log.Debug().Msg("[SDK] Closing RDClient")
508
+func (g *Client) Close() error {
509
+ log.Debug().Msg("[SDK] Closing Client")
510
var errs []error
511
512
// Signal all goroutines to stop (only once)
@@ -515,11 +515,11 @@ func (g *RDClient) Close() error {
515
})
516
517
g.mu.Lock()
518
- listeners := make([]*RDListener, 0, len(g.listeners))
518
+ listeners := make([]*Listener, 0, len(g.listeners))
519
for _, listener := range g.listeners {
520
listeners = append(listeners, listener)
521
}
522
- relays := make([]*rdRelay, 0, len(g.relays))
522
+ relays := make([]*connRelay, 0, len(g.relays))
523
for _, relay := range g.relays {
524
relays = append(relays, relay)
525
}
@@ -545,7 +545,7 @@ func (g *RDClient) Close() error {
545
log.Debug().Msg("[SDK] Waiting for all workers to finish")
546
g.waitGroup.Wait()
547
548
- log.Debug().Msg("[SDK] RDClient closed successfully")
548
+ log.Debug().Msg("[SDK] Client closed successfully")
549
if len(errs) > 0 {
550
return errs[0]
551
}
@@ -553,7 +553,7 @@ func (g *RDClient) Close() error {
553
}
554
555
// healthCheckWorker periodically checks relay health and reconnects if needed
556
-func (g *RDClient) healthCheckWorker(relay *rdRelay) {
556
+func (g *Client) healthCheckWorker(relay *connRelay) {
557
defer g.waitGroup.Done()
558
559
ticker := time.NewTicker(g.config.HealthCheckInterval)
@@ -605,7 +605,7 @@ func (g *RDClient) healthCheckWorker(relay *rdRelay) {
605
}
606
607
// reconnectRelay attempts to reconnect to a relay server
608
-func (g *RDClient) reconnectRelay(relay *rdRelay) {
608
+func (g *Client) reconnectRelay(relay *connRelay) {
609
addr := relay.addr
610
dialer := relay.dialer
611
@@ -675,8 +675,8 @@ func (g *RDClient) reconnectRelay(relay *rdRelay) {
675
}()
676
}
677
678
-// Implement net.Listener interface for RDListener
679
-func (l *RDListener) Accept() (net.Conn, error) {
678
+// Implement net.Listener interface for Listener
679
+func (l *Listener) Accept() (net.Conn, error) {
680
conn, ok := <-l.connCh
681
if !ok {
682
return nil, net.ErrClosed
@@ -684,7 +684,7 @@ func (l *RDListener) Accept() (net.Conn, error) {
684
return conn, nil
685
}
686
687
-func (l *RDListener) Close() error {
687
+func (l *Listener) Close() error {
688
l.mu.Lock()
689
defer l.mu.Unlock()
690
@@ -706,17 +706,17 @@ func (l *RDListener) Close() error {
706
}
707
708
// Clear the connections map
709
- l.conns = make(map[*RDConnection]struct{})
709
+ l.conns = make(map[*connection]struct{})
710
711
return nil
712
}
713
714
-func (l *RDListener) Addr() net.Addr {
715
- return rdAddr(l.cred.ID())
714
+func (l *Listener) Addr() net.Addr {
715
+ return addr(l.cred.ID())
716
}
717
718
// AddRelay adds a new relay server to the client
719
-func (g *RDClient) AddRelay(addr string, dialer func(context.Context, string) (io.ReadWriteCloser, error)) error {
719
+func (g *Client) AddRelay(addr string, dialer func(context.Context, string) (io.ReadWriteCloser, error)) error {
720
g.mu.Lock()
721
defer g.mu.Unlock()
722
@@ -739,7 +739,7 @@ func (g *RDClient) AddRelay(addr string, dialer func(context.Context, string) (i
739
}
740
741
// Add relay
742
- relay := &rdRelay{
742
+ relay := &connRelay{
743
addr: addr,
744
client: relayClient,
745
dialer: dialer,
@@ -782,7 +782,7 @@ func (g *RDClient) AddRelay(addr string, dialer func(context.Context, string) (i
782
}
783
784
// RemoveRelay removes a relay server from the client
785
-func (g *RDClient) RemoveRelay(addr string) error {
785
+func (g *Client) RemoveRelay(addr string) error {
786
g.mu.Lock()
787
relay, exists := g.relays[addr]
788
if !exists {
@@ -818,7 +818,7 @@ func (g *RDClient) RemoveRelay(addr string) error {
818
}
819
820
// GetRelays returns a list of all relay addresses
821
-func (g *RDClient) GetRelays() []string {
821
+func (g *Client) GetRelays() []string {
822
g.mu.Lock()
823
defer g.mu.Unlock()
824
@@ -830,9 +830,9 @@ func (g *RDClient) GetRelays() []string {
830
return relays
831
}
832
833
-func (g *RDClient) LookupName(name string) (*rdverb.Lease, error) {
833
+func (g *Client) LookupName(name string) (*rdverb.Lease, error) {
834
log.Debug().Str("name", name).Msg("[SDK] Looking up name")
835
- var relays []*rdRelay
835
+ var relays []*connRelay
836
837
g.mu.Lock()
838
for _, server := range g.relays {
sdk/sdk_e2e_test.go
+6
-6
@@ -91,7 +91,7 @@ func TestE2E_ClientToAppThroughRelay(t *testing.T) {
91
92
// 4. Create app SDK client and register listener
93
log.Info().Msg("[TEST] Step 4: Creating app SDK client")
94
- appClient, err := NewClient(func(c *RDClientConfig) {
94
+ appClient, err := NewClient(func(c *ClientConfig) {
95
c.BootstrapServers = []string{"ws://127.0.0.1:14017/relay"}
96
})
97
if err != nil {
@@ -140,7 +140,7 @@ func TestE2E_ClientToAppThroughRelay(t *testing.T) {
140
141
// 8. Create client SDK client
142
log.Info().Msg("[TEST] Step 8: Creating client SDK client")
143
- clientSDK, err := NewClient(func(c *RDClientConfig) {
143
+ clientSDK, err := NewClient(func(c *ClientConfig) {
144
c.BootstrapServers = []string{"ws://127.0.0.1:14017/relay"}
145
})
146
if err != nil {
@@ -268,7 +268,7 @@ func TestE2E_MultipleConnections(t *testing.T) {
268
// Setup app
269
appCred := NewCredential()
270
271
- appClient, err := NewClient(func(c *RDClientConfig) {
271
+ appClient, err := NewClient(func(c *ClientConfig) {
272
c.BootstrapServers = []string{"ws://127.0.0.1:14018/relay"}
273
})
274
if err != nil {
@@ -301,7 +301,7 @@ func TestE2E_MultipleConnections(t *testing.T) {
301
// Create client
302
clientCred := NewCredential()
303
304
- clientSDK, err := NewClient(func(c *RDClientConfig) {
304
+ clientSDK, err := NewClient(func(c *ClientConfig) {
305
c.BootstrapServers = []string{"ws://127.0.0.1:14018/relay"}
306
})
307
if err != nil {
@@ -368,7 +368,7 @@ func TestE2E_ConnectionTimeout(t *testing.T) {
368
369
done := make(chan error, 1)
370
go func() {
371
- _, err := NewClient(func(c *RDClientConfig) {
371
+ _, err := NewClient(func(c *ClientConfig) {
372
c.BootstrapServers = []string{"ws://127.0.0.1:19999/relay"} // Non-existent
373
})
374
done <- err
@@ -420,7 +420,7 @@ func TestE2E_ConnectionTimeout(t *testing.T) {
420
421
time.Sleep(500 * time.Millisecond)
422
423
- clientSDK, err := NewClient(func(c *RDClientConfig) {
423
+ clientSDK, err := NewClient(func(c *ClientConfig) {
424
c.BootstrapServers = []string{"ws://127.0.0.1:14019/relay"}
425
})
426
if err != nil {