-
Notifications
You must be signed in to change notification settings - Fork 789
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
Fix error handling for metrics #1
Changes from 4 commits
df01b06
fb84867
8dfb4b9
fa2b8bb
cbc37c7
067f6c2
245c69f
3faec90
6fc9a2b
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 |
---|---|---|
|
@@ -33,35 +33,55 @@ export function anonymizeIp(ip: string): string { | |
} | ||
} | ||
|
||
// Cache country lookups per IP address. | ||
const countryCache = new Map<string, Promise<string>>(); | ||
export interface IpLocationService { countryForIp(ipAddress: string): Promise<string>; } | ||
|
||
export function lookupCountry(ipAddress: string) : Promise<string> { | ||
if (countryCache.has(ipAddress)) { | ||
// Return cached promise to prevent duplicate lookups. | ||
return countryCache.get(ipAddress); | ||
export class FreegeoIpLocationService implements IpLocationService { | ||
countryForIp(ipAddress: string): Promise<string> { | ||
const countryPromise = new Promise<string>((fulfill, reject) => { | ||
const options = {host: 'freegeoip.io', path: '/json/' + ipAddress}; | ||
https | ||
.get( | ||
options, | ||
(response) => { | ||
let body = ''; | ||
response.on('data', (data) => { | ||
body += data; | ||
}); | ||
response.on('end', () => { | ||
try { | ||
const jsonResponse = JSON.parse(body); | ||
if (jsonResponse.country_code) { | ||
fulfill(jsonResponse.country_code); | ||
} else { | ||
// ZZ is user-assigned and used by CLDR for "Uknown" regions. | ||
fulfill('ZZ'); | ||
} | ||
} catch (e) { | ||
reject(new Error(`Error loading country from reponse: ${e}`)); | ||
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. Seems like there's a chance this could log the IP address - I'd feel better if we weren't logging the error verbatim. 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. Removed the cause error |
||
} | ||
}); | ||
}) | ||
.on('error', (e) => { | ||
reject(new Error(`Failed to contact location service: ${e}`)); | ||
}); | ||
}); | ||
return countryPromise; | ||
} | ||
} | ||
|
||
const promise = new Promise<string>((fulfill, reject) => { | ||
const options = {host: 'freegeoip.io', path: '/json/' + ipAddress}; | ||
https.get(options, (response) => { | ||
let body = ''; | ||
response.on('data', (data) => { | ||
body += data; | ||
}); | ||
response.on('end', () => { | ||
try { | ||
fulfill(JSON.parse(body).country_code); | ||
} catch (err) { | ||
console.error('Error loading country: ', err); | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
}); | ||
export class CachedIpLocationService implements IpLocationService { | ||
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. Servers could stay up for weeks - let's file an issue to make this cache:
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. Added the comment. We should make it LRU. |
||
private countryCache: Map<string, Promise<string>>; | ||
|
||
// Prevent multiple lookups of the same country. | ||
countryCache.set(ipAddress, promise); | ||
constructor(private locationService: IpLocationService) { | ||
this.countryCache = new Map<string, Promise<string>>(); | ||
} | ||
|
||
return promise; | ||
countryForIp(ipAddress: string): Promise<string> { | ||
if (this.countryCache.has(ipAddress)) { | ||
return this.countryCache.get(ipAddress); | ||
} | ||
const promise = this.locationService.countryForIp(ipAddress); | ||
this.countryCache.set(ipAddress, promise); | ||
return promise; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, testing for strings is a minefield in JavaScript.
Array.isArray
might be safer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to Array.isArray