Skip to content

Commit 0c28eef

Browse files
author
Enric Sala
committed
Allow to configure the read and write request timeouts
1 parent ccf8f91 commit 0c28eef

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ dbs = influxdb.databases()
4545
influxdb.use('weather_stations');
4646
```
4747

48+
If you plan on doing very large requests you may need to adjust the timeouts:
49+
50+
```matlab
51+
% Configure timeouts
52+
influxdb.setReadTimeout(10);
53+
influxdb.setWriteTimeout(10);
54+
```
55+
4856

4957
Writing data
5058
------------

influxdb-client/InfluxDB.m

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
classdef InfluxDB < handle
22

33
properties(Access = private)
4-
Host, User, Password, Database;
4+
Host = ''
5+
User = ''
6+
Password = ''
7+
Database = ''
8+
ReadTimeout = 10
9+
WriteTimeout = 10
510
end
611

712
methods
@@ -13,6 +18,16 @@
1318
obj.Database = database;
1419
end
1520

21+
% Set the read timeout
22+
function obj = setReadTimeout(obj, timeout)
23+
obj.ReadTimeout = timeout;
24+
end
25+
26+
% Set the write timeout
27+
function obj = setWriteTimeout(obj, timeout)
28+
obj.WriteTimeout = timeout;
29+
end
30+
1631
% Test the connection with a ping
1732
function [ok, millis] = ping(obj)
1833
timer = tic;
@@ -25,7 +40,8 @@
2540
% Show databases
2641
function databases = databases(obj)
2742
url = [obj.Host '/query'];
28-
opts = weboptions('Username', obj.User, 'Password', obj.Password);
43+
opts = weboptions('Timeout', obj.ReadTimeout, ...
44+
'Username', obj.User, 'Password', obj.Password);
2945
response = webread(url, 'q', 'SHOW DATABASES', opts);
3046
databases = [response.results.series.values{:}];
3147
end
@@ -38,7 +54,8 @@
3854
% Execute a query string
3955
function result = runQuery(obj, query)
4056
url = [obj.Host '/query'];
41-
opts = weboptions('Username', obj.User, 'Password', obj.Password);
57+
opts = weboptions('Timeout', obj.ReadTimeout, ...
58+
'Username', obj.User, 'Password', obj.Password);
4259
response = webread(url, 'db', obj.Database, 'epoch', 'ms', 'q', query, opts);
4360
result = QueryResult.from(response);
4461
end
@@ -76,7 +93,8 @@
7693
params{end + 1} = ['consistency=' consistency];
7794
end
7895
url = [obj.Host '/write?' strjoin(params, '&')];
79-
opts = weboptions('Username', obj.User, 'Password', obj.Password);
96+
weboptions('Timeout', obj.WriteTimeout, ...
97+
'Username', obj.User, 'Password', obj.Password);
8098
webwrite(url, lines, opts);
8199
end
82100

0 commit comments

Comments
 (0)