forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-sql-driver/mysql/conncheck.go

88 lines
1.2 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright 2019 The Go-MySQL-Driver Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This Source Code Form is subject to the terms of the Mozilla Public
2024-02-18 10:42:21 +00:00
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
2024-02-18 10:42:21 +00:00
// You can obtain one at http://mozilla.org/MPL/2.0/.
2024-06-14 08:41:36 +00:00
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd || solaris || illumos
2024-02-18 10:42:21 +00:00
// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos
package mysql
import (
"errors"
"io"
"net"
"syscall"
)
var errUnexpectedRead = errors.New("unexpected read from socket")
func connCheck(conn net.Conn) error {
2024-02-18 10:42:21 +00:00
var sysErr error
sysConn, ok := conn.(syscall.Conn)
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
rawConn, err := sysConn.SyscallConn()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
err = rawConn.Read(func(fd uintptr) bool {
2024-02-18 10:42:21 +00:00
var buf [1]byte
2024-02-18 10:42:21 +00:00
n, err := syscall.Read(int(fd), buf[:])
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case n == 0 && err == nil:
2024-02-18 10:42:21 +00:00
sysErr = io.EOF
2024-02-18 10:42:21 +00:00
case n > 0:
2024-02-18 10:42:21 +00:00
sysErr = errUnexpectedRead
2024-02-18 10:42:21 +00:00
case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK:
2024-02-18 10:42:21 +00:00
sysErr = nil
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
sysErr = err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
return sysErr
2024-02-18 10:42:21 +00:00
}