• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang clock.Now函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/luci/luci-go/common/clock.Now函数的典型用法代码示例。如果您正苦于以下问题:Golang Now函数的具体用法?Golang Now怎么用?Golang Now使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Now函数的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: send

func (s *endpointServiceImpl) send(ctx context.Context, data []byte) error {
	ctx = log.SetField(ctx, "endpointURL", s.url)
	return retryCall(ctx, "endpoint.send", func() error {
		startTime := clock.Now(ctx)

		log.Debugf(ctx, "Pushing message to endpoint.")
		req, err := http.NewRequest("POST", s.url, bytes.NewReader(data))
		if err != nil {
			log.Errorf(log.SetError(ctx, err), "Failed to create HTTP request.")
			return err
		}
		req.Header.Add("content-type", protobufContentType)
		req.Header.Add("user-agent", monitoringEndpointUserAgent)

		resp, err := s.client.Do(req)
		if err != nil {
			// Treat a client error as transient.
			log.Warningf(log.SetError(ctx, err), "Failed proxy client request.")
			return errors.WrapTransient(err)
		}
		defer resp.Body.Close()

		// Read the full response body. This will enable us to re-use the
		// connection.
		bodyData, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Errorf(log.SetError(ctx, err), "Error during endpoint connection.")
			return errors.WrapTransient(err)
		}

		log.Fields{
			"status":        resp.Status,
			"statusCode":    resp.StatusCode,
			"headers":       resp.Header,
			"contentLength": resp.ContentLength,
			"body":          string(bodyData),
			"duration":      clock.Now(ctx).Sub(startTime),
		}.Debugf(ctx, "Received HTTP response from endpoint.")

		if http.StatusOK <= resp.StatusCode && resp.StatusCode < http.StatusMultipleChoices {
			log.Debugf(ctx, "Message pushed successfully.")
			return nil
		}

		err = fmt.Errorf("http: server error (%d)", resp.StatusCode)
		if resp.StatusCode >= http.StatusInternalServerError {
			err = errors.WrapTransient(err)
		}

		log.Fields{
			log.ErrorKey: err,
			"status":     resp.Status,
			"statusCode": resp.StatusCode,
		}.Warningf(ctx, "Proxy error.")
		return err
	})
}
开发者ID:nicko96,项目名称:Chrome-Infra,代码行数:57,代码来源:endpoint.go


示例2: Next

// Next implements the Iterator interface.
func (i *Limited) Next(ctx context.Context, _ error) time.Duration {
	if i.Retries == 0 {
		return Stop
	}
	i.Retries--

	// If there is a maximum total time, enforce it.
	if i.MaxTotal > 0 {
		now := clock.Now(ctx)
		if i.startTime.IsZero() {
			i.startTime = now
		}

		var elapsed time.Duration
		if now.After(i.startTime) {
			elapsed = now.Sub(i.startTime)
		}

		// Remaining time is the difference between total allowed time and elapsed
		// time.
		remaining := i.MaxTotal - elapsed
		if remaining <= 0 {
			// No more time!
			i.Retries = 0
			return Stop
		}
	}

	return i.Delay
}
开发者ID:shishkander,项目名称:luci-go,代码行数:31,代码来源:limited.go


示例3: Increment

func (m *memcacheImpl) Increment(key string, delta int64, initialValue *uint64) (uint64, error) {
	now := clock.Now(m.ctx)

	m.data.lock.Lock()
	defer m.data.lock.Unlock()

	cur := uint64(0)
	if initialValue == nil {
		curItm, err := m.data.retrieveLocked(now, key)
		if err != nil {
			return 0, err
		}
		if len(curItm.value) != 8 {
			return 0, errors.New("memcache Increment: got invalid current value")
		}
		cur = binary.LittleEndian.Uint64(curItm.value)
	} else {
		cur = *initialValue
	}
	if delta < 0 {
		if uint64(-delta) > cur {
			cur = 0
		} else {
			cur -= uint64(-delta)
		}
	} else {
		cur += uint64(delta)
	}

	newval := make([]byte, 8)
	binary.LittleEndian.PutUint64(newval, cur)
	m.data.setItemLocked(now, m.NewItem(key).SetValue(newval))

	return cur, nil
}
开发者ID:nishanths,项目名称:gae,代码行数:35,代码来源:memcache.go


示例4: pullAckMessages

// pullAckMessages pulls a set of messages from the configured Subscription.
// If no messages are available, errNoMessages will be returned.
//
// handler is a method that returns true if there was a transient failure,
// indicating that the messages shouldn't be ACK'd.
func (p *pubsubClient) pullAckMessages(ctx context.Context, handler func([]*pubsub.Message)) error {
	var err error
	var msgs []*pubsub.Message
	ackCount := 0

	// Report the duration of a Pull/ACK cycle.
	startTime := clock.Now(ctx)
	defer func() {
		duration := clock.Now(ctx).Sub(startTime)
		log.Fields{
			"count":    len(msgs),
			"ackCount": ackCount,
			"duration": duration,
		}.Infof(ctx, "Pull/ACK cycle complete.")
	}()

	err = retryCall(ctx, "Pull()", func() error {
		var err error
		msgs, err = p.service.Pull(p.subscription, p.batchSize)
		return p.wrapTransient(err)
	})
	log.Fields{
		log.ErrorKey: err,
		"duration":   clock.Now(ctx).Sub(startTime),
		"count":      len(msgs),
	}.Debugf(ctx, "Pull() complete.")

	if err != nil {
		return err
	}

	if len(msgs) == 0 {
		return errNoMessages
	}

	defer func() {
		ackCount, err = p.ackMessages(ctx, msgs)
		if err != nil {
			log.Warningf(log.SetError(ctx, err), "Failed to ACK messages!")
		}
	}()
	handler(msgs)
	return nil
}
开发者ID:nicko96,项目名称:Chrome-Infra,代码行数:49,代码来源:pubsub.go


示例5: SetMulti

func (m *memcacheImpl) SetMulti(items []mc.Item, cb mc.RawCB) error {
	now := clock.Now(m.ctx)
	doCBs(items, cb, func(itm mc.Item) error {
		m.data.lock.Lock()
		defer m.data.lock.Unlock()
		m.data.setItemLocked(now, itm)
		return nil
	})
	return nil
}
开发者ID:nishanths,项目名称:gae,代码行数:10,代码来源:memcache.go


示例6: AddMulti

func (m *memcacheImpl) AddMulti(items []mc.Item, cb mc.RawCB) error {
	now := clock.Now(m.ctx)
	doCBs(items, cb, func(itm mc.Item) error {
		m.data.lock.Lock()
		defer m.data.lock.Unlock()
		if !m.data.hasItemLocked(now, itm.Key()) {
			m.data.setItemLocked(now, itm)
			return nil
		}
		return mc.ErrNotStored
	})
	return nil
}
开发者ID:nishanths,项目名称:gae,代码行数:13,代码来源:memcache.go


示例7: LogCall

func (l *boundCloudLogger) LogCall(level log.Level, calldepth int, f string, args []interface{}) {
	if len(f) == 0 || !log.IsLogging(l.ctx, level) {
		return
	}

	l.logger.logC <- &logEntry{
		timestamp: clock.Now(l.ctx),
		level:     level,
		fmt:       f,
		args:      args,
		fields:    log.GetFields(l.ctx),
	}
}
开发者ID:keelerh,项目名称:chrome_infra,代码行数:13,代码来源:logging.go


示例8: prepTask

func (t *taskQueueData) prepTask(c context.Context, ns string, task *tq.Task, queueName string) (*tq.Task, error) {
	toSched := task.Duplicate()

	if toSched.Path == "" {
		toSched.Path = "/_ah/queue/" + queueName
	}

	if toSched.ETA.IsZero() {
		toSched.ETA = clock.Now(c).Add(toSched.Delay)
	} else if toSched.Delay != 0 {
		panic("taskqueue: both Delay and ETA are set")
	}
	toSched.Delay = 0

	switch toSched.Method {
	// Methods that can have payloads.
	case "":
		toSched.Method = "POST"
		fallthrough
	case "POST", "PUT", "PULL":
		break

	// Methods that can not have payloads.
	case "GET", "HEAD", "DELETE":
		toSched.Payload = nil

	default:
		return nil, fmt.Errorf("taskqueue: bad method %q", toSched.Method)
	}

	if _, ok := toSched.Header[currentNamespace]; !ok {
		if ns != "" {
			if toSched.Header == nil {
				toSched.Header = http.Header{}
			}
			toSched.Header[currentNamespace] = []string{ns}
		}
	}
	// TODO(riannucci): implement DefaultNamespace

	if toSched.Name == "" {
		toSched.Name = mkName(c, "", t.named[queueName])
	} else {
		if !validTaskName.MatchString(toSched.Name) {
			return nil, errors.New("INVALID_TASK_NAME")
		}
	}

	return toSched, nil
}
开发者ID:tetrafolium,项目名称:gae,代码行数:50,代码来源:taskqueue_data.go


