pramsey/pgsql-http

Setting CURLOPT_TIMEOUT does not have any effect

Closed this issue · 1 comments

I'm trying to get encounter records from happywhale and save them in my local db with this code shown below

do
$$
declare
    fromTime timestamp := '2000-01-01'::timestamp;
	toTime timestamp := 'now'::timestamp;
	deltaTime interval := '1 day'::interval;
	
	currTime timestamp;
	response record;
begin
    perform http_set_curlopt('CURLOPT_TIMEOUT', '30');
    perform http_set_curlopt('CURLOPT_TCP_KEEPALIVE', '30');
    create table if not exists encounters(id integer primary key, region text, species text, startdate timestamp, mincount integer, maxcount integer, geom geometry(point, 4326));
    currTime := fromTime;
    while currTime <= toTime loop
	     select * into response from http((
			  'POST'
			 ,'https://critterspot.happywhale.com/v1/cs/admin/encounter/search'
			 ,array[http_header('Connection', 'keep-alive'), http_header('Host', 'critterspot.happywhale.com')]
			 ,'application/json'
	         ,jsonb_build_object('showConnections', false, 'encounter', jsonb_build_object('datesearch', jsonb_build_object('startdate', to_char(currTime, 'YYYY-MM-DD'), 'type', 0)))
		 )::http_request);
	     if response.status <> 200 then
	         raise notice '[%] %', currTime, (response.content::json)->>'message';
	     else
		     insert into encounters select
			      (e->>'id')::int
				 ,e->>'region'
				 ,e->>'species'
				 ,((e#>>'{dateRange,startDate}')::date + (e#>>'{dateRange,startTime}')::time)
				 ,(e->>'minCount')::int minCount
				 ,(e->>'maxCount')::int maxCount
				 ,ST_Point((e#>>'{location,lng}')::float, (e#>>'{location,lat}')::float, 4326) geom
			 from (select json_array_elements(response.content::json) e) on conflict (id) do nothing;
			 
	         raise notice '[%] %', currTime, response.status;
	         currTime := currTime + deltaTime;
	     end if;
	end loop;
end;
$$

However I get this error message after few request later

NOTICE:  [2000-01-01 00:00:00] 200
NOTICE:  [2000-01-02 00:00:00] 200
NOTICE:  [2000-01-03 00:00:00] 200
NOTICE:  [2000-01-04 00:00:00] 200
NOTICE:  [2000-01-05 00:00:00] 200
NOTICE:  [2000-01-06 00:00:00] 200
NOTICE:  [2000-01-07 00:00:00] 200
NOTICE:  [2000-01-08 00:00:00] 200
NOTICE:  [2000-01-09 00:00:00] 200
NOTICE:  [2000-01-10 00:00:00] 200
NOTICE:  [2000-01-11 00:00:00] 200
NOTICE:  [2000-01-12 00:00:00] 200
NOTICE:  [2000-01-13 00:00:00] 200
NOTICE:  [2000-01-14 00:00:00] 200
ERROR:  Failed to connect to critterspot.happywhale.com port 443 after 1001 ms: Timeout was reached
CONTEXT:  SQL statement "select *               from http((
			  'POST'
			 ,'https://critterspot.happywhale.com/v1/cs/admin/encounter/search'
			 ,array[http_header('Connection', 'keep-alive'), http_header('Host', 'critterspot.happywhale.com')]
			 ,'application/json'
	         ,jsonb_build_object('showConnections', false, 'encounter', jsonb_build_object('datesearch', jsonb_build_object('startdate', to_char(currTime, 'YYYY-MM-DD'), 'type', 0)))
		 )::http_request)"
PL/pgSQL function inline_code_block line 15 at SQL statement 

This timeout error always happens, and I cannot solve it by changing CURLOPT_TIMEOUT option.

I believe I solved this issue by setting these options

perform http_set_curlopt('CURLOPT_TIMEOUT', '10000');
perform http_set_curlopt('CURLOPT_CONNECTTIMEOUT', '10000');