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 210a62a

Browse files
Merge pull request #142 from thaJeztah/sockets_cleanups
sockets: minor cleanups
2 parents 34239ab + c4439ac commit 210a62a

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
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-
}

sockets/sockets.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func ConfigureTransport(tr *http.Transport, proto, addr string) error {
3737
}
3838
switch proto {
3939
case "unix":
40-
return configureUnixTransport(tr, proto, addr)
40+
return configureUnixTransport(tr, addr)
4141
case "npipe":
42-
return configureNpipeTransport(tr, proto, addr)
42+
return configureNpipeTransport(tr, addr)
4343
default:
4444
tr.Proxy = http.ProxyFromEnvironment
4545
tr.DisableCompression = false
@@ -50,7 +50,7 @@ func ConfigureTransport(tr *http.Transport, proto, addr string) error {
5050
return nil
5151
}
5252

53-
func configureUnixTransport(tr *http.Transport, proto, addr string) error {
53+
func configureUnixTransport(tr *http.Transport, addr string) error {
5454
if len(addr) > maxUnixSocketPathSize {
5555
return fmt.Errorf("unix socket path %q is too long", addr)
5656
}
@@ -60,7 +60,7 @@ func configureUnixTransport(tr *http.Transport, proto, addr string) error {
6060
Timeout: defaultTimeout,
6161
}
6262
tr.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {
63-
return dialer.DialContext(ctx, proto, addr)
63+
return dialer.DialContext(ctx, "unix", addr)
6464
}
6565
return nil
6666
}

sockets/sockets_unix.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
package sockets
44

5-
import "net/http"
6-
7-
func configureNpipeTransport(tr *http.Transport, proto, addr string) error {
5+
func configureNpipeTransport(any, string) error {
86
return ErrProtocolNotAvailable
97
}

sockets/sockets_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/Microsoft/go-winio"
99
)
1010

11-
func configureNpipeTransport(tr *http.Transport, proto, addr string) error {
11+
func configureNpipeTransport(tr *http.Transport, addr string) error {
1212
// No need for compression in local communications.
1313
tr.DisableCompression = true
1414
tr.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) {

0 commit comments

Comments
 (0)