ActivityPubのHTTPリクエストの強化 (#2820)
* Fix error handling in AP deliver * Set timeout to resolver * Tune looks
This commit is contained in:
		
							parent
							
								
									baf9b65801
								
							
						
					
					
						commit
						f243ce66e7
					
				
					 3 changed files with 24 additions and 13 deletions
				
			
		| 
						 | 
					@ -7,19 +7,18 @@ export default async (job: bq.Job, done: any): Promise<void> => {
 | 
				
			||||||
		await request(job.data.user, job.data.to, job.data.content);
 | 
							await request(job.data.user, job.data.to, job.data.content);
 | 
				
			||||||
		done();
 | 
							done();
 | 
				
			||||||
	} catch (res) {
 | 
						} catch (res) {
 | 
				
			||||||
		if (res == null || !res.hasOwnProperty('statusCode')) {
 | 
							if (res != null && res.hasOwnProperty('statusCode')) {
 | 
				
			||||||
			console.warn(`deliver failed (unknown): ${res}`);
 | 
								if (res.statusCode >= 400 && res.statusCode < 500) {
 | 
				
			||||||
			return done();
 | 
									// HTTPステータスコード4xxはクライアントエラーであり、それはつまり
 | 
				
			||||||
		}
 | 
									// 何回再送しても成功することはないということなのでエラーにはしないでおく
 | 
				
			||||||
 | 
									done();
 | 
				
			||||||
		if (res.statusCode == null) return done();
 | 
								} else {
 | 
				
			||||||
		if (res.statusCode >= 400 && res.statusCode < 500) {
 | 
									console.warn(`deliver failed: ${res.statusCode} ${res.statusMessage} to=${job.data.to}`);
 | 
				
			||||||
			// HTTPステータスコード4xxはクライアントエラーであり、それはつまり
 | 
									done(res.statusMessage);
 | 
				
			||||||
			// 何回再送しても成功することはないということなのでエラーにはしないでおく
 | 
								}
 | 
				
			||||||
			done();
 | 
					 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			console.warn(`deliver failed: ${res.statusMessage}`);
 | 
								console.warn(`deliver failed: ${res} to=${job.data.to}`);
 | 
				
			||||||
			done(res.statusMessage);
 | 
								done();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,6 +12,8 @@ const log = debug('misskey:activitypub:deliver');
 | 
				
			||||||
export default (user: ILocalUser, url: string, object: any) => new Promise((resolve, reject) => {
 | 
					export default (user: ILocalUser, url: string, object: any) => new Promise((resolve, reject) => {
 | 
				
			||||||
	log(`--> ${url}`);
 | 
						log(`--> ${url}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const timeout = 10 * 1000;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const { protocol, hostname, port, pathname, search } = new URL(url);
 | 
						const { protocol, hostname, port, pathname, search } = new URL(url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const data = JSON.stringify(object);
 | 
						const data = JSON.stringify(object);
 | 
				
			||||||
| 
						 | 
					@ -26,6 +28,7 @@ export default (user: ILocalUser, url: string, object: any) => new Promise((reso
 | 
				
			||||||
		port,
 | 
							port,
 | 
				
			||||||
		method: 'POST',
 | 
							method: 'POST',
 | 
				
			||||||
		path: pathname + search,
 | 
							path: pathname + search,
 | 
				
			||||||
 | 
							timeout,
 | 
				
			||||||
		headers: {
 | 
							headers: {
 | 
				
			||||||
			'User-Agent': config.user_agent,
 | 
								'User-Agent': config.user_agent,
 | 
				
			||||||
			'Content-Type': 'application/activity+json',
 | 
								'Content-Type': 'application/activity+json',
 | 
				
			||||||
| 
						 | 
					@ -35,7 +38,7 @@ export default (user: ILocalUser, url: string, object: any) => new Promise((reso
 | 
				
			||||||
		log(`${url} --> ${res.statusCode}`);
 | 
							log(`${url} --> ${res.statusCode}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (res.statusCode >= 400) {
 | 
							if (res.statusCode >= 400) {
 | 
				
			||||||
			reject();
 | 
								reject(res);
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			resolve();
 | 
								resolve();
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -53,5 +56,12 @@ export default (user: ILocalUser, url: string, object: any) => new Promise((reso
 | 
				
			||||||
	sig = sig.replace(/^Signature /, '');
 | 
						sig = sig.replace(/^Signature /, '');
 | 
				
			||||||
	req.setHeader('Signature', sig);
 | 
						req.setHeader('Signature', sig);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						req.on('timeout', () => req.abort());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						req.on('error', e => {
 | 
				
			||||||
 | 
							if (req.aborted) reject('timeout');
 | 
				
			||||||
 | 
							reject(e);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	req.end(data);
 | 
						req.end(data);
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,6 +7,7 @@ const log = debug('misskey:activitypub:resolver');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default class Resolver {
 | 
					export default class Resolver {
 | 
				
			||||||
	private history: Set<string>;
 | 
						private history: Set<string>;
 | 
				
			||||||
 | 
						private timeout = 10 * 1000;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	constructor() {
 | 
						constructor() {
 | 
				
			||||||
		this.history = new Set();
 | 
							this.history = new Set();
 | 
				
			||||||
| 
						 | 
					@ -50,6 +51,7 @@ export default class Resolver {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		const object = await request({
 | 
							const object = await request({
 | 
				
			||||||
			url: value,
 | 
								url: value,
 | 
				
			||||||
 | 
								timeout: this.timeout,
 | 
				
			||||||
			headers: {
 | 
								headers: {
 | 
				
			||||||
				'User-Agent': config.user_agent,
 | 
									'User-Agent': config.user_agent,
 | 
				
			||||||
				Accept: 'application/activity+json, application/ld+json'
 | 
									Accept: 'application/activity+json, application/ld+json'
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue