Closed mingzaily closed 4 months ago
哥们,感谢你的提交,但是 ctx 还是不建议这样使用,最好还是遵循go的建议,通过每个方法单独传递 ctx 为好。
这样写会导致你的追踪不准确,并且也会因为 ctx 引发其它的一些问题:
1、client 会一直持有这个 ctx,如果传递的 ctx 因为一些原因取消了,而没有更新 client 里面的 ctx, 就会导致后续业务都没有办法继续; 2、还是因为 client 持有这个 ctx 引起的问题,会出现多个不同的请求出现在同一个追踪链路中;
我也想遵循go的建议,通过每个方法单独传递 ctx ,那为了不破坏性更新,只能这样;如果不接受client持有ctx的办法就只有两种处理了
给每个Public方法加一个带context的入参,稍微会破坏更新
func (c *Client) TradeFastPayRefundQuery(ctx context.Context, param TradeFastPayRefundQuery) (result *TradeFastPayRefundQueryRsp, err error) {
err = c.doRequest(ctx, "POST", param, &result)
return result, err
}
每个Public方法都重写一个v2,保证用户可以直接升级,如果想用context就切到v2方法
func (c *Client) TradeFastPayRefundQuery(param TradeFastPayRefundQuery) (result *TradeFastPayRefundQueryRsp, err error) {
err = c.doRequest(context.Background(), "POST", param, &result)
return result, err
}
func (c Client) TradeFastPayRefundQueryV2(ctx context.Context, param TradeFastPayRefundQuery) (result TradeFastPayRefundQueryRsp, err error) { err = c.doRequest(ctx, "POST", param, &result) return result, err }
doRequest操作设置ctx
200