armeria icon indicating copy to clipboard operation
armeria copied to clipboard

Provide a way to decorate Thrift services via annotation

Open minwoox opened this issue 2 years ago • 1 comments

Thanks to @ta7uw, we can use decorator annotation to decorate gRPC services. #4041 It would be also very useful if we can use the annotation in Thrift services.

minwoox avatar Apr 26 '22 08:04 minwoox

@AmosDoan might be interested in this issue. 😆

ikhoon avatar Apr 26 '22 08:04 ikhoon

Let me leave some additional information for those interested.

The goal of this pull request is to add support for @Decorator annotation for thrift services.

Assume that we define a class called MyThriftClass in the following snippet. We may add decorators like the following to MyThriftClass.

    @Decorator(ClassDecorator.class)
    private static class MyThriftClass implements HelloService.AsyncIface {

        @Override
        @Decorator(MethodDecorator.class)
        public void hello(String name, AsyncMethodCallback<String> resultHandler) throws TException {
            resultHandler.onComplete(name);
        }
    };

where the decorators may be defined as follows:

    private static class ClassDecorator implements DecoratingHttpServiceFunction {
        @Override
        public HttpResponse serve(HttpService delegate, ServiceRequestContext ctx, HttpRequest req)
                throws Exception {
            return delegate.serve(ctx, req);
        }
    }

    private static class MethodDecorator implements DecoratingHttpServiceFunction {
        @Override
        public HttpResponse serve(HttpService delegate, ServiceRequestContext ctx, HttpRequest req)
                throws Exception {
            return delegate.serve(ctx, req);
        }
    }

For a little more background: Thrift was developed as a way to enable communication across different programming languages. Thrift also provides its own server, client implementation.

Armeria supports thrift by implementing its own server, client stack. In detail, the following decorators are applied in order.

Armeria server -> DecoratingHttpService -> THttpService -> DecoratingRpcService -> ThriftCallService

We can know what class/method will be used at THttpService since the thrift request needs to be parsed to know this information. For this reason, THttpService is probably the location where we want to

  1. Check what decorators should be applied for a request.
  2. Apply the decorators.
  3. Call the downstream RpcService.

We may also refer to #4041 for hints on how to extract and apply decorators depending on the route.

jrhee17 avatar Feb 20 '23 03:02 jrhee17