@@ -18,13 +18,15 @@ public class IPFS {
1818 public enum PinType {all , direct , indirect , recursive }
1919 public List <String > ObjectTemplates = Arrays .asList ("unixfs-dir" );
2020 public List <String > ObjectPatchTypes = Arrays .asList ("add-link" , "rm-link" , "set-data" , "append-data" );
21- private static final int DEFAULT_TIMEOUT = 0 ;
21+ private static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 10_000 ;
22+ private static final int DEFAULT_READ_TIMEOUT_MILLIS = 60_000 ;
2223
2324 public final String host ;
2425 public final int port ;
2526 public final String protocol ;
2627 private final String version ;
27- private int timeout = DEFAULT_TIMEOUT ;
28+ private final int connectTimeoutMillis ;
29+ private final int readTimeoutMillis ;
2830 public final Key key = new Key ();
2931 public final Pin pin = new Pin ();
3032 public final Repo repo = new Repo ();
@@ -56,10 +58,18 @@ public IPFS(MultiAddress addr) {
5658 }
5759
5860 public IPFS (String host , int port , String version , boolean ssl ) {
61+ this (host , port , version , DEFAULT_CONNECT_TIMEOUT_MILLIS , DEFAULT_READ_TIMEOUT_MILLIS , ssl );
62+ }
63+
64+ public IPFS (String host , int port , String version , int connectTimeoutMillis , int readTimeoutMillis , boolean ssl ) {
65+ if (connectTimeoutMillis < 0 ) throw new IllegalArgumentException ("connect timeout must be zero or positive" );
66+ if (readTimeoutMillis < 0 ) throw new IllegalArgumentException ("read timeout must be zero or positive" );
5967 this .host = host ;
6068 this .port = port ;
69+ this .connectTimeoutMillis = connectTimeoutMillis ;
70+ this .readTimeoutMillis = readTimeoutMillis ;
6171
62- if (ssl ) {
72+ if (ssl ) {
6373 this .protocol = "https" ;
6474 } else {
6575 this .protocol = "http" ;
@@ -82,9 +92,7 @@ public IPFS(String host, int port, String version, boolean ssl) {
8292 * @return current IPFS object with configured timeout
8393 */
8494 public IPFS timeout (int timeout ) {
85- if (timeout < 0 ) throw new IllegalArgumentException ("timeout must be zero or positive" );
86- this .timeout = timeout ;
87- return this ;
95+ return new IPFS (host , port , version , connectTimeoutMillis , readTimeoutMillis , protocol .equals ("https" ));
8896 }
8997
9098 public List <MerkleNode > add (NamedStreamable file ) throws IOException {
@@ -676,11 +684,11 @@ private void retrieveAndParseStream(String path, Consumer<Object> results, Consu
676684
677685 private byte [] retrieve (String path ) throws IOException {
678686 URL target = new URL (protocol , host , port , version + path );
679- return IPFS .get (target , timeout );
687+ return IPFS .get (target , connectTimeoutMillis , readTimeoutMillis );
680688 }
681689
682- private static byte [] get (URL target , int timeout ) throws IOException {
683- HttpURLConnection conn = configureConnection (target , "POST" , timeout );
690+ private static byte [] get (URL target , int connectTimeoutMillis , int readTimeoutMillis ) throws IOException {
691+ HttpURLConnection conn = configureConnection (target , "POST" , connectTimeoutMillis , readTimeoutMillis );
684692 conn .setDoOutput (true );
685693 /* See IPFS commit for why this is a POST and not a GET https://github.com/ipfs/go-ipfs/pull/7097
686694 This commit upgrades go-ipfs-cmds and configures the commands HTTP API Handler
@@ -701,8 +709,6 @@ HTTP endpoint (usually :5001). Applications integrating on top of the
701709 */
702710 conn .setRequestMethod ("POST" );
703711 conn .setRequestProperty ("Content-Type" , "application/json" );
704- conn .setConnectTimeout (10_000 );
705- conn .setReadTimeout (60_000 );
706712
707713 try {
708714 OutputStream out = conn .getOutputStream ();
@@ -767,21 +773,21 @@ private List<Object> getAndParseStream(String path) throws IOException {
767773
768774 private InputStream retrieveStream (String path ) throws IOException {
769775 URL target = new URL (protocol , host , port , version + path );
770- return IPFS .getStream (target , timeout );
776+ return IPFS .getStream (target , connectTimeoutMillis , readTimeoutMillis );
771777 }
772778
773- private static InputStream getStream (URL target , int timeout ) throws IOException {
774- HttpURLConnection conn = configureConnection (target , "POST" , timeout );
779+ private static InputStream getStream (URL target , int connectTimeoutMillis , int readTimeoutMillis ) throws IOException {
780+ HttpURLConnection conn = configureConnection (target , "POST" , connectTimeoutMillis , readTimeoutMillis );
775781 return conn .getInputStream ();
776782 }
777783
778784 private Map postMap (String path , byte [] body , Map <String , String > headers ) throws IOException {
779785 URL target = new URL (protocol , host , port , version + path );
780- return (Map ) JSONParser .parse (new String (post (target , body , headers , timeout )));
786+ return (Map ) JSONParser .parse (new String (post (target , body , headers , connectTimeoutMillis , readTimeoutMillis )));
781787 }
782788
783- private static byte [] post (URL target , byte [] body , Map <String , String > headers , int timeout ) throws IOException {
784- HttpURLConnection conn = configureConnection (target , "POST" , timeout );
789+ private static byte [] post (URL target , byte [] body , Map <String , String > headers , int connectTimeoutMillis , int readTimeoutMillis ) throws IOException {
790+ HttpURLConnection conn = configureConnection (target , "POST" , connectTimeoutMillis , readTimeoutMillis );
785791 for (String key : headers .keySet ())
786792 conn .setRequestProperty (key , headers .get (key ));
787793 conn .setDoOutput (true );
@@ -812,11 +818,12 @@ private static boolean detectSSL(MultiAddress multiaddress) {
812818 return multiaddress .toString ().contains ("/https" );
813819 }
814820
815- private static HttpURLConnection configureConnection (URL target , String method , int timeout ) throws IOException {
821+ private static HttpURLConnection configureConnection (URL target , String method , int connectTimeoutMillis , int readTimeoutMillis ) throws IOException {
816822 HttpURLConnection conn = (HttpURLConnection ) target .openConnection ();
817823 conn .setRequestMethod (method );
818824 conn .setRequestProperty ("Content-Type" , "application/json" );
819- conn .setReadTimeout (timeout );
825+ conn .setConnectTimeout (connectTimeoutMillis );
826+ conn .setReadTimeout (readTimeoutMillis );
820827 return conn ;
821828 }
822829}
0 commit comments