Skip to content

Instantly share code, notes, and snippets.

@alasarr
Last active March 17, 2023 17:00
Show Gist options
  • Save alasarr/2633ee721b67cde126cb26cad03abef6 to your computer and use it in GitHub Desktop.
Save alasarr/2633ee721b67cde126cb26cad03abef6 to your computer and use it in GitHub Desktop.
BigQuery connection reset bug snippet
const fetch = require('node-fetch')
const tilebelt = require('@mapbox/tilebelt')
const accessToken = ''
const projectId = 'carto-dev-data'
const url = `https://bigquery.googleapis.com/bigquery/v2/projects/${projectId}/queries`
async function runRequest (query) {
const body = {
kind: 'bigquery#queryRequest',
query,
useLegacySql: false,
// set to false as it's a performance test
useQueryCache: false,
// Requests will return at this time whether or not the Job is finished
timeoutMs: 30000
}
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
})
return await response.json()
}
async function run (nRequests) {
const promises = []
for (let i = 0; i < nRequests; i++) {
const p = runRequest(getQuery(i))
.then((r) => {
//console.log(r)
})
.catch(e => {
console.log('Error running request', e)
console.error(e)
})
promises.push(p)
}
console.log('Running parallel requests: ', nRequests)
await Promise.all(promises)
}
function getQuery (i) {
const [lat, long, zoom] = [40.730610, -73.935242, 12]
const tile = tilebelt.pointToTile(long, lat, zoom)
tile[0] -= i
const tileToGeoJSON = tilebelt.tileToGeoJSON(tile)
return `
SELECT *
FROM carto-dev-data.public.points_1m
WHERE ST_Intersects(geom, ST_GeogFromGeoJSON('${JSON.stringify(tileToGeoJSON)}', make_valid => TRUE))
`
}
run(50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment