dongwo5449 2019-09-04 09:57
浏览 41
已采纳

在Google App Engine上从main.go传递出去后,上下文值发生了变化

There's a problem where I don't know why a context.Context was changed once I pass it to a different package on Google App Engine.

The following code works fine when running on App Engine:

package main

import (
    "net/http"
    "log"

    "google.golang.org/appengine"
)

func main() {
    http.HandleFunc("/", myHandler)

    appengine.Main()
}

func myHandler(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()
    account, err := appengine.ServiceAccount(ctx)
    if err != nil {
        log.Println("[myHandler] error:", err)
    } else {
        log.Println("[myHandler] ServiceAccount:", account)
    }   

    w.Write([]byte("ok"))
}

I could retrieve the ServiceAccount successfully when accessing /, and everything was good.

However, when I passed the context from main.go to another package, the function call didn't work. The following was added to main.go:

import (
    // other stuff
    "github.com/adlerhsieh/q_context/handlers"
)

func main() {
    http.HandleFunc("/", myHandler)

    appengine.Main()
}

func myHandler(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()
    account, err := appengine.ServiceAccount(ctx)
    if err != nil {
        log.Println("[myHandler] error:", err)
    } else {
        log.Println("[myHandler] ServiceAccount:", account)
    }   

    handlers.AnotherFunc(ctx) // <--- added this

    w.Write([]byte("ok"))
}

Another package:

package handlers

import (
    "log"
    "context"

    "google.golang.org/appengine"
)

func AnotherFunc(ctx context.Context) {
    account, err := appengine.ServiceAccount(ctx)
    if err != nil {
        log.Println("[AnotherFunc] error:", err)
    } else {
        log.Println("[AnotherFunc] ServiceAccount:", account)
    }
}

When I ran it on App Engine, the log said:

2019/09/04 09:36:30 [myHandler] ServiceAccount: myaccount@gmail.com
2019/09/04 09:36:30 [AnotherFunc] error: not an App Engine context

The function calls are the same, but just in different packages. I dug in the package itself and found that it uses the key here (which leads to here) to setup the context. And here to check whether that value was setup properly. However, that value seem to be modified/changed so that the second function call couldn't get it. Even if I omitted the first function call and went straight to the second one, it still has the same error.

Any idea why context object was modified when passing to another package?

The following is my app.yaml:

runtime: go111
service: default
instance_class: F1
automatic_scaling:
  min_idle_instances: 0
  max_idle_instances: automatic
  min_pending_latency: automatic
  max_pending_latency: automatic
  max_concurrent_requests: 30

handlers:
- url: /.*
  script: auto
  login: admin

nobuild_files:
- vendor

env_variables:
  ENV: 'dev'
  GO111MODULE: 'off'

Here is the GitHub repo link.

Thank you!

  • 写回答

1条回答 默认 最新

  • duanpan3166 2019-09-05 06:49
    关注

    It turns out that my code actually worked. It's because of some other operation error.

    However, I'll just post the issue that actually caused it so it can help those who have the same issue.

    With the new go111 runtime, it treats packages from non-root directory or its subdirectories as a different type of package. This caused the problem with "not an App Engine context". I'll just call it an "outcast" package for now (cause I'm not entirely sure why's that).

    For example:

    - appengine
      - main.go
      - handlers
        - handlers.go <-- this is a regular package
    
    - appengine
      - main.go
    - handlers
      - handlers.go < -- this is an outcast package
    

    An outcast package would have issues handling context.Context generated from App Engine, as pointed out in my question.

    The mechanism of App Engine knowing that the context is created from App Engine, is using a built-in value that can only be retrieved from its internal package (with an un-exported pointer-string key). When passing the context to an outcast package, we can no longer retrieve the value from the context. It's still a mystery for me that why the value disappeared, but it's probably because of some Go compiling mechanism.

    The solution would be moving the main.go to the top-level directory in the project, so that there would be no outcast package anywhere.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?