Skip to content

Commit 4729e41

Browse files
committed
feat: add retry for tinybird 408
1 parent 9e3a063 commit 4729e41

1 file changed

Lines changed: 40 additions & 22 deletions

File tree

services/libs/database/src/tinybirdClient.ts

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,25 @@ export class TinybirdClient {
6767
const url = `${this.host}/v0/pipes/${encodeURIComponent(pipeName)}.json${
6868
searchParams.toString() ? `?${searchParams}` : ''
6969
}`
70-
71-
const result = await axios.get<T>(url, {
72-
headers: {
73-
Authorization: `Bearer ${this.token}`,
74-
Accept: 'application/json',
75-
},
76-
httpsAgent: TinybirdClient.httpsAgent,
77-
})
78-
79-
// TODO: check the response type
80-
return result.data
70+
71+
for (let attempt = 1; attempt <= 3; attempt++) {
72+
try {
73+
const result = await axios.get<T>(url, {
74+
headers: {
75+
Authorization: `Bearer ${this.token}`,
76+
Accept: 'application/json',
77+
},
78+
httpsAgent: TinybirdClient.httpsAgent,
79+
})
80+
return result.data
81+
} catch (error) {
82+
if (error?.response?.status === 408 && attempt < 3) {
83+
await new Promise((res) => setTimeout(res, 1000)) // wait before retrying
84+
continue // retry
85+
}
86+
throw error
87+
}
88+
}
8189
}
8290

8391
/**
@@ -115,16 +123,26 @@ export class TinybirdClient {
115123
}
116124
}
117125

118-
const result = await axios.post<T>(url, body, {
119-
headers: {
120-
Authorization: `Bearer ${this.token}`,
121-
'Content-Type': 'application/json',
122-
Accept: 'application/json',
123-
},
124-
responseType: 'json',
125-
httpsAgent: TinybirdClient.httpsAgent,
126-
})
127-
128-
return result.data
126+
for (let attempt = 1; attempt <= 3; attempt++) {
127+
try {
128+
const result = await axios.post<T>(url, body, {
129+
headers: {
130+
Authorization: `Bearer ${this.token}`,
131+
'Content-Type': 'application/json',
132+
Accept: 'application/json',
133+
},
134+
responseType: 'json',
135+
httpsAgent: TinybirdClient.httpsAgent,
136+
})
137+
138+
return result.data
139+
} catch (error) {
140+
if (error?.response?.status === 408 && attempt < 3) {
141+
await new Promise((res) => setTimeout(res, 1000)) // wait before retrying
142+
continue // retry
143+
}
144+
throw error
145+
}
146+
}
129147
}
130148
}

0 commit comments

Comments
 (0)