question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can you consider let com.alipay.sofa.rpc.message.ResponseFuture inherit java.util.concurrent.CompletionStage?

See original GitHub issue

现有的sofarpc的异步使用方式有回调和Future两种,都有比较大的局限性;

  • 回调
    • service级别的回调意义不大,功能和Filter有所重合;
    • 调用级别的回调处理起来比较繁琐,写起来也不直观;
  • Future
    • 目前的Future使用时依然得阻塞当前线程等待结果;
    • 无法兼容reactor这样的框架;

java1.8引入了java.util.concurrent.CompletableFuture这个强大的异步编程辅助类,目前sofarpc的使用者只能通过这样的代码勉强做到兼容:


    public static void main(String... args) {


        // omit
        // .setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
        CompletableFuture<?> future = new CompletableFuture<>();
        RpcInvokeContext.getContext().setResponseCallback(new CallbackImpl(future));
        RemoteService.invoke();
        future.thenApply(result -> {
            // omit
        })
    }

    static class CallbackImpl <T> implements SofaResponseCallback <T> {

        private final CompletableFuture<T> future;

        public CallbackImpl(CompletableFuture<T> future) {
            this.future = future;
        }

        @Override
        @SuppressWarnings("unchecked")
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            // BTW  why the appResponse type is Object rather than T?
            future.complete((T)appResponse);
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
            future.completeExceptionally(throwable);
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            future.completeExceptionally(sofaException);
        }
    }

是否可以考虑让com.alipay.sofa.rpc.message.ResponseFuture继承java.util.concurrent.CompletionStage, 来提供更好的异步编程支持?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
nanfeng1999commented, Nov 29, 2022

您好,我想认领这个任务

1reaction
EvenLjjcommented, Jun 3, 2021

@guyeu 非常好的建议,不知道你对于这块是不是感兴趣,随时欢迎提PR一起共建,当然我们也会一起看下。


@guyeu Good suggestion, I don’t know if you are interested in this, we advocate providing PR to join us any time.Of course we will look at it together.

Read more comments on GitHub >

github_iconTop Results From Across the Web

com.alipay.sofa.rpc.message.AbstractResponseFuture ... - Tabnine
@Override public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { long realTimeOut = unit.
Read more >
Extension mechanism - SOFAStack
The following sections introduce how to extend through the SPI interaction method. SOFARPC provides the capabilities of ExtensionLoader. Design ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found