github.com/dubbogo/gost@v1.14.0/time/sleep.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  // Package gxtime encapsulates some golang.time functions
    19  package gxtime
    20  
    21  import (
    22  	"time"
    23  )
    24  
    25  // Timer is a wrapper of TimeWheel to supply go timer funcs
    26  type Timer struct {
    27  	C  <-chan time.Time
    28  	ID TimerID
    29  	w  *TimerWheel
    30  }
    31  
    32  // After waits for the duration to elapse and then sends the current time
    33  // on the returned channel.
    34  func After(d time.Duration) <-chan time.Time {
    35  	if d <= 0 {
    36  		return nil
    37  	}
    38  
    39  	return defaultTimerWheel.After(d)
    40  }
    41  
    42  // Sleep pauses the current goroutine for at least the duration d.
    43  // A negative or zero duration causes Sleep to return immediately.
    44  func Sleep(d time.Duration) {
    45  	if d <= 0 {
    46  		return
    47  	}
    48  
    49  	defaultTimerWheel.Sleep(d)
    50  }
    51  
    52  // AfterFunc waits for the duration to elapse and then calls f
    53  // in its own goroutine. It returns a Timer that can
    54  // be used to cancel the call using its Stop method.
    55  func AfterFunc(d time.Duration, f func()) *Timer {
    56  	if d <= 0 {
    57  		return nil
    58  	}
    59  
    60  	return defaultTimerWheel.AfterFunc(d, f)
    61  }
    62  
    63  // NewTimer creates a new Timer that will send
    64  // the current time on its channel after at least duration d.
    65  func NewTimer(d time.Duration) *Timer {
    66  	if d <= 0 {
    67  		return nil
    68  	}
    69  
    70  	return defaultTimerWheel.NewTimer(d)
    71  }
    72  
    73  // Reset changes the timer to expire after duration d.
    74  // It returns true if the timer had been active, false if the timer had
    75  // expired or been stopped.
    76  func (t *Timer) Reset(d time.Duration) {
    77  	if d <= 0 {
    78  		return
    79  	}
    80  	if t.w == nil {
    81  		panic("time: Stop called on uninitialized Timer")
    82  	}
    83  
    84  	_ = t.w.resetTimer(t, d)
    85  }
    86  
    87  // Stop prevents the Timer from firing.
    88  func (t *Timer) Stop() {
    89  	if t.w == nil {
    90  		panic("time: Stop called on uninitialized Timer")
    91  	}
    92  
    93  	_ = t.w.deleteTimer(t)
    94  	t.w = nil
    95  }