package main // Storage in bulk, the way any app calls it (storage's README, "For an app: in bulk"): a JSON call to // storage with rc.app_headers("storage", true), and the bytes straight to B2 and back on the // presigned URLs it hands out. No storage key, and no byte passes through storage's own code. // // storage_call("begin", `{"files":[{"path":"postcards/a.png","size":812,"type":"image/png"}]}`) // // files[0]: {id, url, type, size, expires}; PUT the bytes to url with that Content-Type // storage_call("commit", `{"ids":[...]}`) // one B2 listing for the whole begin // storage_call("urls", `{"ids":[...]}`) // presigned GETs, five minutes each // storage_call("list", `{"after":0,"limit":1000}`) // what the zoo saved for this person import "core:encoding/json" import "core:fmt" import "ratcloud:rc" storage_call :: proc(op: string, in_json: string) -> (int, json.Value) { a := call_app("storage", cat("/app/", op), true, "POST", in_json, "application/json") if v := parse(a.body); v != nil do return a.status, v return a.status, parse(fmt.tprintf(`{{"error":%s}}`, jq(fmt.tprintf("storage answered %d: %s %s", a.status, clip(a.body, 160), a.err)))) } Put :: struct { url, type: string, body: []u8, } // At most this many calls at once: the host lets one request hold 16 open calls. MANY :: 15 // PUT many bodies to their presigned URLs, MANY at a time: their statuses, in order. B2 answers a // PUT now and then with 500 or 503 and asks clients to send it again (as storage does with its own // B2 calls); a dropped connection (0) is sent again too. Twice more at most: the URL is the same, so // a PUT that did arrive is simply replaced by the same bytes. put_many :: proc(jobs: []Put) -> []int { out := make([]int, len(jobs)) todo := make([dynamic]int) for i in 0 ..< len(jobs) do append(&todo, i) for try in 0 ..< 3 { if try > 0 do rc.sleep(500) for start := 0; start < len(todo); start += MANY { chunk := todo[start:min(start + MANY, len(todo))] calls := make([]rc.Fetch_Call, len(chunk)) for i, k in chunk { hs := make(map[string]string) hs["Content-Type"] = jobs[i].type calls[k] = rc.fetch_start({method = "PUT", url = jobs[i].url, headers = hs, body = jobs[i].body, timeout_ms = 30_000}) } buf: [4096]u8 for &c, k in calls { out[chunk[k]] = rc.fetch_status(&c) for rc.fetch_read(&c, buf[:]) > 0 {} rc.fetch_close(&c) } } again := make([dynamic]int) for i in todo do if out[i] == 0 || out[i] == 500 || out[i] == 503 do append(&again, i) if len(again) == 0 do break todo = again } return out } Got :: struct { status: int, body: []u8, } // GET many presigned URLs, MANY at a time: status and body, in order. get_many :: proc(urls: []string) -> []Got { out := make([]Got, len(urls)) for start := 0; start < len(urls); start += MANY { end := min(start + MANY, len(urls)) calls := make([]rc.Fetch_Call, end - start) for k in 0 ..< len(calls) do calls[k] = rc.fetch_start({url = urls[start + k], timeout_ms = 30_000}) buf := make([]u8, 16 * 1024) for &c, k in calls { st := rc.fetch_status(&c) body := make([dynamic]u8) for { n := rc.fetch_read(&c, buf) if n <= 0 do break append(&body, ..buf[:n]) } rc.fetch_close(&c) out[start + k] = {st, body[:]} } } return out }