From f243ce66e77d2a2eeee08d160b9d764c2d1084d5 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Fri, 5 Oct 2018 01:58:41 +0900 Subject: [PATCH] =?UTF-8?q?ActivityPub=E3=81=AEHTTP=E3=83=AA=E3=82=AF?= =?UTF-8?q?=E3=82=A8=E3=82=B9=E3=83=88=E3=81=AE=E5=BC=B7=E5=8C=96=20(#2820?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix error handling in AP deliver * Set timeout to resolver * Tune looks --- src/queue/processors/http/deliver.ts | 23 +++++++++++------------ src/remote/activitypub/request.ts | 12 +++++++++++- src/remote/activitypub/resolver.ts | 2 ++ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/queue/processors/http/deliver.ts b/src/queue/processors/http/deliver.ts index e14a162105..621219fec6 100644 --- a/src/queue/processors/http/deliver.ts +++ b/src/queue/processors/http/deliver.ts @@ -7,19 +7,18 @@ export default async (job: bq.Job, done: any): Promise => { await request(job.data.user, job.data.to, job.data.content); done(); } catch (res) { - if (res == null || !res.hasOwnProperty('statusCode')) { - console.warn(`deliver failed (unknown): ${res}`); - return done(); - } - - if (res.statusCode == null) return done(); - if (res.statusCode >= 400 && res.statusCode < 500) { - // HTTPステータスコード4xxはクライアントエラーであり、それはつまり - // 何回再送しても成功することはないということなのでエラーにはしないでおく - done(); + if (res != null && res.hasOwnProperty('statusCode')) { + if (res.statusCode >= 400 && res.statusCode < 500) { + // HTTPステータスコード4xxはクライアントエラーであり、それはつまり + // 何回再送しても成功することはないということなのでエラーにはしないでおく + done(); + } else { + console.warn(`deliver failed: ${res.statusCode} ${res.statusMessage} to=${job.data.to}`); + done(res.statusMessage); + } } else { - console.warn(`deliver failed: ${res.statusMessage}`); - done(res.statusMessage); + console.warn(`deliver failed: ${res} to=${job.data.to}`); + done(); } } }; diff --git a/src/remote/activitypub/request.ts b/src/remote/activitypub/request.ts index 07f0ecca8b..177b6f458e 100644 --- a/src/remote/activitypub/request.ts +++ b/src/remote/activitypub/request.ts @@ -12,6 +12,8 @@ const log = debug('misskey:activitypub:deliver'); export default (user: ILocalUser, url: string, object: any) => new Promise((resolve, reject) => { log(`--> ${url}`); + const timeout = 10 * 1000; + const { protocol, hostname, port, pathname, search } = new URL(url); const data = JSON.stringify(object); @@ -26,6 +28,7 @@ export default (user: ILocalUser, url: string, object: any) => new Promise((reso port, method: 'POST', path: pathname + search, + timeout, headers: { 'User-Agent': config.user_agent, 'Content-Type': 'application/activity+json', @@ -35,7 +38,7 @@ export default (user: ILocalUser, url: string, object: any) => new Promise((reso log(`${url} --> ${res.statusCode}`); if (res.statusCode >= 400) { - reject(); + reject(res); } else { resolve(); } @@ -53,5 +56,12 @@ export default (user: ILocalUser, url: string, object: any) => new Promise((reso sig = sig.replace(/^Signature /, ''); req.setHeader('Signature', sig); + req.on('timeout', () => req.abort()); + + req.on('error', e => { + if (req.aborted) reject('timeout'); + reject(e); + }); + req.end(data); }); diff --git a/src/remote/activitypub/resolver.ts b/src/remote/activitypub/resolver.ts index 215e5e8704..ff26971758 100644 --- a/src/remote/activitypub/resolver.ts +++ b/src/remote/activitypub/resolver.ts @@ -7,6 +7,7 @@ const log = debug('misskey:activitypub:resolver'); export default class Resolver { private history: Set; + private timeout = 10 * 1000; constructor() { this.history = new Set(); @@ -50,6 +51,7 @@ export default class Resolver { const object = await request({ url: value, + timeout: this.timeout, headers: { 'User-Agent': config.user_agent, Accept: 'application/activity+json, application/ld+json'