[ https://issues.apache.org/jira/browse/THRIFT-4553?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16436847#comment-16436847 ]
Can Celasun edited comment on THRIFT-4553 at 4/13/18 5:26 AM: -------------------------------------------------------------- {quote} I want to do it in BinaryProtocol, too {quote} The protocol doesn't matter, the transport does. For non-HTTP transports a middleware layer is not always feasible. {quote} do you have some suggestions? {quote} Yes, you can already do this by using custom processor that embeds the generated processor. For example, if you have a {{service Foo}} in your Thrift file, the generated code will have a {{Process}} method like this: {code:go} func (p *FooProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { name, _, seqId, err := iprot.ReadMessageBegin() if err != nil { return false, err } if processor, ok := p.GetProcessorFunction(name); ok { return processor.Process(ctx, seqId, iprot, oprot) } iprot.Skip(thrift.STRUCT) iprot.ReadMessageEnd() x247 := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name) oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId) x247.Write(oprot) oprot.WriteMessageEnd() oprot.Flush() return false, x247 } {code} What you can do is create another processor, embed {{FooProcessor}} into it and override {{Process}}, like this: {code:go} type MyProcessor struct { *FooProcessor } func (p *MyProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { name, _, seqId, err := iprot.ReadMessageBegin() if err != nil { return false, err } if processor, ok := p.GetProcessorFunction(name); ok { // HERE: Do anything with "name" before calling processor.Process() return processor.Process(ctx, seqId, iprot, oprot) } iprot.Skip(thrift.STRUCT) iprot.ReadMessageEnd() x247 := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name) oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId) x247.Write(oprot) oprot.WriteMessageEnd() oprot.Flush() return false, x247 } {code} was (Author: calcifer): You can already do this by using custom processor that embeds the generated processor. For example, if you have a {{service Foo}} in your Thrift file, the generated code will have a {{Process}} method like this: {code:go} func (p *FooProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { name, _, seqId, err := iprot.ReadMessageBegin() if err != nil { return false, err } if processor, ok := p.GetProcessorFunction(name); ok { return processor.Process(ctx, seqId, iprot, oprot) } iprot.Skip(thrift.STRUCT) iprot.ReadMessageEnd() x247 := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name) oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId) x247.Write(oprot) oprot.WriteMessageEnd() oprot.Flush() return false, x247 } {code} What you can do is create another processor, embed {{FooProcessor}} into it and override {{Process}}, like this: {code:go} type MyProcessor struct { *FooProcessor } func (p *MyProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) { name, _, seqId, err := iprot.ReadMessageBegin() if err != nil { return false, err } if processor, ok := p.GetProcessorFunction(name); ok { // HERE: Do anything with "name" before calling processor.Process() return processor.Process(ctx, seqId, iprot, oprot) } iprot.Skip(thrift.STRUCT) iprot.ReadMessageEnd() x247 := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name) oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId) x247.Write(oprot) oprot.WriteMessageEnd() oprot.Flush() return false, x247 } {code} > are there any plans for add middle-ware support for thrift Go implementation? > ----------------------------------------------------------------------------- > > Key: THRIFT-4553 > URL: https://issues.apache.org/jira/browse/THRIFT-4553 > Project: Thrift > Issue Type: New Feature > Affects Versions: 0.11.0 > Reporter: gansteed > Priority: Major > > version1: > provide a function to register middleware, and chain processors all. each > function can call `Next` to skip to next function. something like middle-ware > implementation in gin[1]. > version2: > provide two function in TSimpleServer, one for preProcess, one for > afterProcess. and call them > before and after `ok, err := processor.Process(defaultCtx, inputProtocol, > outputProtocol)`, so they can use defaultCtx to do something. > > middle-ware is useful for something like monitoring micro service, count > execution time...etc. > > [1]: https://github.com/gin-gonic/gin/blob/master/context.go#L104 -- This message was sent by Atlassian JIRA (v7.6.3#76005)