包google.golang.org/grpc
定义类型UnaryClientInterceptor
为:
type UnaryClientInterceptor func(ctx context.Context, method string, req, reply interface{}, cc *ClientConn, invoker UnaryInvoker, opts ...CallOption) error
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我想做的事情如下:
func clientInterceptor() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
//intercept stuff
return invoker(ctx, method, req, reply, cc, opts...)
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到此错误:
"不能使用func文字(类型func("context".Context,string,interface {},interface {},*grpc.ClientConn,grpc.UnaryInvoker,... grpc.CallOption)error)作为返回类型grpc.UnaryClientInterceptor说法"
看看为什么我可以键入别名函数并在不进行转换的情况下使用它们?我得到的印象是,我的返回的匿名函数clientInterceptor()
应该与grpc.ClientInterceptor
类型匹配.
此外,从类型标识的规范(http://golang.org/ref/spec#Type_identity)
如果两个函数类型具有相同数量的参数和结果值,相应的参数和结果类型相同,并且两个函数都是可变参数或两者都不是,则它们是相同的.参数和结果名称不需要匹配.
我已经尝试过铸造和制作类型的变量,grpc.UnaryClientInterceptor
但没有任何作用.
我也基本上做了同样的事情grpc.UnaryServerInterceptor
并且没有问题.
我在这里错过了什么?
我看到以下情况:
func foo(ctx context.Context) {
localCtx := ctx
... //do stuff
}
Run Code Online (Sandbox Code Playgroud)
这两个context.Context
变量可以在所有方面互换使用吗?
查看源代码,我看到context.Context
从WithCancel
、WithDeadline
、WithTimeout
和WithValue
返回的返回变量是通过指向结构的指针在内部实现的,这让我认为是的,如果父上下文来自这些函数之一,它们可以互换使用。但是,emptyCtx
返回的 bycontext.Background()
在内部是一个 int ,所以在这里我想如果父上下文是背景上下文,它们可能无法在内部使用。
但context.Context
实际上是一个界面,我不确定它是否/如何改变事情。
当上下文变量超出范围并且未明确调用cancel时,context.Done()是否会解锁?
假设我有以下代码:
func DoStuff() {
ctx, _ := context.WithCancel(context.Background())
go DoWork(ctx)
return
}
Run Code Online (Sandbox Code Playgroud)
在DoStuff()中返回后,ctx.Done()会在DoWork中解除阻塞吗?
我找到了这个帖子https://groups.google.com/forum/#!topic/golang-nuts/BbvTlaQwhjw,其中询问如何使用Context.Done()的人声称context.Done()会在上下文变量离开范围,但没有人验证这一点,我没有在文档中看到任何内容.
我正在使用 Windows Server 2016 来启动windowsservercore
docker 容器,并注意到我认为不正确的行为,即容器很快退出,即使它应该休眠超过 15 分钟。我有以下内容Dockerfile
:
FROM microsoft/windowsservercore
RUN powershell Start-Sleep -s 1000
Run Code Online (Sandbox Code Playgroud)
docker build -t mybuild .
我使用when构建容器与Dockerfile
. 然后我运行容器docker run mybuild
,它很快就退出了。
看看这个答案,似乎睡眠应该让容器保持活力。这个答案显示的是 Linux,所以不确定这是否重要,但我觉得睡眠进程在任何一种情况下都在运行,这就是决定容器是否应该默认退出的原因
如果我使用交互模式和/或(我尝试了所有 3 种组合) tty ( docker run -it mybuild
),它会一直保持状态,直到我退出容器的 shell
查看 docker 文档run
会在前台执行容器(如 -it ),尽管我不明白为什么这很重要,因为无论容器是否分离,进程都应该仍在运行。我还尝试以分离模式运行它docker run -d
,在这种情况下它也很快退出。
我还尝试在睡眠后运行另一个命令,但仍然不起作用。docker 文件如下所示:
FROM microsoft/windowsservercore
RUN powershell Start-Sleep -s 1000
RUN echo "hello" > C:\hello.txt
Run Code Online (Sandbox Code Playgroud)
我查看了dockerfile 参考RUN
,它说以 shell 形式在 Windows …