Skip to content

OkHttp optimizations #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

pupitetris
Copy link
Contributor

This pull request allows the reuse of the okHttpClient object, as recommended by the library's documentation.

  • okHttpClient reuse allows for HTTP/2 connecton reuse and reduces
    overhead.

  • sets eager timeout specs to circumvent Roku issues regarding a lack
    of priority for the ECP web server and not handling requests promptly
    enough when the appliance is busy, such as with fast browsing of
    collections or search results or while gaming. A low timeout cancels
    requests that are not being timely handled, eluding "catch up"
    interactions that only generate out-of-sequence inputs.

  • sets up a connection pool with a 1 sec keepalive so HTTP/2
    connections can be reused and to try to keep the ECP web server from
    sleeping.

  • just for a tiny bit of performance, a quick request object cache is
    put in place based on URL, for requests where lag is relevant (those
    being key press/up/down requests).

allows the reuse of the okHttpClient object, as recommended by the
library's documentation.

* okHttpClient reuse allows for HTTP/2 connecton reuse and reduces
overhead.

* sets eager timeout specs to circumvent Roku issues regarding a lack
of priority for the ECP web server and not handling requests promptly
enough when the appliance is busy, such as with fast browsing of
collections or search results or while gaming. A low timeout cancels
requests that are not being timely handled, eluding "catch up"
interactions that only generate out-of-sequence inputs.

* sets up a connection pool with a 1 sec keepalive so HTTP/2
connections can be reused and to try to keep the ECP web server from
sleeping.

* just for a tiny bit of performance, a quick request object cache is
put in place based on URL, for requests where lag is relevant (those
being key press/up/down requests).
also, new private member useRequestCache specifies if the Request
should tell HttpManager to cache the okHttp.Request object.
Repository owner deleted a comment from wseemannroku Jan 3, 2025
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody

class HttpManager private constructor() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you mark this as internal class please? It shouldn't be exposed as an end user API.

Repository owner deleted a comment from wseemannroku Jan 3, 2025

class HttpManager private constructor() {

private val okHttpClient = OkHttpClient.Builder()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are extremely low connection and read times. I'd prefer at least two seconds. While these times may work on your internet connection other users may have less than ideal network conditions are setting the values this low would make this SDK unusable.

This allows for the ECPRequest to be reused at the application level
without having to rebuild the okHttp Request every time.

HttpManager: improved public methods granularity to expose the
okHttp Request.
@pupitetris
Copy link
Contributor Author

OK, both modifications are in.

Since ECP key requests don't change at the HTTP level, I also added that the okHTTP requests are kept in memory once they are built. This way, if the ECPRequest is not destroyed by the application code, the okHTTP request needs not be rebuilt when the request is sent again.

return null
val httpManager = HttpManager.getInstance()

if (http_request == null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the purpose of http_request here? Wouldn't this cause caching of every request regardless of the useRequestCache flag because once the value is set to true then:

http_request = httpManager.buildRequest(url, getMethod(), useRequestCache)

is never executed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants