-
Notifications
You must be signed in to change notification settings - Fork 235
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
Add a connection timeout to the socket #76
Conversation
When using invalid URL, without an explicit timeout, the connection would fail only after 60s, which is too long. Let the nativeOpen() method set a timeout and if it doesn't, use a default timeout of 10s.
/* Timeout value in seconds */ | ||
private int timeout = TIMEOUT; | ||
/* Sets the timeout variable */ | ||
public void setTimeout(int timeout) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'm guessing that this method should probably go below the static exception class (e.g. just above the open method).
Please also add proper javadoc (e.g. /** / rather than / */, and @param documenting the argument - including units).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
private long rtmpPointer = 0; | ||
|
||
/* Timeout value in seconds */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems strange that this is in whole seconds. Normally I'd expect to specify a timeout in milliseconds. Is this possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We initially considered setting the timeout in milliseconds. The only conflict we saw was that there is already a timeout in the rtmp library but it was being used to set only the socket's receive timeout. So we favoured using the same.
Do you think it would be better to use a different value for the send timeout (in ms), which can be set from the application and continue using the earlier timeout for receive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only conflict we saw was that there is already a timeout in the rtmp library but it was being used to set only the socket's receive timeout. So we favoured using the same.
I'm confused about how this is relevant to the unit being used. Please clarify. Is it that seconds is used somewhere elsewhere (if so, where abouts)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a rtmp->Link.timeout variable which holds the timeout in seconds. It is being used here - https://github.com/ittiam-systems/LibRtmp-Client-for-Android/blob/bug-67/rtmp-client/src/main/cpp/librtmp/rtmp.c#L950.
I could add another timeoutInMs variable to the rtmp structure and keep the receive timeout and send timeout different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- If it's possible to set them separately, then I think they should be separate and both settable via the Java API
- All Java timeouts should be in the same unit. Ideally milliseconds, since this is more standard for a Java API. However if there's already a Java API that takes seconds that we need to remain consistent with (I couldn't see one?), or if a value passed is going to be rounded to seconds internally (i.e. a millisecond value would lose precision internally) then it's indeed better to use seconds. Either way, let's make sure the unit is well documented on the Java APIs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two APIs now, one to set the send timeout (in milliseconds) and another to set the receive timeout (in seconds).
The javadoc mention the units for both the timeouts and even the variable names used indicate the units.
I have kept the receive timeout unit in seconds since the variable that is used, rtmp->Link.timeout is also set using another C API called RTMP_SetupStream().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have kept the receive timeout unit in seconds since the variable that is used, rtmp->Link.timeout is also set using another C API called RTMP_SetupStream().
Can we not just change the type of the variable, and the implementation of RTMP_SetupStream()
, and then use milliseconds everywhere? I would really rather avoid mixing units for timeouts if at all possible, since doing so seems quite non-ideal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Changed to milliseconds everywhere.
private int timeout = TIMEOUT; | ||
/* Sets the timeout variable */ | ||
public void setTimeout(int timeout) { | ||
if (timeout > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's considered an error to pass a non-positive values, then this should IMO throw IllegalArgumentException rather than silently doing nothing, if such a value is passed.
Alternatively, if it's possible for passing a non-positive value to return the client to whatever its default is, rather than doing nothing, then that would also be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zero or negative values will now reset the timeout to default values.
@@ -50,7 +60,7 @@ public RtmpIOException(int errorCode) { | |||
|
|||
public void open(String url, boolean isPublishMode) throws RtmpIOException { | |||
rtmpPointer = nativeAlloc(); | |||
int result = nativeOpen(url, isPublishMode, rtmpPointer); | |||
int result = nativeOpen(url, isPublishMode, rtmpPointer, timeout); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally these timeout variables will have the unit in the variable name, so it's clear as its passed around what unit it's in. If we can do milliseconds then timeoutMs would be a good name. Ditto elsewhere in this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
I've just make a quick review. Just let me know when it's ok to merge @ojw28 @vavadhani |
Send timeout can be configured in milliseconds and receive timeout can be set in seconds.
See inline comment:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks better, thanks!
@@ -338,8 +338,8 @@ RTMP_Init(RTMP *r) | |||
r->m_nServerBW = 2500000; | |||
r->m_fAudioCodecs = 3191.0; | |||
r->m_fVideoCodecs = 252.0; | |||
//making timeout value to 10 from 30 | |||
r->Link.timeout = 10; | |||
//Changing units to milliseconds. Changing from 10 to 10000. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this line (it's not a comment that makes sense when someone looks at the file - it only makes sense when they look at this change diff specifically)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -176,7 +176,8 @@ extern "C" | |||
int swfAge; | |||
|
|||
int protocol; | |||
int timeout; /* connection timeout in seconds */ | |||
int timeout; /* connection timeout in milliseconds */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be renamed to receiveTimeoutInMs
, for consistency?
Note that you can remove the comment on this line and the one below if so, because the names are then clear enough to be self documenting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
* Parameter expects a non-zero positive integer and will reset timeout to the default value | ||
* (10000 ms) if zero or a negative integer is passed. | ||
* */ | ||
public void setSendTimeoutInMs(int sendTimeoutInMs) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can we call these methods setSendTimeout
and setReceiveTimeout
? The names of the arguments are sufficient to make the unit clear, so there's no need to also document this in the method name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Changing timeout in Link structure to receiveTimeoutInMs. Remove InMs suffix from Java API that set the timeout values.
Looks good! @mekya - I think this can be merged |
When using invalid URL, without an explicit timeout, the connection
would fail only after 60s, which is too long. Let the nativeOpen()
method set a timeout and if it doesn't, use a default timeout of 10s.
Allows this timeout value to be set from the application.
#67 and google/ExoPlayer#4249 will be solved with this commit.