-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathconncheck.go
50 lines (44 loc) · 1.12 KB
/
conncheck.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
46
47
48
49
50
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
//
// Copyright 2019 The Go-MySQL-Driver Authors. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || solaris || illumos
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos
package mysql
import (
"fmt"
"net"
"syscall"
"golang.org/x/sys/unix"
)
func connCheck(conn net.Conn) error {
sysConn, ok := conn.(syscall.Conn)
if !ok {
return nil
}
rawConn, err := sysConn.SyscallConn()
if err != nil {
return err
}
var pollErr error
err = rawConn.Control(func(fd uintptr) {
fds := []unix.PollFd{
{Fd: int32(fd), Events: unix.POLLIN | unix.POLLERR},
}
n, err := unix.Poll(fds, 0)
if err != nil {
pollErr = fmt.Errorf("poll: %w", err)
}
if n > 0 {
// fmt.Errorf("poll: %v", fds[0].Revents)
pollErr = errUnexpectedEvent
}
})
if err != nil {
return err
}
return pollErr
}