WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c4439ac

Browse files
committed
sockets: InmemSocket: use dummyAddr as field-type
- Move the dummyAddr type definition and methods together. - Change the InmemSocket.address field to a dummyAddr; the field is un-exported and could only be set through the constructor, so we can convert it when doing so. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent ccad206 commit c4439ac

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

sockets/inmem_socket.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,42 @@ import (
55
"sync"
66
)
77

8-
// InmemSocket implements net.Listener using in-memory only connections.
8+
// dummyAddr is used to satisfy net.Addr for the in-mem socket
9+
// it is just stored as a string and returns the string for all calls
10+
type dummyAddr string
11+
12+
// Network returns the addr string, satisfies net.Addr
13+
func (a dummyAddr) Network() string {
14+
return string(a)
15+
}
16+
17+
// String returns the string form
18+
func (a dummyAddr) String() string {
19+
return string(a)
20+
}
21+
22+
// InmemSocket implements [net.Listener] using in-memory only connections.
923
type InmemSocket struct {
1024
chConn chan net.Conn
1125
chClose chan struct{}
12-
addr string
26+
addr dummyAddr
1327
mu sync.Mutex
1428
}
1529

16-
// dummyAddr is used to satisfy net.Addr for the in-mem socket
17-
// it is just stored as a string and returns the string for all calls
18-
type dummyAddr string
19-
20-
// NewInmemSocket creates an in-memory only net.Listener
21-
// The addr argument can be any string, but is used to satisfy the `Addr()` part
22-
// of the net.Listener interface
30+
// NewInmemSocket creates an in-memory only [net.Listener]. The addr argument
31+
// can be any string, but is used to satisfy the [net.Listener.Addr] part
32+
// of the [net.Listener] interface
2333
func NewInmemSocket(addr string, bufSize int) *InmemSocket {
2434
return &InmemSocket{
2535
chConn: make(chan net.Conn, bufSize),
2636
chClose: make(chan struct{}),
27-
addr: addr,
37+
addr: dummyAddr(addr),
2838
}
2939
}
3040

3141
// Addr returns the socket's addr string to satisfy net.Listener
3242
func (s *InmemSocket) Addr() net.Addr {
33-
return dummyAddr(s.addr)
43+
return s.addr
3444
}
3545

3646
// Accept implements the Accept method in the Listener interface; it waits
@@ -69,13 +79,3 @@ func (s *InmemSocket) Dial(network, addr string) (net.Conn, error) {
6979

7080
return clientConn, nil
7181
}
72-
73-
// Network returns the addr string, satisfies net.Addr
74-
func (a dummyAddr) Network() string {
75-
return string(a)
76-
}
77-
78-
// String returns the string form
79-
func (a dummyAddr) String() string {
80-
return string(a)
81-
}

0 commit comments

Comments
 (0)