Fast-Android-Networking icon indicating copy to clipboard operation
Fast-Android-Networking copied to clipboard

Throttling the server

Open satputekuldip opened this issue 4 years ago • 6 comments

First of all Thank you very much for such a great libraries and MVP architecture.

I have RecyclerView which has posts of my blog. I'm counting my views in onBind method of adapter by calling API everything is working fine. But when I scroll too fast then lots API calls are fired so my Laravel server sends me response of throttling exception (*ofcourse for preventing ddos) so How do I prevent it by using your library to make queue of this API call to run smoothly without overloading my server... I know many people have face this problem but increasing throttling capacity is not a solution to this issue...

In Presenter

getCompositeDisposable().add(getDataManager()
                .postImageViewApiCall(new ImageViewRequest(mediaID, null, getDataManager().getCurrentUserId(), deviceId))
                .subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())
                .subscribe(mediasResponse -> { AppLogger.d("VIEW_IMG",mediasResponse); },
                        throwable -> {
                            if (!isViewAttached()) return;

                            if (throwable instanceof ANError){
                                ANError anError = (ANError) throwable;
                                AppLogger.d("LIKE_IMG",anError.getErrorBody());
                            }
                            getMvpView().onError(R.string.some_error);
                            getMvpView().stopLoading();
                        }));

In API Data Manager

@Override
    public Observable<SingleImageViewResponse> postImageViewApiCall(ImageLikeRequest request) {
        return Rx2AndroidNetworking.post(ApiEndPoint.ENDPOINT_IMAGE_VIEWS)
                .addHeaders(mApiHeader.getProtectedApiHeader())
                .addBodyParameter(request)
                .build()
                .getObjectObservable(SingleImageViewResponse.class);
    }

satputekuldip avatar Apr 12 '20 17:04 satputekuldip

I think, here you can use RxJava operators like throttle and handle this.

amitshekhariitbhu avatar Apr 13 '20 01:04 amitshekhariitbhu

I've checked for that already but there no method in Rx2AndroidNetworking class to use perform this... Is there another way to do this Or am I missing something here?

satputekuldip avatar Apr 13 '20 05:04 satputekuldip

As the library suppport RxJava, you can apply any operators from the RxJava. Check official RxJava website for operators.

amitshekhariitbhu avatar Apr 13 '20 05:04 amitshekhariitbhu

Well I did some G-Search and found this

    @Override
    public Flowable<SingleImageLikeResponse> postImageViewApiCall(ImageLikeRequest request) {
        return Rx2AndroidNetworking.post(ApiEndPoint.ENDPOINT_IMAGE_VIEWS)
                .addHeaders(mApiHeader.getProtectedApiHeader())
                .addBodyParameter(request)
                .build()
                .getObjectObservable(SingleImageViewResponse.class)
                .toFlowable(BackpressureStrategy.BUFFER)
                .onBackpressureBuffer(1000)
                .delay(3, TimeUnit.SECONDS);
    }

but still its firing lots of calls... I don't understand what to do...

satputekuldip avatar Apr 13 '20 13:04 satputekuldip

Do you really want to delay the request for 3 seconds? I will not recommend doing that. One way is to write the logic at the client's end that if the request is in progress, do not make a request.

amitshekhariitbhu avatar Apr 13 '20 13:04 amitshekhariitbhu

3 second was testing number... It's not working so I was increasing time just for checking... Don't know why its not working... Still there is question...

But I did this at client side by delay in between two items view time... 1500 milliseconds... So I stopped calling each n every item when initiated...

satputekuldip avatar Apr 13 '20 15:04 satputekuldip