问题:
thrift0.12在gen go代码的时候默认增加了一个参数context
IDL是:
Response GetCommentAll(
1:required i32 businessType
)
//生成go方法时增加了content:
GetCommentAll(ctx context.Context, businessType int32) (r *Response, err error)
//生成的php代码并未增加context:
public function GetCommentAll($businessType)
所以我们在go代码实现GetCommentAll 方法时必须加上 context参数
func (s *Service) GetCommentAll(ctx context.Context, businessType int32) (r *thriftdemo.Response, err error) {
......
}
再来看看调用方法:
//go调用时:
newthriftdemo.GetCommentList(context.Background(), 1)
//php调用时:
$client->GetCommentList(1);
我们发现,go调用时必须传递的context参数,但php上是没有的。
这就带来了一个问题,本来想通过这个参数传递trace信息, 可是php怎么去传递呢?
尝试在go上给context赋值,但是通过debug模式看,并没有把这个值传递过去:
ctx := context.WithValue(context.Background(), "trycontext", "balabalabala")
newthriftdemo.GetCommentList(ctx, 1)
Run:
2019/04/19 11:43:04 client:WriteMessageBegin(name="GetCommentList", typeId=1, seqid=1) => <nil>
2019/04/19 11:43:04 client:WriteStructBegin(name="GetCommentList_args") => <nil>
2019/04/19 11:43:04 client:WriteFieldBegin(name="businessType", typeId=0x8, id2) => <nil>
2019/04/19 11:43:04 client:WriteI32(value=1) => <nil>
2019/04/19 11:43:04 client:WriteFieldEnd() => <nil>
2019/04/19 11:43:04 client:WriteFieldStop() => <nil>
2019/04/19 11:43:04 client:WriteStructEnd() => <nil>
2019/04/19 11:43:04 client:WriteMessageEnd() => <nil>
2019/04/19 11:43:04 client:Flush()
思考:
看了下源码,关于0.12的diff:https://github.com/apache/thrift/commit/5785279e2e809f6c56dbbe0eb41d13fb17c88bdd#diff-8a01e4124163225974263ecb577e8bf4
- f_types_ << indent() << " oprot.Flush(ctx)" << endl;
但实际上,并没有传递ctx。
在Changelog for Apache Thrift 0.12.0上写了:https://abi-laboratory.pro/index.php?view=changelog&l=apache-thrift&v=0.12.0
*[THRIFT-4674] - Add stream context support into PHP/THttpClient
感觉context这里是个bug,并没有传递过去,或者说context只能在 THttpClient 模式才能传递?
找到一篇文章关于通过扩展TBinaryProtocol来增加一个数据传递,可是php怎么处理呢?https://www.kancloud.cn/digest/thrift/118992