Source File
deadline.go
Belonging Package
github.com/pion/transport/v3/deadline
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>// SPDX-License-Identifier: MIT// Package deadline provides deadline timer used to implement// net.Conn compatible connectionpackage deadlineimport ()type deadlineState uint8const (deadlineStopped deadlineState = iotadeadlineStarteddeadlineExceeded)var _ context.Context = (*Deadline)(nil)// Deadline signals updatable deadline timer.// Also, it implements context.Context.type Deadline struct {mu sync.RWMutextimer timerdone chan struct{}deadline time.Timestate deadlineStatepending uint8}// New creates new deadline timer.func () *Deadline {return &Deadline{done: make(chan struct{}),}}func ( *Deadline) () {.mu.Lock()if .pending--; .pending != 0 || .state != deadlineStarted {.mu.Unlock()return}.state = deadlineExceeded:= .done.mu.Unlock()close()}// Set new deadline. Zero value means no deadline.func ( *Deadline) ( time.Time) {.mu.Lock()defer .mu.Unlock()if .state == deadlineStarted && .timer.Stop() {.pending--}.deadline =.pending++if .state == deadlineExceeded {.done = make(chan struct{})}if .IsZero() {.pending--.state = deadlineStoppedreturn}if := time.Until(); > 0 {.state = deadlineStartedif .timer == nil {.timer = afterFunc(, .timeout)} else {.timer.Reset()}return}.pending--.state = deadlineExceededclose(.done)}// Done receives deadline signal.func ( *Deadline) () <-chan struct{} {.mu.RLock()defer .mu.RUnlock()return .done}// Err returns context.DeadlineExceeded if the deadline is exceeded.// Otherwise, it returns nil.func ( *Deadline) () error {.mu.RLock()defer .mu.RUnlock()if .state == deadlineExceeded {return context.DeadlineExceeded}return nil}// Deadline returns current deadline.func ( *Deadline) () (time.Time, bool) {.mu.RLock()defer .mu.RUnlock()if .deadline.IsZero() {return .deadline, false}return .deadline, true}// Value returns nil.func ( *Deadline) (interface{}) interface{} {return nil}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |