opt slow download

master
cyhhao 2 years ago
parent 4c1d32031c
commit 28092ef0e1

@ -1,6 +1,6 @@
{ {
"name": "git3-cli", "name": "git3-cli",
"version": "0.1.1", "version": "0.1.3",
"license": "MIT", "license": "MIT",
"type": "module", "type": "module",
"engines": { "engines": {

@ -1,7 +1,9 @@
export default { export default {
gateways: [ gateways: [
"https://ipfs.io/ipfs/", "https://{cid}.ipfs.nftstorage.link/",
"https://cloudflare-ipfs.com/ipfs/", "https://{cid}.ipfs.w3s.link",
"https://gateway.ipfs.io/ipfs/", "https://ipfs.io/ipfs/{cid}",
"https://cloudflare-ipfs.com/ipfs/{cid}",
"https://fleek.ipfs.io/ipfs/{cid}",
], ],
} }

@ -11,6 +11,7 @@ class Git {
refs: Map<string, string> = new Map() refs: Map<string, string> = new Map()
pushed: Map<string, string> = new Map() pushed: Map<string, string> = new Map()
head: string | null head: string | null
fetchPending: Map<string, number> = new Map()
constructor(info: ApiBaseParams, storage: Storage) { constructor(info: ApiBaseParams, storage: Storage) {
this.gitdir = info.gitdir this.gitdir = info.gitdir
@ -92,6 +93,13 @@ class Git {
} }
async fetch(oid: string) { async fetch(oid: string) {
if (this.fetchPending.has(oid)) {
let cnt = this.fetchPending.get(oid)!
this.fetchPending.set(oid, cnt + 1)
return
}
this.fetchPending.set(oid, 1)
let fetching: Promise<void>[] = [] let fetching: Promise<void>[] = []
if (GitUtils.objectExists(oid)) { if (GitUtils.objectExists(oid)) {
if (oid == GitUtils.EMPTY_TREE_HASH) { if (oid == GitUtils.EMPTY_TREE_HASH) {
@ -112,10 +120,12 @@ class Git {
fetching.push(this.fetch(sha)) fetching.push(this.fetch(sha))
} }
} else { } else {
fetching.push(this.fetch(oid)) throw error
//fetching.push(this.fetch(oid))
} }
} }
await Promise.all(fetching) await Promise.all(fetching)
this.fetchPending.set(oid, 0)
} }
async download(sha: string): Promise<Error | null> { async download(sha: string): Promise<Error | null> {

@ -5,7 +5,7 @@ import ipfsConf from "../config/ipfs.js"
import axios from "axios" import axios from "axios"
import { Git3Protocol } from "../common/git3-protocol.js" import { Git3Protocol } from "../common/git3-protocol.js"
import { QueueTask, Retrier } from "../common/queue-task.js" import { QueueTask, Retrier } from "../common/queue-task.js"
import https from "https"
export class SLIStorage implements Storage { export class SLIStorage implements Storage {
repoName: string repoName: string
wallet: ethers.Wallet wallet: ethers.Wallet
@ -65,13 +65,15 @@ export class SLIStorage implements Storage {
const buffer = Buffer.from(res[0].slice(2), "hex") const buffer = Buffer.from(res[0].slice(2), "hex")
const cid = buffer.toString("utf8") const cid = buffer.toString("utf8")
for (let i = 0; i < ipfsConf.gateways.length; i++) { for (let i = 0; i < ipfsConf.gateways.length; i++) {
let gateway = ipfsConf.gateways[Math.floor(Math.random() * ipfsConf.gateways.length)] //random get rpc let gateway = ipfsConf.gateways[i] // don't random, try every gateway
try { try {
let resault = await Retrier( let resault = await Retrier(
async () => { async () => {
const TIMEOUT = 15 const TIMEOUT = 3
let response = await axios.get(gateway + cid, { const CONNECT_TIMEOUT = 3
let response = await axios.get(gateway.replace("{cid}", cid), {
responseType: "arraybuffer", responseType: "arraybuffer",
httpsAgent: new https.Agent({ timeout: CONNECT_TIMEOUT * 1000 }),
timeout: TIMEOUT * 1000, timeout: TIMEOUT * 1000,
}) })
if (response.status === 200) { if (response.status === 200) {
@ -83,8 +85,9 @@ export class SLIStorage implements Storage {
{ maxRetry: 3 } { maxRetry: 3 }
) )
return [Status.SUCCEED, resault] return [Status.SUCCEED, resault]
} catch (e) { } catch (e: any) {
//pass //pass
// console.error("download err:", e.message, gateway.replace("{cid}", cid))
} }
} }

Loading…
Cancel
Save