我正在尝试从 happywhale 获取遭遇记录并将其保存在我的本地数据库中,代码如下所示:
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;
$$
但是,循环发送请求总是会引发超时错误,并且无法通过更改 CURLOPT_TIMEOUT 来解决
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
我尝试使用设置curl选项CURLOPT_TIMEOUT和CURLOPT_TCP_KEEPALIVE
perform http_set_curlopt('CURLOPT_TIMEOUT', '30');
perform http_set_curlopt('CURLOPT_TCP_KEEPALIVE', '30');
我相信我通过设置这些选项解决了这个问题
perform http_set_curlopt('CURLOPT_TIMEOUT', '10000');
perform http_set_curlopt('CURLOPT_CONNECTTIMEOUT', '10000');