-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Enable jib.httpTimeout system property for HTTP connection and read timeout #656
Changes from 4 commits
2f9bafd
ba9b44f
4654fac
0af2594
b780031
5643bf3
8ffcb97
4c98316
42fc992
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,15 @@ public static Initializer initializer(String serverUrl, String repository) { | |
return new Initializer(serverUrl, repository); | ||
} | ||
|
||
@Nullable | ||
private static Integer getHttpTimeout() { | ||
Integer httpTimeout = Integer.getInteger("jib.httpTimeout"); | ||
if (httpTimeout != null && httpTimeout >= 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could do just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking showing a warning with a negative value, instead of errorring out. Do you think it's not good? Anyways, if we decide to error out, I'll probably detect it and fail at the frontends with better error messages, rather than just displaying up an unfriendly stack trace from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think either way is good. I lean towards the errorring since the case I could think of where negative could show up is if the user may have meant to type another timeout and accidentally put a minus sign in front - in which case, a warning may be lost in the logs to a different error when the connection fails. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. I think then the same argument applies to non-parsable string values. The user could accidentally put any non-digit characters, so it also makes sense to error out on non-parsable strings. Going for errorring on both cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, I think in the non-parsable version, this already errors, since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, errorring on both cases. (BTW, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah right, looks like the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will update the code tomorrow, but I'm thinking of doing Integer httpTimeout = null; // implies using the default timeout
String timeoutValue = System.getProperty("jib.httpTimeout");
if (timeoutValue != null) {
httpTimeout = Integer.parseInt(timeoutValue);
if (httpTimeout < 0) {
throw new RegistryAuthenticationFailedException("Negative value of jib.httpTimeout");
}
}
...
try (Connection connection = ...) {
Request.Builder requestBuilder = Request.builder().setHttpTimeout(httpTimeout);
...
} catch (NumberFormatException ex) {
throw new RegistryAuthenticationFailedException("Non-integer value of jib.httpTimeout");
} Since all versions of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coollog and I agreed that we make the code simple and not care about the edge cases too much here. If something goes awry, so be it. Instead, we will guard the errors at the plugin frontends. |
||
return httpTimeout; | ||
} | ||
return null; | ||
} | ||
|
||
// TODO: Replace with a WWW-Authenticate header parser. | ||
/** | ||
* Instantiates from parsing a {@code WWW-Authenticate} header. | ||
|
@@ -239,7 +248,7 @@ private Authorization authenticate(String scope) throws RegistryAuthenticationFa | |
URL authenticationUrl = getAuthenticationUrl(scope); | ||
|
||
try (Connection connection = new Connection(authenticationUrl)) { | ||
Request.Builder requestBuilder = Request.builder(); | ||
Request.Builder requestBuilder = Request.builder().setHttpTimeout(getHttpTimeout()); | ||
if (authorization != null) { | ||
requestBuilder.setAuthorization(authorization); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2018 Google LLC. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.cloud.tools.jib.http; | ||
|
||
import com.google.api.client.http.GenericUrl; | ||
import java.io.IOException; | ||
import java.util.function.BiFunction; | ||
|
||
/** | ||
* Mock {@link Connection} used for testing. Normally, you would use {@link | ||
* org.mockito.Mockito#mock}; this class is intended to examine the {@link Request) object by | ||
* calling its non-public package-protected methods. | ||
*/ | ||
public class MockConnection extends Connection { | ||
|
||
private BiFunction<String, Request, Response> responseSupplier; | ||
private Integer httpTimeout; | ||
|
||
public MockConnection(BiFunction<String, Request, Response> responseSupplier) { | ||
super(new GenericUrl("ftp://non-exisiting.example.url.ever").toURL()); | ||
this.responseSupplier = responseSupplier; | ||
} | ||
|
||
@Override | ||
public Response send(String httpMethod, Request request) throws IOException { | ||
httpTimeout = request.getHttpTimeout(); | ||
return responseSupplier.apply(httpMethod, request); | ||
} | ||
|
||
public Integer getRequestedHttpTimeout() { | ||
return httpTimeout; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2018 Google LLC. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.cloud.tools.jib.http; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** Tests for {@link Request}. */ | ||
public class RequestTest { | ||
|
||
@Test | ||
public void testGetHttpTimeout() { | ||
Request request = Request.builder().build(); | ||
|
||
Assert.assertNull(request.getHttpTimeout()); | ||
} | ||
|
||
@Test | ||
public void testSetHttpTimeout() { | ||
Request request = Request.builder().setHttpTimeout(3000).build(); | ||
|
||
Assert.assertEquals(Integer.valueOf(3000), request.getHttpTimeout()); | ||
} | ||
} |
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.
Is using
null
a common way to do this? If not, I was thinking maybe use negative to indicate absence?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 think it's not uncommon to use
null
to indicate a field is unset or uninitialized, or something is absent. Using a negative value works too. However, it won't be distinguishable from the user wrongly giving a negative value. That will matter if we error out on a negative timeout value as you said.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.
Okay sounds good 👍 let's add a javadoc explicitly mentioning that
null
means the caller should use some default.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.
Ah, you weren't talking about a field here. I see what you meant. I'll think about the API-aspect of this method.