本文整理匯總了Golang中github.com/MiniProfiler/go/miniprofiler_gae.Context.Step方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Step方法的具體用法?Golang Context.Step怎麽用?Golang Context.Step使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/MiniProfiler/go/miniprofiler_gae.Context
的用法示例。
在下文中一共展示了Context.Step方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetFeed
func GetFeed(c mpg.Context, w http.ResponseWriter, r *http.Request) {
gn := goon.FromContext(c)
f := Feed{Url: r.FormValue("f")}
var stars []string
wg := sync.WaitGroup{}
fk := gn.Key(&f)
q := datastore.NewQuery(gn.Kind(&Story{})).Ancestor(fk).KeysOnly()
q = q.Order("-" + IDX_COL)
if cur := r.FormValue("c"); cur != "" {
if dc, err := datastore.DecodeCursor(cur); err == nil {
q = q.Start(dc)
}
} else {
// grab the stars list on the first run
wg.Add(1)
go c.Step("stars", func(c mpg.Context) {
gn := goon.FromContext(c)
usk := starKey(c, f.Url, "")
q := datastore.NewQuery(gn.Kind(&UserStar{})).Ancestor(gn.Key(usk).Parent()).KeysOnly()
keys, _ := gn.GetAll(q, nil)
stars = make([]string, len(keys))
for i, key := range keys {
stars[i] = starID(key)
}
wg.Done()
})
}
iter := gn.Run(q)
var stories []*Story
for i := 0; i < 20; i++ {
if k, err := iter.Next(nil); err == nil {
stories = append(stories, &Story{
Id: k.StringID(),
Parent: k.Parent(),
})
} else if err == datastore.Done {
break
} else {
serveError(w, err)
return
}
}
cursor := ""
if ic, err := iter.Cursor(); err == nil {
cursor = ic.String()
}
gn.GetMulti(&stories)
wg.Wait()
b, _ := json.Marshal(struct {
Cursor string
Stories []*Story
Stars []string `json:",omitempty"`
}{
Cursor: cursor,
Stories: stories,
Stars: stars,
})
w.Write(b)
}
示例2: ListFeeds
func ListFeeds(c mpg.Context, w http.ResponseWriter, r *http.Request) {
cu := user.Current(c)
gn := goon.FromContext(c)
u := &User{Id: cu.ID}
ud := &UserData{Id: "data", Parent: gn.Key(u)}
gn.GetMulti([]interface{}{u, ud})
read := make(Read)
var uf Opml
c.Step("unmarshal user data", func() {
json.Unmarshal(ud.Read, &read)
json.Unmarshal(ud.Opml, &uf)
})
var feeds []*Feed
opmlMap := make(map[string]*OpmlOutline)
c.Step("fetch feeds", func() {
for _, outline := range uf.Outline {
if outline.XmlUrl == "" {
for _, so := range outline.Outline {
feeds = append(feeds, &Feed{Url: so.XmlUrl})
opmlMap[so.XmlUrl] = so
}
} else {
feeds = append(feeds, &Feed{Url: outline.XmlUrl})
opmlMap[outline.XmlUrl] = outline
}
}
gn.GetMulti(feeds)
})
lock := sync.Mutex{}
fl := make(map[string][]*Story)
q := datastore.NewQuery(gn.Key(&Story{}).Kind())
hasStories := false
updatedLinks := false
icons := make(map[string]string)
c.Step("feed fetch + wait", func() {
queue := make(chan *Feed)
wg := sync.WaitGroup{}
feedProc := func() {
for f := range queue {
defer wg.Done()
var newStories []*Story
if u.Read.Before(f.Date) {
c.Debugf("query for %v", f.Url)
fk := gn.Key(f)
sq := q.Ancestor(fk).Filter("p >", u.Read).KeysOnly().Order("-p")
keys, _ := gn.GetAll(sq, nil)
stories := make([]*Story, len(keys))
for j, key := range keys {
stories[j] = &Story{
Id: key.StringID(),
Parent: fk,
}
}
gn.GetMulti(stories)
for _, st := range stories {
found := false
for _, s := range read[f.Url] {
if s == st.Id {
found = true
break
}
}
if !found {
newStories = append(newStories, st)
}
}
}
if f.Link != opmlMap[f.Url].HtmlUrl {
updatedLinks = true
c.Debugf("fixing link %v, %v -> %v", f.Url, opmlMap[f.Url].HtmlUrl, f.Link)
opmlMap[f.Url].HtmlUrl = f.Link
}
lock.Lock()
fl[f.Url] = newStories
if len(newStories) > 0 {
hasStories = true
}
if f.Image != "" {
icons[f.Url] = f.Image
}
lock.Unlock()
}
}
for i := 0; i < 20; i++ {
go feedProc()
}
wg.Add(len(feeds))
for _, f := range feeds {
queue <- f
}
close(queue)
wg.Wait()
})
if !hasStories {
var last time.Time
for _, f := range feeds {
if last.Before(f.Date) {
//.........這裏部分代碼省略.........
示例3: ListFeeds
func ListFeeds(c mpg.Context, w http.ResponseWriter, r *http.Request) {
cu := user.Current(c)
gn := goon.FromContext(c)
u := &User{Id: cu.ID}
ud := &UserData{Id: "data", Parent: gn.Key(u)}
gn.GetMulti([]interface{}{u, ud})
put := false
fixRead := false
if time.Since(u.Read) > oldDuration {
c.Warningf("u.Read too old, fixing: %v", u.Read)
u.Read = time.Now().Add(-oldDuration)
put = true
fixRead = true
c.Warningf("new: %v", u.Read)
}
read := make(Read)
var uf Opml
c.Step("unmarshal user data", func() {
gob.NewDecoder(bytes.NewReader(ud.Read)).Decode(&read)
json.Unmarshal(ud.Opml, &uf)
})
var feeds []*Feed
opmlMap := make(map[string]*OpmlOutline)
var merr error
c.Step("fetch feeds", func() {
for _, outline := range uf.Outline {
if outline.XmlUrl == "" {
for _, so := range outline.Outline {
feeds = append(feeds, &Feed{Url: so.XmlUrl})
opmlMap[so.XmlUrl] = so
}
} else {
feeds = append(feeds, &Feed{Url: outline.XmlUrl})
opmlMap[outline.XmlUrl] = outline
}
}
merr = gn.GetMulti(feeds)
})
lock := sync.Mutex{}
fl := make(map[string][]*Story)
q := datastore.NewQuery(gn.Key(&Story{}).Kind())
hasStories := false
updatedLinks := false
icons := make(map[string]string)
now := time.Now()
numStories := 0
c.Step("feed fetch + wait", func() {
queue := make(chan *Feed)
wg := sync.WaitGroup{}
feedProc := func() {
for f := range queue {
defer wg.Done()
var stories []*Story
if u.Read.Before(f.Date) {
fk := gn.Key(f)
sq := q.Ancestor(fk).Filter(IDX_COL+" >", u.Read).KeysOnly().Order("-" + IDX_COL)
keys, _ := gn.GetAll(sq, nil)
stories = make([]*Story, len(keys))
for j, key := range keys {
stories[j] = &Story{
Id: key.StringID(),
Parent: fk,
}
}
gn.GetMulti(stories)
}
if f.Link != opmlMap[f.Url].HtmlUrl {
updatedLinks = true
opmlMap[f.Url].HtmlUrl = f.Link
}
if f.Errors == 0 && f.NextUpdate.Before(now) {
t := taskqueue.NewPOSTTask(routeUrl("update-feed"), url.Values{
"feed": {f.Url},
})
if _, err := taskqueue.Add(c, t, "update-manual"); err != nil {
c.Errorf("taskqueue error: %v", err.Error())
} else {
c.Warningf("manual feed update: %v", f.Url)
}
}
f.Subscribe(c)
lock.Lock()
fl[f.Url] = stories
numStories += len(stories)
if len(stories) > 0 {
hasStories = true
}
if f.Image != "" {
icons[f.Url] = f.Image
}
lock.Unlock()
}
}
for i := 0; i < 20; i++ {
go feedProc()
}
for i, f := range feeds {
//.........這裏部分代碼省略.........
示例4: ListFeeds
func ListFeeds(c mpg.Context, w http.ResponseWriter, r *http.Request) {
cu := user.Current(c)
gn := goon.FromContext(c)
u := &User{Id: cu.ID}
ud := &UserData{Id: "data", Parent: gn.Key(u)}
if err := gn.GetMulti([]interface{}{u, ud}); err != nil && !goon.NotFound(err, 1) {
serveError(w, err)
return
}
l := &Log{
Parent: ud.Parent,
Id: time.Now().UnixNano(),
Text: "list feeds",
}
l.Text += fmt.Sprintf(", len opml %v", len(ud.Opml))
putU := false
putUD := false
fixRead := false
if time.Since(u.Read) > oldDuration {
u.Read = time.Now().Add(-oldDuration)
putU = true
fixRead = true
l.Text += ", u.Read"
}
trialRemaining := 0
if STRIPE_KEY != "" && ud.Opml != nil {
if u.Created.IsZero() {
u.Created = time.Now()
putU = true
} else if time.Since(u.Created) > accountFreeDuration {
b, _ := json.Marshal(struct {
ErrorSubscription bool
}{
true,
})
w.Write(b)
return
}
trialRemaining = int((accountFreeDuration-time.Since(u.Created))/time.Hour/24) + 1
}
read := make(Read)
var uf Opml
c.Step("unmarshal user data", func(c mpg.Context) {
gob.NewDecoder(bytes.NewReader(ud.Read)).Decode(&read)
json.Unmarshal(ud.Opml, &uf)
})
var feeds []*Feed
opmlMap := make(map[string]*OpmlOutline)
var merr error
c.Step("fetch feeds", func(c mpg.Context) {
gn := goon.FromContext(appengine.Timeout(c, time.Minute))
for _, outline := range uf.Outline {
if outline.XmlUrl == "" {
for _, so := range outline.Outline {
feeds = append(feeds, &Feed{Url: so.XmlUrl})
opmlMap[so.XmlUrl] = so
}
} else {
feeds = append(feeds, &Feed{Url: outline.XmlUrl})
opmlMap[outline.XmlUrl] = outline
}
}
merr = gn.GetMulti(feeds)
})
lock := sync.Mutex{}
fl := make(map[string][]*Story)
q := datastore.NewQuery(gn.Key(&Story{}).Kind()).
Filter(IDX_COL+" >=", u.Read).
KeysOnly().
Order("-" + IDX_COL).
Limit(250)
updatedLinks := false
now := time.Now()
numStories := 0
c.Step(fmt.Sprintf("feed unreads: %v", u.Read), func(c mpg.Context) {
queue := make(chan *Feed)
tc := make(chan *taskqueue.Task)
done := make(chan bool)
wg := sync.WaitGroup{}
feedProc := func() {
for f := range queue {
c.Step(f.Title, func(c mpg.Context) {
defer wg.Done()
var stories []*Story
gn := goon.FromContext(appengine.Timeout(c, time.Minute))
if !f.Date.Before(u.Read) {
fk := gn.Key(f)
sq := q.Ancestor(fk)
keys, _ := gn.GetAll(sq, nil)
stories = make([]*Story, len(keys))
for j, key := range keys {
stories[j] = &Story{
Id: key.StringID(),
Parent: fk,
}
}
gn.GetMulti(stories)
}
//.........這裏部分代碼省略.........
示例5: ListFeeds
func ListFeeds(c mpg.Context, w http.ResponseWriter, r *http.Request) {
cu := user.Current(c)
gn := goon.FromContext(c)
u := &User{Id: cu.ID}
ud := &UserData{Id: "data", Parent: gn.Key(u)}
gn.GetMulti([]interface{}{u, ud})
read := make(Read)
var uf Opml
c.Step("unmarshal user data", func() {
json.Unmarshal(ud.Read, &read)
json.Unmarshal(ud.Opml, &uf)
})
var feeds []*Feed
opmlMap := make(map[string]*OpmlOutline)
var merr error
c.Step("fetch feeds", func() {
for _, outline := range uf.Outline {
if outline.XmlUrl == "" {
for _, so := range outline.Outline {
feeds = append(feeds, &Feed{Url: so.XmlUrl})
opmlMap[so.XmlUrl] = so
}
} else {
feeds = append(feeds, &Feed{Url: outline.XmlUrl})
opmlMap[outline.XmlUrl] = outline
}
}
merr = gn.GetMulti(feeds)
})
lock := sync.Mutex{}
fl := make(map[string][]*Story)
q := datastore.NewQuery(gn.Key(&Story{}).Kind())
hasStories := false
updatedLinks := false
icons := make(map[string]string)
now := time.Now()
c.Step("feed fetch + wait", func() {
queue := make(chan *Feed)
wg := sync.WaitGroup{}
feedProc := func() {
for f := range queue {
defer wg.Done()
var newStories []*Story
if u.Read.Before(f.Date) {
c.Debugf("query for %v", f.Url)
fk := gn.Key(f)
sq := q.Ancestor(fk).Filter(IDX_COL+" >", u.Read).KeysOnly().Order("-" + IDX_COL)
keys, _ := gn.GetAll(sq, nil)
stories := make([]*Story, len(keys))
for j, key := range keys {
stories[j] = &Story{
Id: key.StringID(),
Parent: fk,
}
}
gn.GetMulti(stories)
for _, st := range stories {
found := false
for _, s := range read[f.Url] {
if s == st.Id {
found = true
break
}
}
if !found {
newStories = append(newStories, st)
}
}
}
if f.Link != opmlMap[f.Url].HtmlUrl {
updatedLinks = true
opmlMap[f.Url].HtmlUrl = f.Link
}
if f.Errors == 0 && f.NextUpdate.Before(now) {
t := taskqueue.NewPOSTTask(routeUrl("update-feed"), url.Values{
"feed": {f.Url},
})
if _, err := taskqueue.Add(c, t, "update-manual"); err != nil {
c.Errorf("taskqueue error: %v", err.Error())
} else {
c.Warningf("manual feed update: %v", f.Url)
}
}
f.Subscribe(c)
lock.Lock()
fl[f.Url] = newStories
if len(newStories) > 0 {
hasStories = true
}
if f.Image != "" {
icons[f.Url] = f.Image
}
lock.Unlock()
}
}
for i := 0; i < 20; i++ {
go feedProc()
//.........這裏部分代碼省略.........
示例6: ListFeeds
func ListFeeds(c mpg.Context, w http.ResponseWriter, r *http.Request) {
cu := user.Current(c)
gn := goon.FromContext(c)
u := &User{Id: cu.ID}
ud := &UserData{Id: "data", Parent: gn.Key(u)}
gn.GetMulti([]interface{}{u, ud})
putU := false
putUD := false
fixRead := false
if time.Since(u.Read) > oldDuration {
u.Read = time.Now().Add(-oldDuration)
putU = true
fixRead = true
}
read := make(Read)
var uf Opml
c.Step("unmarshal user data", func() {
gob.NewDecoder(bytes.NewReader(ud.Read)).Decode(&read)
json.Unmarshal(ud.Opml, &uf)
})
var feeds []*Feed
opmlMap := make(map[string]*OpmlOutline)
var merr error
c.Step("fetch feeds", func() {
for _, outline := range uf.Outline {
if outline.XmlUrl == "" {
for _, so := range outline.Outline {
feeds = append(feeds, &Feed{Url: so.XmlUrl})
opmlMap[so.XmlUrl] = so
}
} else {
feeds = append(feeds, &Feed{Url: outline.XmlUrl})
opmlMap[outline.XmlUrl] = outline
}
}
merr = gn.GetMulti(feeds)
})
lock := sync.Mutex{}
fl := make(map[string][]*Story)
q := datastore.NewQuery(gn.Key(&Story{}).Kind())
hasStories := false
updatedLinks := false
icons := make(map[string]string)
noads := make(map[string]bool)
now := time.Now()
numStories := 0
c.Step("feed fetch + wait", func() {
queue := make(chan *Feed)
tc := make(chan *taskqueue.Task)
wg := sync.WaitGroup{}
feedProc := func() {
for f := range queue {
defer wg.Done()
var stories []*Story
if u.Read.Before(f.Date) {
fk := gn.Key(f)
sq := q.Ancestor(fk).Filter(IDX_COL+" >", u.Read).KeysOnly().Order("-" + IDX_COL)
keys, _ := gn.GetAll(sq, nil)
stories = make([]*Story, len(keys))
for j, key := range keys {
stories[j] = &Story{
Id: key.StringID(),
Parent: fk,
}
}
gn.GetMulti(stories)
}
if f.Link != opmlMap[f.Url].HtmlUrl {
updatedLinks = true
opmlMap[f.Url].HtmlUrl = f.Link
}
manualDone := false
if time.Since(f.LastViewed) > time.Hour*24*2 {
if f.NextUpdate.Equal(timeMax) {
tc <- taskqueue.NewPOSTTask(routeUrl("update-feed-manual"), url.Values{
"feed": {f.Url},
"last": {"1"},
})
manualDone = true
} else {
tc <- taskqueue.NewPOSTTask(routeUrl("update-feed-last"), url.Values{
"feed": {f.Url},
})
}
}
if !manualDone && now.Sub(f.NextUpdate) >= 0 {
tc <- taskqueue.NewPOSTTask(routeUrl("update-feed-manual"), url.Values{
"feed": {f.Url},
})
}
lock.Lock()
fl[f.Url] = stories
numStories += len(stories)
if len(stories) > 0 {
hasStories = true
}
if f.Image != "" {
//.........這裏部分代碼省略.........