github.com/SandwichDev/go-internals@v0.0.0-20210605002614-12311ac6b2c5/poll/fd.go (about)

     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 returned when a network descriptor is used after
    17  // it has been closed. Keep this string consistent because of issue
    18  // #4373: since historically programs have not been able to detect
    19  // this error, they look for the string.
    20  var ErrNetClosing = errors.New("use of closed network connection")
    21  
    22  // ErrFileClosing is returned when a file descriptor is used after it
    23  // has been closed.
    24  var ErrFileClosing = errors.New("use of closed file")
    25  
    26  // ErrNoDeadline is returned when a request is made to set a deadline
    27  // on a file type that does not use the poller.
    28  var ErrNoDeadline = errors.New("file type does not support deadline")
    29  
    30  // Return the appropriate closing error based on isFile.
    31  func errClosing(isFile bool) error {
    32  	if isFile {
    33  		return ErrFileClosing
    34  	}
    35  	return ErrNetClosing
    36  }
    37  
    38  // ErrDeadlineExceeded is returned for an expired deadline.
    39  // This is exported by the os package as os.ErrDeadlineExceeded.
    40  var ErrDeadlineExceeded error = &DeadlineExceededError{}
    41  
    42  // DeadlineExceededError is returned for an expired deadline.
    43  type DeadlineExceededError struct{}
    44  
    45  // Implement the net.Error interface.
    46  // The string is "i/o timeout" because that is what was returned
    47  // by earlier Go versions. Changing it may break programs that
    48  // match on error strings.
    49  func (e *DeadlineExceededError) Error() string   { return "i/o timeout" }
    50  func (e *DeadlineExceededError) Timeout() bool   { return true }
    51  func (e *DeadlineExceededError) Temporary() bool { return true }
    52  
    53  // ErrNotPollable is returned when the file or socket is not suitable
    54  // for event notification.
    55  var ErrNotPollable = errors.New("not pollable")
    56  
    57  // consume removes data from a slice of byte slices, for writev.
    58  func consume(v *[][]byte, n int64) {
    59  	for len(*v) > 0 {
    60  		ln0 := int64(len((*v)[0]))
    61  		if ln0 > n {
    62  			(*v)[0] = (*v)[0][n:]
    63  			return
    64  		}
    65  		n -= ln0
    66  		*v = (*v)[1:]
    67  	}
    68  }
    69  
    70  // TestHookDidWritev is a hook for testing writev.
    71  var TestHookDidWritev = func(wrote int) {}