gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit02dccbc902dccbc9Merge branch 'records-1119' into mainJukebox02dccbc9/plugins/fetch/README.md

3.5 KB

  1. # hl:fetch
  2. `fetch(url, options)` makes an HTTP or HTTPS request and waits for the answer.
  3. ## Waiting and the event loop
  4. `fetch()` suspends only the delivery fiber that called it; other requests and
  5. concurrent arms can run. At the process root there is no delivery fiber, so the
  6. wait can block. `fetchStart()` returns immediately. With `completions()` and
  7. `on fetched(ev)`, the caller never waits for the response. Register the source
  8. before starting requests, and keep the pending ticket reachable until completion
  9. (closing a ticket joins its worker).
  10. ```hybriel
  11. // /slow queues a fetch; /fast answers while it runs.
  12. import { NativeHttpServer, Response } from 'hl:http1'
  13. import { fetchStart, completions } from 'hl:fetch'
  14. inherit NativeHttpServer
  15. port = 27570
  16. pending = null
  17. on request(req) { if (req.path == '/slow') { pending = fetchStart('http://127.0.0.1:27571/slow', { tag = 'job' }) } return new Response('accepted') }
  18. on fetched(ev) { console.log(ev.tag + ': ' + ev.status) }
  19. listen()
  20. __native('eventloop.register', completions(), 'fetched')
  21. ```
  22. Native fetch tries resolved addresses in order. While another address remains,
  23. a connection attempt waits at most 300 ms before closing its socket and trying
  24. the next; refused or failed connections move on immediately. The last address
  25. gets the remaining whole-exchange `timeoutMs` budget, including time spent in DNS
  26. and earlier redirect hops. DNS itself uses a blocking resolver and cannot be
  27. interrupted at the deadline; after it returns, an expired budget prevents connect.
  28. ## When the connection fails
  29. A refused connection, an unknown host, a connect timeout, a reset by the peer and a TLS
  30. verification failure are all raised as an ordinary located error. The message names the method,
  31. the URL and the reason, for example
  32. `hl:fetch POST http://127.0.0.1:9/x: connection refused`,
  33. `hl:fetch GET http://nonexistent.invalid/x: could not resolve host` or `hl:fetch GET http://10.255.255.1/x: timed out`.
  34. The error can be caught with `on Error(e)`, in a plain program and inside a request handler alike.
  35. When the handler has absorbed the error, `fetch` gives back `null`, so the code can answer the failure itself:
  36. ```hybriel
  37. import { NativeHttpServer, Response } from 'hl:http1'
  38. import { fetch } from 'hl:fetch'
  39. inherit NativeHttpServer
  40. port = 8080
  41. on Error(e) { console.log('fetch failed: ' + e.message) }
  42. on request(req) {
  43. let res = fetch('http://127.0.0.1:9/x', { method = 'POST' json = { a = 1 } })
  44. if (res == null) { return new Response('upstream is down', { status = 502 }) }
  45. return new Response(res.text())
  46. }
  47. listen()
  48. ```
  49. Without an `on Error` handler the error is not caught. That is the language rule: the request then ends
  50. with the framework's `500` answer carrying the located message, and the server keeps serving.
  51. ## A failure as a value
  52. Import `tryFetch` from `hl:fetch` to handle a failed exchange without `on Error`.
  53. It accepts fetch's options and waits for completion. Every HTTP reply, including
  54. 404 or 500, has the same response and text()/json() readers as fetch. Connection,
  55. DNS, TLS and timeout failures return exactly `{ ok = false, status = 0, error }`.
  56. The error is fetch's message without its call site, for example
  57. `hl:fetch GET http://127.0.0.1:9/x: connection refused`. Native fetch is unchanged.
  58. The JS target and browser use the host Fetch API with the same response shape
  59. and whole-exchange `timeoutMs`. Browsers enforce CORS and expose only a generic
  60. network reason for many failures. `caFile` is supported only by the native engine.
  61. `tryFetch` waits even with `stream = true`; use fetchStart for native chunk events.

Branches

Latest commits

  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
  • 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
  • 1fecd6a7Merge branch 'records-day39' into mainJukebox
  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
  • 5fa19e89Merge branch 'day39' into mainJukebox
  • cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
  • f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
  • 9f5c6532Merge branch 'status-22-pr' into mainJukebox