-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefix_dialer.go
45 lines (36 loc) · 1010 Bytes
/
prefix_dialer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package risefront
import (
"errors"
"net"
"os"
"strings"
"syscall"
)
// PrefixDialer uses github.com/Microsoft/go-winio.{DialPipe,ListenPipe} on windows and net.{Dial,Listen} on other platforms.
type PrefixDialer struct {
Prefix string
}
var _ Dialer = PrefixDialer{}
func (pd PrefixDialer) Listen(name string) (net.Listener, error) {
return listen(pd.Prefix + name)
}
func (pd PrefixDialer) Dial(name string) (net.Conn, error) {
var builder strings.Builder
builder.WriteString(pd.Prefix)
builder.WriteString(name)
c, err := dial(builder.String())
// attempt to remove file if nobofy is listening
if err != nil && errorIsNobodyListening(err) {
_ = os.Remove(builder.String())
// re-dial to have a nice fs.ErrNotExist error
c, err = dial(builder.String())
}
return c, err
}
func errorIsNobodyListening(err error) bool {
sysErr := &os.SyscallError{}
if errors.As(err, &sysErr) {
return sysErr.Syscall == "connect" && errors.Is(sysErr.Err, syscall.Errno(111))
}
return false
}