示例9: ackMessages

// ackMessages ACKs the supplied messages. If a message is nil, it will be
// ignored.
func (p *pubsubClient) ackMessages(ctx context.Context, messages []*pubsub.Message) (int, error) {
	messageIds := make([]string, 0, len(messages))
	skipped := 0
	for _, msg := range messages {
		if msg != nil {
			messageIds = append(messageIds, msg.AckID)
		} else {
			skipped++
		}
	}
	if len(messageIds) == 0 {
		return 0, nil
	}

	startTime := clock.Now(ctx)
	ctx = log.SetFields(ctx, log.Fields{
		"count":   len(messageIds),
		"skipped": skipped,
	})
	err := retryCall(ctx, "Ack()", func() error {
		return p.wrapTransient(p.service.Ack(p.subscription, messageIds))
	})
	duration := clock.Now(ctx).Sub(startTime)

	if err != nil {
		log.Fields{
			log.ErrorKey: err,
			"duration":   duration,
		}.Errorf(ctx, "Failed to ACK messages.")
		return 0, err
	}

	log.Fields{
		"duration": duration,
	}.Debugf(ctx, "Successfully ACK messages.")
	return len(messageIds), nil
}
开发者ID:nicko96,项目名称:Chrome-Infra,代码行数:39,代码来源:pubsub.go


示例10: CompareAndSwapMulti

func (m *memcacheImpl) CompareAndSwapMulti(items []mc.Item, cb mc.RawCB) error {
	now := clock.Now(m.ctx)
	doCBs(items, cb, func(itm mc.Item) error {
		m.data.lock.Lock()
		defer m.data.lock.Unlock()

		if cur, err := m.data.retrieveLocked(now, itm.Key()); err == nil {
			casid := uint64(0)
			if mi, ok := itm.(*mcItem); ok && mi != nil {
				casid = mi.CasID
			}

			if cur.casID == casid {
				m.data.setItemLocked(now, itm)
			} else {
				return mc.ErrCASConflict
			}
			return nil
		}
		return mc.ErrNotStored
	})
	return nil
}
开发者ID:nishanths,项目名称:gae,代码行数:23,代码来源:memcache.go


示例11: LogCall

func (l *boundCloudLogger) LogCall(level logging.Level, calldepth int, f string, args []interface{}) {
	if len(f) == 0 {
		return
	}

	text := fmt.Sprintf(f, args...)
	fields := logging.GetFields(l.ctx)
	if len(fields) > 0 {
		text = text + " " + fields.FieldString(true)
	}

	// Add logging fields to labels.
	entry := cloudlogging.Entry{
		Timestamp:   clock.Now(l.ctx),
		Severity:    l.getSeverity(level),
		Labels:      make(map[string]string, len(fields)),
		TextPayload: text,
	}

	// Populate Labels.
	for k, v := range fields {
		val := ""
		if l.FieldConverter != nil {
			val = l.FieldConverter(v)
		} else {
			val = fmt.Sprintf("%v", v)
		}
		entry.Labels[k] = val
	}

	// Generate an InsertID, if we're configured with a base.
	if l.InsertIDBase != "" {
		entry.InsertID = l.generateInsertID()
	}

	l.client.PushEntries([]*cloudlogging.Entry{&entry})
}
开发者ID:shishkander,项目名称:luci-go,代码行数:37,代码来源:logging.go


示例12: DeleteMulti

