github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/common/mclock/mclock.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:34</date>
    10  //</624450074025398272>
    11  
    12  
    13  //包mclock是单调时钟源的包装器
    14  package mclock
    15  
    16  import (
    17  	"time"
    18  
    19  	"github.com/aristanetworks/goarista/monotime"
    20  )
    21  
    22  //Abstime代表绝对单调时间。
    23  type AbsTime time.Duration
    24  
    25  //现在返回当前绝对单调时间。
    26  func Now() AbsTime {
    27  	return AbsTime(monotime.Now())
    28  }
    29  
    30  //添加返回t+d。
    31  func (t AbsTime) Add(d time.Duration) AbsTime {
    32  	return t + AbsTime(d)
    33  }
    34  
    35  //时钟接口使得用
    36  //模拟时钟。
    37  type Clock interface {
    38  	Now() AbsTime
    39  	Sleep(time.Duration)
    40  	After(time.Duration) <-chan time.Time
    41  }
    42  
    43  //系统使用系统时钟实现时钟。
    44  type System struct{}
    45  
    46  //现在实现时钟。
    47  func (System) Now() AbsTime {
    48  	return AbsTime(monotime.Now())
    49  }
    50  
    51  //睡眠实现时钟。
    52  func (System) Sleep(d time.Duration) {
    53  	time.Sleep(d)
    54  }
    55  
    56  //在执行时钟之后。
    57  func (System) After(d time.Duration) <-chan time.Time {
    58  	return time.After(d)
    59  }
    60