commit 56c906f4a0d8ddbc3dd5cab08e3ea548384bbd10 Author: iwakeh iwakeh@torproject.org Date: Mon Mar 19 10:58:38 2018 +0000
Use timeout of one minute for fetching index.
This is set for connect as well as read and can be overridden by system properties sun.net.client.default<Connect|Read>Timeout.
Implements task-24290. --- .../java/org/torproject/descriptor/index/IndexNode.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/torproject/descriptor/index/IndexNode.java b/src/main/java/org/torproject/descriptor/index/IndexNode.java index 4c4c884..19a5aa4 100644 --- a/src/main/java/org/torproject/descriptor/index/IndexNode.java +++ b/src/main/java/org/torproject/descriptor/index/IndexNode.java @@ -19,6 +19,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.net.URL; +import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Path; import java.util.SortedMap; @@ -37,6 +38,12 @@ public class IndexNode {
private static Logger log = LoggerFactory.getLogger(IndexNode.class);
+ private static final int READ_TIMEOUT = Integer.parseInt(System + .getProperty("sun.net.client.defaultReadTimeout", "60000")); + + private static final int CONNECT_TIMEOUT = Integer.parseInt(System + .getProperty("sun.net.client.defaultConnectTimeout", "60000")); + /** An empty node, which is not added to JSON output. */ public static final IndexNode emptyNode = new IndexNode("", "", new TreeSet<FileNode>(), new TreeSet<DirectoryNode>()); @@ -97,8 +104,12 @@ public class IndexNode { public static IndexNode fetchIndex(String urlString) throws Exception { String ending = urlString.substring(urlString.lastIndexOf(".") + 1).toUpperCase(); + URLConnection connection = (new URL(urlString)).openConnection(); + connection.setReadTimeout(READ_TIMEOUT); + connection.setConnectTimeout(CONNECT_TIMEOUT); + connection.connect(); try (InputStream is = FileType.valueOf(ending) - .inputStream(new URL(urlString).openStream())) { + .inputStream(connection.getInputStream())) { return fetchIndex(is); } }
tor-commits@lists.torproject.org