通过goroutine异步发布到google pub sub [英] Publishing to google pub sub asynchronously through goroutine

查看:162
本文介绍了通过goroutine异步发布到google pub sub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过goroutine将消息异步推送到google pub-sub,但是我遇到了以下错误 panic: not an App Engine context 我正在使用mux并有一个api处理程序

I'm trying to push the message to google pub-sub asynchronously through goroutine but I'm facing below error panic: not an App Engine context I'm using mux and have an api handler

n = 1百万

    func apihandler(w http.ResponseWriter, r *http.Request) {
       go createuniquecodes(n)
       return "request running in background"
    }

    func createuniquecodes(n) {
       c := make(chan string)
       go createuniquecodes(c, n)  
       for val := range c {        
           publishtopubsub(val)
       } 
   }
   func createuniquecodes(n) {
        for i := 0; i < n; i++ {
           uniquecode := some random string
           // publish to channel and pubsub
           c <- uniquecode
        }
        close(c)
   } 

func publishuq(msg string) error {
   ctx := context.Background()
   client, err := pubsub.NewClient(ctx, projectId)
   if err != nil {
     log.Fatalf("Could not create pubsub Client: %v", err)
   }
   t := client.Topic(topicName)
   result := t.Publish(ctx, &pubsub.Message{
   Data: []byte(msg),
 })
 id, err := result.Get(ctx)
 if err != nil {
    return err
}
fmt.Printf("Published a message; msg ID: %v\n", id)
return nil

}

请注意,我需要生成500万个唯一代码, 由于我是异步进行的,因此如何在go例程中定义上下文

Please note that I need to generate 5 million unique codes, How will I define a context in go routine since I'm doing everything asynchronously

推荐答案

我假设您使用的是App Engine标准(非灵活)环境.请注意,请求处理程序(在您的情况下为apihandler)在有限的时间内生成并返回对请求的响应,

I assume you're using the App Engine standard (not flexible) environment. Please note that a "request handler (apihandler in your case) has a limited amount of time to generate and return a response to a request, typically around 60 seconds. Once the deadline has been reached, the request handler is interrupted".

您正在尝试在调用go createuniquecodes(n)然后在ctx := context.Background()时中断"请求,这是not an App Engine context感到恐慌的原因.从技术上讲,您可以使用 NewContext(req * http.Request)从原始上下文中获取有效上下文,但是同样,请求超时之前只有60秒.

You're trying to "break out" of the request when calling go createuniquecodes(n) and then ctx := context.Background() down the line is what panics with not an App Engine context. You could technically use NewContext(req *http.Request) to derive a valid context from the original context, but again, you'd only have 60s before your request times out.

请查看任务队列,因为它们让应用程序在用户请求之外异步执行工作(称为任务)."

Please have a look at TaskQueues, as they " let applications perform work, called tasks, asynchronously outside of a user request."

这篇关于通过goroutine异步发布到google pub sub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