func (m *memcacheImpl) DeleteMulti(keys []string, cb mc.RawCB) error {
	now := clock.Now(m.ctx)

	errs := make([]error, len(keys))

	for i, k := range keys {
		errs[i] = func() error {
			m.data.lock.Lock()
			defer m.data.lock.Unlock()
			_, err := m.data.retrieveLocked(now, k)
			if err != nil {
				return err
			}
			m.data.delItemLocked(k)
			return nil
		}()
	}

	for _, e := range errs {
		cb(e)
	}

	return nil
}
开发者ID:nishanths,项目名称:gae,代码行数:24,代码来源:memcache.go


示例13: GetMulti

func (m *memcacheImpl) GetMulti(keys []string, cb mc.RawItemCB) error {
	now := clock.Now(m.ctx)

	itms := make([]mc.Item, len(keys))
	errs := make([]error, len(keys))

	for i, k := range keys {
		itms[i], errs[i] = func() (mc.Item, error) {
			m.data.lock.RLock()
			defer m.data.lock.RUnlock()
			val, err := m.data.retrieveLocked(now, k)
			if err != nil {
				return nil, err
			}
			return val.toUserItem(k), nil
		}()
	}

	for i, itm := range itms {
		cb(itm, errs[i])
	}

	return nil
}
开发者ID:nishanths,项目名称:gae,代码行数:24,代码来源:memcache.go


示例14: IsGloballyEnabled

// IsGloballyEnabled checks to see if this filter is enabled globally.
//
// This checks InstanceEnabledStatic, as well as polls the datastore entity
//   /dscache,1 (a GlobalConfig instance)
// Once every GlobalEnabledCheckInterval.
//
// For correctness, any error encountered returns true. If this assumed false,
// then Put operations might incorrectly invalidate the cache.
func IsGloballyEnabled(c context.Context) bool {
	if !InstanceEnabledStatic {
		return false
	}

	now := clock.Now(c)

	globalEnabledLock.RLock()
	nextCheck := globalEnabledNextCheck
	enabledVal := globalEnabled
	globalEnabledLock.RUnlock()

	if now.Before(nextCheck) {
		return enabledVal
	}

	globalEnabledLock.Lock()
	defer globalEnabledLock.Unlock()
	// just in case we raced
	if now.Before(globalEnabledNextCheck) {
		return globalEnabled
	}

	// always go to the default namespace
	c, err := info.Get(c).Namespace("")
	if err != nil {
		return true
	}
	cfg := &GlobalConfig{Enable: true}
	if err := datastore.Get(c).Get(cfg); err != nil && err != datastore.ErrNoSuchEntity {
		return true
	}
	globalEnabled = cfg.Enable
	globalEnabledNextCheck = now.Add(GlobalEnabledCheckInterval)
	return globalEnabled
}
开发者ID:nishanths,项目名称:gae,代码行数:44,代码来源:globalconfig.go


示例15: Get

// Get gets a *"math/rand".Rand from the context. If one hasn't been
// set, this creates a new Rand object with a Source initialized from the
// current time clock.Now(c).UnixNano().
func Get(c context.Context) *rand.Rand {
	if f, ok := c.Value(mathRandKey).(Factory); ok && f != nil {
		return f(c)
	}
	return rand.New(rand.NewSource(clock.Now(c).UnixNano()))
}
开发者ID:shishkander,项目名称:luci-go,代码行数:9,代码来源:mathrand.go


示例16: TestTaskQueue

func TestTaskQueue(t *testing.T) {
	t.Parallel()

	Convey("TaskQueue", t, func() {
		now := time.Date(2000, time.January, 1, 1, 1, 1, 1, time.UTC)
		c, tc := testclock.UseTime(context.Background(), now)
		c = mathrand.Set(c, rand.New(rand.NewSource(clock.Now(c).UnixNano())))
		c = Use(c)

		tq := tqS.Get(c)
		tqt := tq.Testable()
		So(tqt, ShouldNotBeNil)

		So(tq, ShouldNotBeNil)

		Convey("implements TQMultiReadWriter", func() {
			Convey("Add", func() {
				t := tq.NewTask("/hello/world")

				Convey("works", func() {
					t.Delay = 4 * time.Second
					t.Header = http.Header{}
					t.Header.Add("Cat", "tabby")
					t.Payload = []byte("watwatwat")
					t.RetryOptions = &tqS.RetryOptions{AgeLimit: 7 * time.Second}
					So(tq.Add(t, ""), ShouldBeNil)

					name := "Z_UjshxM9ecyMQfGbZmUGOEcgxWU0_5CGLl_-RntudwAw2DqQ5-58bzJiWQN4OKzeuUb9O4JrPkUw2rOvk2Ax46THojnQ6avBQgZdrKcJmrwQ6o4qKfJdiyUbGXvy691yRfzLeQhs6cBhWrgf3wH-VPMcA4SC-zlbJ2U8An7I0zJQA5nBFnMNoMgT-2peGoay3rCSbj4z9VFFm9kS_i6JCaQH518ujLDSNCYdjTq6B6lcWrZAh0U_q3a1S2nXEwrKiw_t9MTNQFgAQZWyGBbvZQPmeRYtu8SPaWzTfd25v_YWgBuVL2rRSPSMvlDwE04nNdtvVzE8vNNiA1zRimmdzKeqATQF9_ReUvj4D7U8dcS703DZWfKMBLgBffY9jqCassOOOw77V72Oq5EVauUw3Qw0L6bBsfM9FtahTKUdabzRZjXUoze3EK4KXPt3-wdidau-8JrVf2XFocjjZbwHoxcGvbtT3b4nGLDlgwdC00bwaFBZWff"
					So(tqt.GetScheduledTasks()["default"][name], ShouldResemble, &tqS.Task{
						ETA:          now.Add(4 * time.Second),
						Header:       http.Header{"Cat": []string{"tabby"}},
						Method:       "POST",
						Name:         name,
						Path:         "/hello/world",
						Payload:      []byte("watwatwat"),
						RetryOptions: &tqS.RetryOptions{AgeLimit: 7 * time.Second},
					})
				})

				Convey("picks up namespace", func() {
					c, err := info.Get(c).Namespace("coolNamespace")
					So(err, ShouldBeNil)
					tq = tqS.Get(c)

					t := tq.NewTask("")
					So(tq.Add(t, ""), ShouldBeNil)
					So(t.Header, ShouldResemble, http.Header{
						"X-Appengine-Current-Namespace": {"coolNamespace"},
					})

				})

				Convey("cannot add to bad queues", func() {
					So(tq.Add(nil, "waaat").Error(), ShouldContainSubstring, "UNKNOWN_QUEUE")

					Convey("but you can add Queues when testing", func() {
						tqt.CreateQueue("waaat")
						So(tq.Add(t, "waaat"), ShouldBeNil)

						Convey("you just can't add them twice", func() {
							So(func() { tqt.CreateQueue("waaat") }, ShouldPanic)
						})
					})
				})

				Convey("supplies a URL if it's missing", func() {
					t.Path = ""
					So(tq.Add(t, ""), ShouldBeNil)
					So(t.Path, ShouldEqual, "/_ah/queue/default")
				})

				Convey("cannot add twice", func() {
					t.Name = "bob"
					So(tq.Add(t, ""), ShouldBeNil)

					// can't add the same one twice!
					So(tq.Add(t, ""), ShouldEqual, tqS.ErrTaskAlreadyAdded)
				})

				Convey("cannot add deleted task", func() {
					t.Name = "bob"
					So(tq.Add(t, ""), ShouldBeNil)

					So(tq.Delete(t, ""), ShouldBeNil)

					// can't add a deleted task!
					So(tq.Add(t, ""), ShouldEqual, tqS.ErrTaskAlreadyAdded)
				})

				Convey("cannot set ETA+Delay", func() {
					t.ETA = clock.Now(c).Add(time.Hour)
					tc.Add(time.Second)
					t.Delay = time.Hour
					So(func() {
						So(tq.Add(t, ""), ShouldBeNil)
					}, ShouldPanic)
				})

				Convey("must use a reasonable method", func() {
					t.Method = "Crystal"
//.........这里部分代码省略.........
开发者ID:tetrafolium,项目名称:gae,代码行数:101,代码来源:taskqueue_test.go



注:本文中的github.com/luci/luci-go/common/clock.Now函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang errors.New函数代码示例发布时间:2022-05-23
下一篇:
Golang info.Get函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap