本文整理汇总了Golang中github.com/benbjohnson/clock.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: setDefaults
// setDefaults sets default values for any Worker fields that are
// uninitialized.
func (w *Worker) setDefaults() {
if w.WorkerID == "" {
// May as well use a UUID here, "it's what we've always done"
w.WorkerID = uuid.NewV4().String()
}
if w.Concurrency == 0 {
w.Concurrency = runtime.NumCPU()
}
if w.PollInterval == time.Duration(0) {
w.PollInterval = time.Duration(1) * time.Second
}
if w.HeartbeatInterval == time.Duration(0) {
w.HeartbeatInterval = time.Duration(15) * time.Second
}
if w.MaxAttempts == 0 {
w.MaxAttempts = 100
}
if w.Clock == nil {
w.Clock = clock.New()
}
}
示例2: TestClock_Now
// Ensure that the clock's time matches the standary library.
func TestClock_Now(t *testing.T) {
a := time.Now().Round(time.Second)
b := clock.New().Now().Round(time.Second)
if !a.Equal(b) {
t.Errorf("not equal: %s != %s", a, b)
}
}
示例3: newRateLimiter
func newRateLimiter(perSec int, maxBurst time.Duration) *rateLimiter {
maxPerBatch := int64(perSec / int(time.Second/maxBurst))
return &rateLimiter{
limitPerSec: perSec,
resolution: maxBurst,
time: clock.New(),
maxPerBatch: maxPerBatch,
}
}
示例4: NewLWWSetWithBias
func NewLWWSetWithBias(bias BiasType) (*LWWSet, error) {
if bias != BiasAdd && bias != BiasRemove {
return nil, ErrNoSuchBias
}
return &LWWSet{
addMap: make(map[interface{}]time.Time),
rmMap: make(map[interface{}]time.Time),
bias: bias,
clock: clock.New(),
}, nil
}
示例5: NewEngine
func NewEngine() *Engine {
e := &Engine{
ticker: NewTicker(time.Now(), time.Second*0, clock.New()),
execQueue: make(chan *Job, 1000),
scheduler: NewScheduler(),
evalHandler: NewEvalHandler(),
ruleReader: NewRuleReader(),
log: log.New("alerting.engine"),
resultHandler: NewResultHandler(),
}
return e
}
示例6: NewZoneDb
// Create a new zone database
func NewZoneDb(config ZoneConfig) (zone *ZoneDb, err error) {
zone = &ZoneDb{
domain: config.Domain,
idents: make(identRecordSet),
mdnsCli: config.MDNSClient,
mdnsSrv: config.MDNSServer,
iface: config.Iface,
clock: config.Clock,
relevantLimit: time.Duration(DefaultRelevantTime) * time.Second,
refreshCloseChan: make(chan bool),
}
// fix the default configuration parameters
if zone.clock == nil {
zone.clock = clock.New()
}
if len(zone.domain) == 0 {
zone.domain = DefaultLocalDomain
}
if config.RefreshInterval > 0 {
zone.refreshInterval = time.Duration(config.RefreshInterval) * time.Second
}
if config.RelevantTime > 0 {
zone.relevantLimit = time.Duration(config.RelevantTime) * time.Second
}
if zone.refreshInterval > 0 {
zone.refreshScheds = NewSchedQueue(zone.clock)
}
// Create the mDNS client and server
if zone.mdnsCli == nil {
if zone.mdnsCli, err = NewMDNSClient(); err != nil {
return
}
}
if zone.mdnsSrv == nil {
mdnsTTL := DefaultLocalTTL
if config.LocalTTL > 0 {
mdnsTTL = config.LocalTTL
}
if zone.mdnsSrv, err = NewMDNSServer(zone, false, mdnsTTL); err != nil {
return
}
}
return
}
示例7: Spawn
// Spawn initializes the limiter
func (l *Limiter) Spawn(id int) utils.Composer {
l.keepSending = make(chan struct{}, l.Config.Burst)
if l.clk == nil {
l.clk = clock.New()
}
go func() {
for {
<-l.clk.Timer(1 * time.Second).C
l.keepSending <- struct{}{}
}
}()
return l
}
示例8: TestClock_Timer_Stop
// Ensure that the clock's timer can be stopped.
func TestClock_Timer_Stop(t *testing.T) {
var ok bool
go func() {
time.Sleep(10 * time.Millisecond)
ok = true
}()
timer := clock.New().Timer(20 * time.Millisecond)
timer.Stop()
select {
case <-timer.C:
t.Fatal("unexpected send")
case <-time.After(30 * time.Millisecond):
}
}
示例9: TestClock_Sleep
// Ensure that the clock sleeps for the appropriate amount of time.
func TestClock_Sleep(t *testing.T) {
var ok bool
go func() {
time.Sleep(10 * time.Millisecond)
ok = true
}()
go func() {
time.Sleep(30 * time.Millisecond)
t.Fatal("too late")
}()
gosched()
clock.New().Sleep(20 * time.Millisecond)
if !ok {
t.Fatal("too early")
}
}
示例10: NewCache
// NewCache creates a cache of the given capacity
func NewCache(capacity int, clk clock.Clock) (*Cache, error) {
if capacity <= 0 {
return nil, errInvalidCapacity
}
c := &Cache{
capacity: capacity,
entries: make(entries, capacity),
clock: clk,
}
if c.clock == nil {
c.clock = clock.New()
}
heap.Init(&c.entriesH)
return c, nil
}
示例11: TestClock_Ticker
// Ensure that the clock's ticker ticks correctly.
func TestClock_Ticker(t *testing.T) {
var ok bool
go func() {
time.Sleep(100 * time.Millisecond)
ok = true
}()
go func() {
time.Sleep(200 * time.Millisecond)
t.Fatal("too late")
}()
gosched()
ticker := clock.New().Ticker(50 * time.Millisecond)
<-ticker.C
<-ticker.C
if !ok {
t.Fatal("too early")
}
}
示例12: defaultOptions
func defaultOptions() *Options {
opts := &Options{
SuspicionTimeout: 5000 * time.Millisecond,
MinProtocolPeriod: 200 * time.Millisecond,
JoinTimeout: 1000 * time.Millisecond,
PingTimeout: 1500 * time.Millisecond,
PingRequestTimeout: 5000 * time.Millisecond,
PingRequestSize: 3,
RollupFlushInterval: 5000 * time.Millisecond,
RollupMaxUpdates: 250,
Clock: clock.New(),
}
return opts
}
示例13: TestClock_Tick
// Ensure that the clock ticks correctly.
func TestClock_Tick(t *testing.T) {
var ok bool
go func() {
time.Sleep(10 * time.Millisecond)
ok = true
}()
go func() {
time.Sleep(50 * time.Millisecond)
t.Fatal("too late")
}()
gosched()
c := clock.New().Tick(20 * time.Millisecond)
<-c
<-c
if !ok {
t.Fatal("too early")
}
}
示例14: Run
// start statsdaemon instance with standard network daemon behaviors
func (s *StatsDaemon) Run(listen_addr, admin_addr, graphite_addr string) {
s.listen_addr = listen_addr
s.admin_addr = admin_addr
s.graphite_addr = graphite_addr
s.submitFunc = s.GraphiteQueue
s.Clock = clock.New()
s.graphiteQueue = make(chan []byte, 1000)
log.Printf("statsdaemon instance '%s' starting\n", s.instance)
output := &common.Output{
Metrics: s.Metrics,
MetricAmounts: s.metricAmounts,
Valid_lines: s.valid_lines,
Invalid_lines: s.Invalid_lines,
}
go udp.StatsListener(s.listen_addr, s.prefix, output) // set up udp listener that writes messages to output's channels (i.e. s's channels)
go s.adminListener() // tcp admin_addr to handle requests
go s.metricStatsMonitor() // handles requests fired by telnet api
go s.graphiteWriter() // writes to graphite in the background
s.metricsMonitor() // takes data from s.Metrics and puts them in the guage/timers/etc objects. pointers guarded by select. also listens for signals.
}
示例15: TestClock_AfterFunc
// Ensure that the clock's AfterFunc executes at the correct time.
func TestClock_AfterFunc(t *testing.T) {
var ok bool
go func() {
time.Sleep(10 * time.Millisecond)
ok = true
}()
go func() {
time.Sleep(30 * time.Millisecond)
t.Fatal("too late")
}()
gosched()
var wg sync.WaitGroup
wg.Add(1)
clock.New().AfterFunc(20*time.Millisecond, func() {
wg.Done()
})
wg.Wait()
if !ok {
t.Fatal("too early")
}
}