Skip to content

Commit 77a58e4

Browse files
authored
RedisAI constructor with custom socket timeout (#45)
1 parent 67f61b2 commit 77a58e4

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ and
6161

6262
### Note
6363

64-
Since version `0.10.0`, the chunk size of model (blob) is set to 512mb (536870912 bytes) based on default Redis
65-
configuration. This behavior can be changed by `redisai.blob.chunkSize` system property at the beginning of the
66-
application. For example, chunk size can be limited to 8mb by setting `-Dredisai.blob.chunkSize=8388608` or
64+
**Chunk size:** Since version `0.10.0`, the chunk size of model (blob) is set to 512mb (536870912 bytes) based on
65+
default Redis configuration. This behavior can be changed by `redisai.blob.chunkSize` system property at the beginning
66+
of the application. For example, chunk size can be limited to 8mb by setting `-Dredisai.blob.chunkSize=8388608` or
6767
`System.setProperty(Model.BLOB_CHUNK_SIZE_PROPERTY, "8388608");`. A limit of 0 (zero) would disable chunking.
68+
69+
**Socket timeout:** Operations with large data and/or long processing time may require a higher socket timeout.
70+
Following constructor may come in handy for that purpose.
71+
```
72+
HostAndPort hostAndPort = new HostAndPort(host, port);
73+
JedisClientConfig clientConfig = DefaultJedisClientConfig.builder().socketTimeoutMillis(largeTimeout).build();
74+
new RedisAI(hostAndPort, clientConfig);
75+
```

src/main/java/com/redislabs/redisai/RedisAI.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
import java.util.HashMap;
88
import java.util.List;
99
import java.util.Map;
10+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
1011
import redis.clients.jedis.BinaryClient;
12+
import redis.clients.jedis.HostAndPort;
1113
import redis.clients.jedis.Jedis;
14+
import redis.clients.jedis.JedisClientConfig;
1215
import redis.clients.jedis.JedisPool;
1316
import redis.clients.jedis.JedisPoolConfig;
1417
import redis.clients.jedis.exceptions.JedisDataException;
@@ -55,6 +58,16 @@ public RedisAI(String host, int port, int timeout, int poolSize, String password
5558
this(new JedisPool(initPoolConfig(poolSize), host, port, timeout, password));
5659
}
5760

61+
/**
62+
* Create a new RedisAI client
63+
*
64+
* @param hostAndPort
65+
* @param clientConfig
66+
*/
67+
public RedisAI(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
68+
this(new JedisPool(new GenericObjectPoolConfig<>(), hostAndPort, clientConfig));
69+
}
70+
5871
/**
5972
* Create a new RedisAI client
6073
*

src/test/java/com/redislabs/redisai/RedisAITest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
import org.junit.Assert;
1111
import org.junit.Before;
1212
import org.junit.Test;
13+
import redis.clients.jedis.DefaultJedisClientConfig;
14+
import redis.clients.jedis.HostAndPort;
1315
import redis.clients.jedis.Jedis;
16+
import redis.clients.jedis.JedisClientConfig;
1417
import redis.clients.jedis.JedisPool;
1518

1619
public class RedisAITest {
@@ -58,6 +61,22 @@ public void testClientPoolSizeConstructorSetTensorFLOAT() {
5861
}
5962
}
6063

64+
@Test
65+
public void withoutDefaultConfig() {
66+
String host = "localhost";
67+
int port = 6379;
68+
int largeTimeout = 5000;
69+
70+
HostAndPort hostAndPort = new HostAndPort(host, port);
71+
JedisClientConfig clientConfig =
72+
DefaultJedisClientConfig.builder().socketTimeoutMillis(largeTimeout).build();
73+
74+
try (RedisAI redisAI = new RedisAI(hostAndPort, clientConfig)) {
75+
String script = "def bar(a, b): return a + b\n";
76+
Assert.assertTrue(redisAI.setScript("script", Device.CPU, script));
77+
}
78+
}
79+
6180
@Test
6281
public void testSetTensorNegative() {
6382
try {

0 commit comments

Comments
 (0)