| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package poll supports non-blocking I/O on file descriptors with polling. |
| 6 |
// This supports I/O operations that block only a goroutine, not a thread. |
| 7 |
// This is used by the net and os packages. |
| 8 |
// It uses a poller built into the runtime, with support from the |
| 9 |
// runtime scheduler. |
| 10 |
package poll |
| 11 |
|
| 12 |
import ( |
| 13 |
"errors" |
| 14 |
) |
| 15 |
|
| 16 |
// errNetClosing is the type of the variable ErrNetClosing. |
| 17 |
// This is used to implement the net.Error interface. |
| 18 |
type errNetClosing struct{} |
| 19 |
|
| 20 |
// Error returns the error message for ErrNetClosing. |
| 21 |
// Keep this string consistent because of issue #4373: |
| 22 |
// since historically programs have not been able to detect |
| 23 |
// this error, they look for the string. |
| 24 |
func (e errNetClosing) Error() string { return "use of closed network connection" } |
| 25 |
|
| 26 |
func (e errNetClosing) Timeout() bool { return false } |
| 27 |
func (e errNetClosing) Temporary() bool { return false } |
| 28 |
|
| 29 |
// ErrNetClosing is returned when a network descriptor is used after |
| 30 |
// it has been closed. |
| 31 |
var ErrNetClosing = errNetClosing{} |
| 32 |
|
| 33 |
// ErrFileClosing is returned when a file descriptor is used after it |
| 34 |
// has been closed. |
| 35 |
var ErrFileClosing = errors.New("use of closed file") |
| 36 |
|
| 37 |
// ErrNoDeadline is returned when a request is made to set a deadline |
| 38 |
// on a file type that does not use the poller. |
| 39 |
var ErrNoDeadline = errors.New("file type does not support deadline") |
| 40 |
|
| 41 |
// Return the appropriate closing error based on isFile. |
| 42 |
func errClosing(isFile bool) error { |
| 43 |
if isFile { |
| 44 |
return ErrFileClosing |
| 45 |
} |
| 46 |
return ErrNetClosing |
| 47 |
} |
| 48 |
|
| 49 |
// ErrDeadlineExceeded is returned for an expired deadline. |
| 50 |
// This is exported by the os package as os.ErrDeadlineExceeded. |
| 51 |
var ErrDeadlineExceeded error = &DeadlineExceededError{} |
| 52 |
|
| 53 |
// DeadlineExceededError is returned for an expired deadline. |
| 54 |
type DeadlineExceededError struct{} |
| 55 |
|
| 56 |
// Implement the net.Error interface. |
| 57 |
// The string is "i/o timeout" because that is what was returned |
| 58 |
// by earlier Go versions. Changing it may break programs that |
| 59 |
// match on error strings. |
| 60 |
func (e *DeadlineExceededError) Error() string { return "i/o timeout" } |
| 61 |
func (e *DeadlineExceededError) Timeout() bool { return true } |
| 62 |
func (e *DeadlineExceededError) Temporary() bool { return true } |
| 63 |
|
| 64 |
// ErrNotPollable is returned when the file or socket is not suitable |
| 65 |
// for event notification. |
| 66 |
var ErrNotPollable = errors.New("not pollable") |
| 67 |
|
| 68 |
// consume removes data from a slice of byte slices, for writev. |
| 69 |
func consume(v *[][]byte, n int64) { |
| 70 |
for len(*v) > 0 { |
| 71 |
ln0 := int64(len((*v)[0])) |
| 72 |
if ln0 > n { |
| 73 |
(*v)[0] = (*v)[0][n:] |
| 74 |
return |
| 75 |
} |
| 76 |
n -= ln0 |
| 77 |
(*v)[0] = nil |
| 78 |
*v = (*v)[1:] |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// TestHookDidWritev is a hook for testing writev. |
| 83 |
var TestHookDidWritev = func(wrote int) {} |
| 84 |
|
| 85 |
// String is an internal string definition for methods/functions |
| 86 |
// that is not intended for use outside the standard libraries. |
| 87 |
// |
| 88 |
// Other packages in std that import internal/poll and have some |
| 89 |
// exported APIs (now we've got some in net.rawConn) which are only used |
| 90 |
// internally and are not intended to be used outside the standard libraries, |
| 91 |
// Therefore, we make those APIs use internal types like poll.FD or poll.String |
| 92 |
// in their function signatures to disable the usability of these APIs from |
| 93 |
// external codebase. |
| 94 |
type String string |