{"modulePath":"github.com/Evolution404/simcore","version":"v1.0.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package mclock\n\nPackage mclock is a wrapper for a monotonic clock source\n\n## Types\n\n```go\ntype AbsTime int64\n```\nAbsTime represents absolute monotonic time.\n\n```go\nfunc Now() AbsTime\n```\nNow returns the current absolute monotonic time. 获取当前系统启动到现在的绝对时间\n\n```go\nfunc (t AbsTime) Add(d time.Duration) AbsTime\n```\n对AbsTime实现加减操作 Add returns t + d as absolute time.\n\n```go\nfunc (t AbsTime) Sub(t2 AbsTime) time.Duration\n```\nSub returns t - t2 as a duration. 减法返回time.Duration对象\n\n```go\ntype ChanTimer interface {\n\tTimer\n\n\t// The channel returned by C receives a value when the timer expires.\n\t// 返回一个只读管道,当ChanTimer时间到了就会返回向管道输入一个值\n\tC() \u003c-chan AbsTime\n\t// Reset reschedules the timer with a new timeout.\n\t// It should be invoked only on stopped or expired timers with drained channels.\n\tReset(time.Duration)\n}\n```\nChanTimer is a cancellable event created by NewTimer. systemTimer和simTimer实现了这个接口\n\n```go\ntype Clock interface {\n\tNow() AbsTime\n\tSleep(time.Duration)\n\tNewTimer(time.Duration) ChanTimer\n\t// After返回一个管道,当输入时间到了管道内输出结束时间\n\tAfter(time.Duration) \u003c-chan AbsTime\n\tAfterFunc(d time.Duration, f func()) Timer\n}\n```\nThe Clock interface makes it possible to replace the monotonic system clock with a simulated clock. System对象和Simulated对象实现了这个接口 System使用的系统内置接口 Simulated应该是重写了一个\n\n```go\ntype Simulated struct {\n\t// contains filtered or unexported fields\n}\n```\nSimulated implements a virtual Clock for reproducible time-sensitive tests. It simulates a scheduler on a virtual timescale where actual processing takes zero time.\n\nThe virtual clock doesn't advance on its own, call Run to advance it and execute timers. Since there is no way to influence the Go scheduler, testing timeout behaviour involving goroutines needs special care. A good way to test such timeouts is as follows: First perform the action that is supposed to time out. Ensure that the timer you want to test is created. Then run the clock until after the timeout. Finally observe the effect of the timeout using a channel or semaphore.\n\n```go\nfunc (s *Simulated) ActiveTimers() int\n```\nActiveTimers returns the number of timers that haven't fired. 返回当前scheduled的长度\n\n```go\nfunc (s *Simulated) After(d time.Duration) \u003c-chan AbsTime\n```\nAfter returns a channel which receives the current time after the clock has advanced by d.\n\n```go\nfunc (s *Simulated) AfterFunc(d time.Duration, fn func()) Timer\n```\nAfterFunc runs fn after the clock has advanced by d. Unlike with the system clock, fn runs on the goroutine that calls Run.\n\n```go\nfunc (s *Simulated) NewTimer(d time.Duration) ChanTimer\n```\nNewTimer creates a timer which fires when the clock has advanced by d.\n\n```go\nfunc (s *Simulated) Now() AbsTime\n```\nNow returns the current virtual time.\n\n```go\nfunc (s *Simulated) Run(d time.Duration)\n```\nRun moves the clock by the given duration, executing all timers before that duration. 调用s.scheduled里面所有到期的函数\n\n```go\nfunc (s *Simulated) Sleep(d time.Duration)\n```\nSleep blocks until the clock has advanced by d.\n\n```go\nfunc (s *Simulated) WaitForTimers(n int)\n```\nWaitForTimers waits until the clock has at least n scheduled timers.\n\n```go\ntype System struct{}\n```\nSystem implements Clock using the system clock. 使用系统内置方法实现了Clock接口的5个函数\n\n```go\nfunc (c System) After(d time.Duration) \u003c-chan AbsTime\n```\nAfter returns a channel which receives the current time after d has elapsed. 等待指定时间管道可以读出时间\n\n```go\nfunc (c System) AfterFunc(d time.Duration, f func()) Timer\n```\nAfterFunc runs f on a new goroutine after the duration has elapsed. 等待指定时间执行函数\n\n```go\nfunc (c System) NewTimer(d time.Duration) ChanTimer\n```\nNewTimer creates a timer which can be rescheduled. 返回一个ChanTimer,等待指定的d时间后timer.C返回的管道会输出结束时的时间\n\n```go\nfunc (c System) Now() AbsTime\n```\nNow returns the current monotonic time.\n\n```go\nfunc (c System) Sleep(d time.Duration)\n```\nSleep blocks for the given duration.\n\n```go\ntype Timer interface {\n\t// Stop cancels the timer. It returns false if the timer has already\n\t// expired or been stopped.\n\tStop() bool\n}\n```\nTimer is a cancellable event created by AfterFunc. Clock.AfterFunc返回的对象\n\n","path":"github.com/Evolution404/simcore/common/mclock","name":"mclock","synopsis":"Package mclock is a wrapper for a monotonic clock source","isRedistributable":true}
