-
Notifications
You must be signed in to change notification settings - Fork 21
fix: add echo requester and fix timeouts #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import http from 'http'; | ||
import { shuffle } from '../utils/helpers'; | ||
import { Transporter } from '../utils/Transporter'; | ||
import { Headers, Host, Request, RequestOptions } from '../utils/types'; | ||
import { Requester } from '../utils/Requester'; | ||
|
||
import { BatchObject } from '../model/batchObject'; | ||
import { BatchResponse } from '../model/batchResponse'; | ||
|
@@ -32,7 +32,7 @@ export class SearchApi { | |
|
||
protected interceptors: Interceptor[] = []; | ||
|
||
constructor(appId: string, apiKey: string) { | ||
constructor(appId: string, apiKey: string, requester?: Requester) { | ||
this.setApiKey(SearchApiApiKeys.appId, appId); | ||
this.setApiKey(SearchApiApiKeys.apiKey, apiKey); | ||
this.transporter = new Transporter({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not in the scope of this PR, but just wanted to make sure everyone's aware of this; I would recommend making the hosts (or the whole transporter) something you can pass to the constructor as well. Once Metis is released, the client would need to work with different hosts than the one we set by default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we should prepare this codebase for Metis even if we don't have an ETA? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Does not cost anything, but just wondering) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's good to keep in mind for sure, otherwise we might have to rework a lot later on 🙂 Besides the hosts part, not a lot should change, though (perhaps the retry strategy but that is a problem for later 😄 ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point I'll include the host too |
||
|
@@ -57,6 +57,7 @@ export class SearchApi { | |
read: 5, | ||
write: 30, | ||
}, | ||
requester, | ||
}); | ||
} | ||
|
||
|
@@ -143,7 +144,7 @@ export class SearchApi { | |
|
||
await interceptorPromise; | ||
|
||
return this.transporter.retryableRequest(request, requestOptions); | ||
return this.transporter.request(request, requestOptions); | ||
} | ||
/** | ||
* | ||
|
@@ -208,7 +209,7 @@ export class SearchApi { | |
|
||
await interceptorPromise; | ||
|
||
return this.transporter.retryableRequest(request, requestOptions); | ||
return this.transporter.request(request, requestOptions); | ||
} | ||
/** | ||
* Add an object to the index, automatically assigning it an object ID | ||
|
@@ -285,7 +286,7 @@ export class SearchApi { | |
|
||
await interceptorPromise; | ||
|
||
return this.transporter.retryableRequest(request, requestOptions); | ||
return this.transporter.request(request, requestOptions); | ||
} | ||
/** | ||
* | ||
|
@@ -363,6 +364,6 @@ export class SearchApi { | |
|
||
await interceptorPromise; | ||
|
||
return this.transporter.retryableRequest(request, requestOptions); | ||
return this.transporter.request(request, requestOptions); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { EndRequest, Response } from './types'; | ||
import { Requester } from './Requester'; | ||
|
||
export class EchoRequester extends Requester { | ||
async send(request: EndRequest): Promise<Response> { | ||
return { | ||
content: JSON.stringify(request), | ||
isTimedOut: false, | ||
status: 200, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { EndRequest, Response } from './types'; | ||
import * as http from 'http'; | ||
import * as https from 'https'; | ||
import { Requester } from './Requester'; | ||
|
||
export class HttpRequester extends Requester { | ||
private httpAgent: http.Agent; | ||
private httpsAgent: https.Agent; | ||
|
||
constructor() { | ||
super(); | ||
this.httpAgent = new http.Agent({ keepAlive: true }); | ||
this.httpsAgent = new https.Agent({ keepAlive: true }); | ||
} | ||
|
||
async send(request: EndRequest): Promise<Response> { | ||
return new Promise((resolve) => { | ||
const url = new URL(request.url); | ||
|
||
const path = url.search === null ? url.pathname : `${url.pathname}?${url.search}`; | ||
|
||
const options: https.RequestOptions = { | ||
agent: url.protocol === 'https:' ? this.httpsAgent : this.httpAgent, | ||
hostname: url.hostname, | ||
path, | ||
method: request.method, | ||
headers: request.headers, | ||
...(url.port !== undefined ? { port: url.port || '' } : {}), | ||
}; | ||
|
||
const req = (url.protocol === 'https:' ? https : http).request(options, (response) => { | ||
let contentBuffers: Buffer[] = []; | ||
|
||
response.on('data', (chunk) => { | ||
contentBuffers = contentBuffers.concat(chunk); | ||
}); | ||
|
||
response.on('end', () => { | ||
clearTimeout(connectTimeout); | ||
clearTimeout(responseTimeout as NodeJS.Timeout); | ||
|
||
resolve({ | ||
status: response.statusCode || 0, | ||
content: Buffer.concat(contentBuffers).toString(), | ||
isTimedOut: false, | ||
}); | ||
}); | ||
}); | ||
|
||
const createTimeout = (timeout: number, content: string): NodeJS.Timeout => { | ||
return setTimeout(() => { | ||
req.destroy(); | ||
|
||
resolve({ | ||
status: 0, | ||
content, | ||
isTimedOut: true, | ||
}); | ||
}, timeout * 1000); | ||
}; | ||
|
||
const connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout'); | ||
|
||
let responseTimeout: NodeJS.Timeout | undefined; | ||
|
||
req.on('error', (error) => { | ||
clearTimeout(connectTimeout); | ||
clearTimeout(responseTimeout!); | ||
resolve({ status: 0, content: error.message, isTimedOut: false }); | ||
}); | ||
|
||
req.once('response', () => { | ||
clearTimeout(connectTimeout); | ||
responseTimeout = createTimeout(request.responseTimeout, 'Socket timeout'); | ||
}); | ||
|
||
if (request.data !== undefined) { | ||
req.write(request.data); | ||
} | ||
|
||
req.end(); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,4 @@ | ||
import { EndRequest, Response } from './types'; | ||
import * as http from 'http'; | ||
import * as https from 'https'; | ||
|
||
export class Requester { | ||
private httpAgent: http.Agent; | ||
private httpsAgent: https.Agent; | ||
|
||
constructor() { | ||
this.httpAgent = new http.Agent({ keepAlive: true }); | ||
this.httpsAgent = new https.Agent({ keepAlive: true }); | ||
} | ||
|
||
async send(request: EndRequest): Promise<Response> { | ||
return new Promise((resolve) => { | ||
const url = new URL(request.url); | ||
|
||
const path = url.search === null ? url.pathname : `${url.pathname}?${url.search}`; | ||
|
||
const options: https.RequestOptions = { | ||
agent: url.protocol === 'https:' ? this.httpsAgent : this.httpAgent, | ||
hostname: url.hostname, | ||
path, | ||
method: request.method, | ||
headers: request.headers, | ||
...(url.port !== undefined ? { port: url.port || '' } : {}), | ||
}; | ||
|
||
const req = (url.protocol === 'https:' ? https : http).request(options, (response) => { | ||
let contentBuffers: Buffer[] = []; | ||
|
||
response.on('data', (chunk) => { | ||
contentBuffers = contentBuffers.concat(chunk); | ||
}); | ||
|
||
response.on('end', () => { | ||
clearTimeout(connectTimeout); | ||
clearTimeout(responseTimeout as NodeJS.Timeout); | ||
|
||
resolve({ | ||
status: response.statusCode || 0, | ||
content: Buffer.concat(contentBuffers).toString(), | ||
isTimedOut: false, | ||
}); | ||
}); | ||
}); | ||
|
||
const createTimeout = (timeout: number, content: string): NodeJS.Timeout => { | ||
return setTimeout(() => { | ||
req.destroy(); | ||
|
||
resolve({ | ||
status: 0, | ||
content, | ||
isTimedOut: true, | ||
}); | ||
}, timeout * 1000); | ||
}; | ||
|
||
const connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout'); | ||
|
||
let responseTimeout: NodeJS.Timeout | undefined; | ||
|
||
req.on('error', (error) => { | ||
clearTimeout(connectTimeout); | ||
clearTimeout(responseTimeout!); | ||
resolve({ status: 0, content: error.message, isTimedOut: false }); | ||
}); | ||
|
||
req.once('response', () => { | ||
clearTimeout(connectTimeout); | ||
responseTimeout = createTimeout(request.responseTimeout, 'Socket timeout'); | ||
}); | ||
|
||
if (request.data !== undefined) { | ||
req.write(request.data); | ||
} | ||
|
||
req.end(); | ||
}); | ||
} | ||
export abstract class Requester { | ||
abstract send(request: EndRequest): Promise<Response>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Response } from './types'; | ||
|
||
export function isNetworkError({ isTimedOut, status }: Omit<Response, 'content'>): boolean { | ||
return !isTimedOut && ~~status === 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I get the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's when there's no response at all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copied it from the existing client, I don't know why there is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, thanks for clarifying! The |
||
} | ||
|
||
export function isRetryable({ isTimedOut, status }: Omit<Response, 'content'>): boolean { | ||
return ( | ||
isTimedOut || | ||
isNetworkError({ isTimedOut, status }) || | ||
(~~(status / 100) !== 2 && ~~(status / 100) !== 4) | ||
); | ||
} | ||
|
||
export function isSuccess({ status }: Pick<Response, 'status'>): boolean { | ||
return ~~(status / 100) === 2; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.