smartwalle / alipay

支付宝 AliPay SDK for Go, 集成简单,功能完善,持续更新,支持公钥证书和普通公钥进行签名和验签,支持文件上传和接口内容加密。
MIT License
1.85k stars 417 forks source link

feat: 支持context #201

Closed mingzaily closed 4 months ago

mingzaily commented 4 months ago

doRequest操作设置ctx

按照现在的go开发习惯,一般会将context作为函数的第一个参数传入,但为了防止破坏性更新,只能使用链路注入的方法 另外issues说的otel,想了想确实不该在这个包处理,初始化http.Client修改Transport即可

200

smartwalle commented 4 months ago

哥们,感谢你的提交,但是 ctx 还是不建议这样使用,最好还是遵循go的建议,通过每个方法单独传递 ctx 为好。

这样写会导致你的追踪不准确,并且也会因为 ctx 引发其它的一些问题:

1、client 会一直持有这个 ctx,如果传递的 ctx 因为一些原因取消了,而没有更新 client 里面的 ctx, 就会导致后续业务都没有办法继续; 2、还是因为 client 持有这个 ctx 引起的问题,会出现多个不同的请求出现在同一个追踪链路中;

mingzaily commented 4 months ago

我也想遵循go的建议,通过每个方法单独传递 ctx ,那为了不破坏性更新,只能这样;如果不接受client持有ctx的办法就只有两种处理了

  1. 给每个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
    }
  2. 每个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 }