Skip to content

Commit

Permalink
Add proxy location (#934)
Browse files Browse the repository at this point in the history
* fix: update prisma setups for proxy locations

* fix: set location optional add update dto

* feat: use proxy for specified location if defined in adapter

* fix: remove unused import

* fix: fix test code

* fix: move location setting into definition

* fix: check location existance

* feat: update based on feedbacks

* docs: update readme
  • Loading branch information
nick-bisonai authored Nov 20, 2023
1 parent 58b79f6 commit 59eb4a7
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 7 deletions.
7 changes: 7 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,10 @@ GET http://localhost:3000/api/v1/proxy
3. Insert `Aggregator` (initial settings)
4. Insert `Proxy` (during fetching data with Orakl Network Fetcher)
5. Insert `Data` (during regular data fetching with Orakl Network Fetcher)

## Proxy Location codes

| location | code |
| --------- | ---- |
| Korea | kr |
| Singapore | sg |
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `location` to the `proxies` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "proxies" ADD COLUMN "location" TEXT NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "proxies" ALTER COLUMN "location" DROP NOT NULL;
3 changes: 2 additions & 1 deletion api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ model Error {
}

model Proxy {
id BigInt @id @default(autoincrement())
id BigInt @id @default(autoincrement())
protocol String
host String
port Int
location String?
@@unique([protocol, host, port])
@@map("proxies")
Expand Down
3 changes: 3 additions & 0 deletions api/src/proxy/dto/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export class ProxyDto {

@ApiProperty()
port: number

@ApiProperty()
location?: string
}
15 changes: 14 additions & 1 deletion api/src/proxy/proxy.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,26 @@ describe('ProxyService', () => {
const proxyData = {
protocol: 'http',
host: '127.0.0.1',
port: 80
port: 80,
location: 'kr'
}
const proxyObj = await proxy.create(proxyData)
expect(proxyObj.protocol).toBe(proxyData.protocol)
expect(proxyObj.host).toBe(proxyData.host)
expect(proxyObj.port).toBe(proxyData.port)
expect(proxyObj.location).toBe(proxyData.location)

const proxyDataWithoutLocation = {
protocol: 'http',
host: '127.0.0.2',
port: 80
}

const proxyObjWithoutLocation = await proxy.create(proxyDataWithoutLocation)
expect(proxyObjWithoutLocation.protocol).toBe(proxyDataWithoutLocation.protocol)
expect(proxyObjWithoutLocation.host).toBe(proxyDataWithoutLocation.host)
expect(proxyObjWithoutLocation.port).toBe(proxyDataWithoutLocation.port)
expect(proxyObjWithoutLocation.location).toBe(null)
// The same proxy cannot be defined twice
await expect(async () => {
await proxy.create(proxyData)
Expand Down
11 changes: 7 additions & 4 deletions cli/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios'
import { command, subcommands, option, string as cmdstring, number } from 'cmd-ts'
import { idOption, buildUrl, isOraklNetworkApiHealthy } from './utils'
import { idOption, buildUrl, isOraklNetworkApiHealthy, proxyOptionalOption } from './utils'
import { ORAKL_NETWORK_API_URL } from './settings'

const PROXY_ENDPOINT = buildUrl(ORAKL_NETWORK_API_URL, 'proxy')
Expand Down Expand Up @@ -30,7 +30,8 @@ export function proxySub() {
port: option({
type: number,
long: 'port'
})
}),
location: proxyOptionalOption
},
handler: insertHandler()
})
Expand Down Expand Up @@ -70,16 +71,18 @@ export function insertHandler() {
async function wrapper({
protocol,
host,
port
port,
location
}: {
protocol: string
host: string
port: number
location?: string
}) {
if (!(await isOraklNetworkApiHealthy())) return

try {
const response = (await axios.post(PROXY_ENDPOINT, { protocol, host, port }))?.data
const response = (await axios.post(PROXY_ENDPOINT, { protocol, host, port, location }))?.data
console.dir(response, { depth: null })
} catch (e) {
console.error('Proxy was not inserted. Reason:')
Expand Down
5 changes: 5 additions & 0 deletions cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export const fetcherTypeOptionalOption = option({
long: 'fetcherType'
})

export const proxyOptionalOption = option({
type: optional(cmdstring),
long: 'location'
})

export const idOption = option({
type: cmdnumber,
long: 'id'
Expand Down
1 change: 1 addition & 0 deletions fetcher/src/job/job.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ export interface IProxy {
protocol: string | undefined
host: string | undefined
port: number | undefined
location?: string | undefined
}
13 changes: 12 additions & 1 deletion fetcher/src/job/job.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,18 @@ export function extractFeeds(
const feeds = adapter.feeds.map((f) => {
let proxy: IProxy
try {
proxy = proxySelector(f.definition.url)
if (!f.definition.location) {
proxy = proxySelector(f.definition.url)
} else {
const availableProxies = proxies.filter(
(item) => item.location && item.location === f.definition.location
)
if (availableProxies.length == 0) {
throw `no proxies available for location:${f.definition.location}`
}
const randomIndex = Math.floor(Math.random() * availableProxies.length)
proxy = availableProxies[randomIndex]
}
} catch (e) {
logger.error('Assigning proxy has failed')
logger.error(e)
Expand Down

0 comments on commit 59eb4a7

Please sign in to comment.