当前位置: 首页>>代码示例>>Golang>>正文


Golang Key.MarshalJSON方法代码示例

本文整理汇总了Golang中google/golang.org/appengine/datastore.Key.MarshalJSON方法的典型用法代码示例。如果您正苦于以下问题:Golang Key.MarshalJSON方法的具体用法?Golang Key.MarshalJSON怎么用?Golang Key.MarshalJSON使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在google/golang.org/appengine/datastore.Key的用法示例。


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

示例1: NewTask

// NewTask creates a new taskqueue.Task for the entity with the correct
// headers set to match those on the entity
func (l *Locker) NewTask(key *datastore.Key, entity Lockable, path string, params url.Values) *taskqueue.Task {
	// prepare the lock entries
	lock := entity.getLock()
	lock.Timestamp = getTime()
	lock.RequestID = ""
	lock.Retries = 0
	lock.Sequence++

	json, _ := key.MarshalJSON()

	// set task headers so that we can retrieve the matching entity
	// and check that the executing task is the one we're expecting
	task := taskqueue.NewPOSTTask(path, params)
	task.Header.Set("X-Lock-Seq", strconv.Itoa(lock.Sequence))
	task.Header.Set("X-Lock-Key", string(json))

	if l.Host != "" {
		task.Header.Set("Host", l.Host)
	}

	return task
}
开发者ID:CaptainCodeman,项目名称:datastore-locker,代码行数:24,代码来源:task.go

示例2: counterHandler

func counterHandler(c context.Context, r *http.Request, key *datastore.Key, entity locker.Lockable) error {
	counter := entity.(*Counter)
	log.Debugf(c, "process: %d", counter.Sequence)

	// simulate some processing work
	time.Sleep(time.Duration(1) * time.Second)
	if counter.Sequence == 5 {
		// simulate a duplicate task execution by creating one ourselves
		// needless to say, you wouldn't want to be doing this in practice
		// but it should demonstrate that the locker prevents spurious
		// task execution and guarantees the correct sequencing happens
		json, _ := key.MarshalJSON()
		t := taskqueue.NewPOSTTask("/process", nil)
		t.Header.Set("X-Lock-Seq", "6")
		t.Header.Set("X-Lock-Key", string(json))
		taskqueue.Add(c, t, "")
	}

	if counter.Sequence < counter.Limit {
		return l.Schedule(c, key, counter, "/process", nil)
	}

	return l.Complete(c, key, counter)
}
开发者ID:CaptainCodeman,项目名称:datastore-locker,代码行数:24,代码来源:main.go


注:本文中的google/golang.org/appengine/datastore.Key.MarshalJSON方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。