okhttp-eventsource icon indicating copy to clipboard operation
okhttp-eventsource copied to clipboard

Latest version doesn't connect to the server properly [4.1.0]

Open rubenquadros12 opened this issue 2 years ago • 4 comments

Hi,

I'm using version 4.1.0 of the library.

I'm calling eventsource.start() method to connect to the server. I see that the library internally calls call.execute()

This call.execute() doesn't return anything and no code after this line gets executed. I have debugged the code, there are no exceptions.

I'm making both init and connect call in a IO Dispatcher.

The funny thing is if I attach HttpLoggingInterceptor I can see 200 for the api call.

I have tried by calling eventsource.readAnyEvent() as well which internally connects to the server first and it's the same result.

My code snippets:

This is init call:


private var source: EventSource? = null

val uri = URI.create(url)
val connectStrategy = HttpConnectStrategy.http(uri).httpClient(okHttpClient) //my custom client
val eventSourceBuilder = EventSource.Builder(connectStrategy)
      .errorStrategy(ErrorStrategy.alwaysThrow())
      .logger(LDLogger.withAdapter(rtcLogger, TAG)) //my custom logger
      .retryDelay(realTimeEventsConfig.reconnectTimeInSeconds, TimeUnit.SECONDS)
      .retryDelayStrategy(
                DefaultRetryDelayStrategy.defaultStrategy().maxDelay(
                    realTimeEventsConfig.maxReconnectTimeInSeconds, TimeUnit.SECONDS)
        )

source = eventSourceBuilder.build()

Connect call:

try {
     disconnect(shouldClear = false)
     source?.start()
} catch (e: Exception) {
      //log exception
 }

Can you please let me know what can be the issue?

Edit: If I dont use custom http client it works

rubenquadros12 avatar May 29 '23 13:05 rubenquadros12

Hi @rubenquadros12 , thank you for reporting this issue. We have filed it internally as 205140. I'm not sure when one of our engineers will get a chance to investigate, but we'll update this issue when we do. Thank you.

tanderson-ld avatar Jun 01 '23 14:06 tanderson-ld

Hey @tanderson-ld, thanks for responding. We have identified what caused the issue. Actually it was related to one of the interceptors used by the Okhttp client inside our app. Very similar to this issue, we noticed that one of the interceptors added in the Okhttp client, uses response.peekBody(Long.MAX_VALUE) , which does the following :

Peeks up to byteCount bytes from the response body and returns them as a new response body. If fewer than byteCount bytes are in the response body, the full response body is returned. If more than byteCount bytes are in the response body, the returned value will be truncated to byteCount bytes.

After removing this interceptor from the okhttp client, the connection is successful. Is this an intended behaviour of the library?

hiteshchopra11 avatar Jun 14 '23 19:06 hiteshchopra11

We don't intend to behave a certain way based on externally added interceptors, but perhaps it was leading to modification of the data our code was receiving? I haven't had a change to dig in deeper yet on how peek could interfere. It doesn't seem like it should.

tanderson-ld avatar Jun 26 '23 15:06 tanderson-ld

@hiteshchopra11 Would be very appreciated if you would kindly guide me on how to remove okhttp client interceptor?

henryngcw avatar Aug 21 '23 10:08 henryngcw