https://discord.cloudflare.com logo
Join Discord
Powered by
# workers-discussions
  • i

    Iann

    05/26/2023, 9:32 PM
    Any way to point dynamic subdomains to a worker ?
  • c

    Chaika

    05/26/2023, 9:32 PM
    You can use Durable Object locationHints as well, but the regions are pretty large
  • c

    Chaika

    05/26/2023, 9:33 PM
    You mean like wildcard? You can use HTTP Routes and a proxied wildcard, (ex:
    *.example.com/*
    or
    */*
    worker route,
    AAAA
    *.example.com
    100::
    Proxied
    record)
  • h

    harris

    05/26/2023, 9:33 PM
    Smart placement isn't available for cron trigger i believe
  • i

    Iann

    05/26/2023, 9:33 PM
    it doesn't seem possible for workers
  • j

    James

    05/26/2023, 9:33 PM
    Yeah they don't work together yet unfortunately
  • c

    Chaika

    05/26/2023, 9:33 PM
    For Workers? 100% it is, I have a worker just like that. You just have to use Http Routes and not Custom Domains
  • i

    Iann

    05/26/2023, 9:34 PM
    Interesting, let me check
  • c

    Chaika

    05/26/2023, 9:34 PM
    Also for each domain you don't want to be controlled by the worker, you need to add a seperate route with a service of none
  • c

    Chaika

    05/26/2023, 9:35 PM
    You'd end up with something like: `api.example.com/*`: service none (for normal origins)
    */*
    to match all or
    *.example.com/*
    to match all subdomains besides apex, service: Worker
  • c

    Chaika

    05/26/2023, 9:36 PM
    (It may be worth keeping in mind as well the default universal cert you get only covers first level subdomains, while your worker could respond on gifhigfohgfd.workers.example.com, your cert won't cover it unless you use ACM for
    *.workers.example.com
    )
  • h

    harris

    05/26/2023, 9:47 PM
    Question on limits: I'm using the Unbounded Usage Model, but my worker sends a HTTPS request that has a streaming body, and can take longer than 30 seconds, however my worker runs every 5 minutes. How do these limits interact?

    https://cdn.discordapp.com/attachments/779390076219686943/1111772739167080468/image.png▾

    https://cdn.discordapp.com/attachments/779390076219686943/1111772739380969542/image.png▾

  • k

    kian

    05/26/2023, 9:49 PM
    30 seconds is CPU time
  • k

    kian

    05/26/2023, 9:49 PM
    Streaming a body from a
    fetch
    subrequest isn't going to use up any CPU
  • h

    harris

    05/26/2023, 9:49 PM
    even if I am using the main function to log something like time to first byte?
  • h

    harris

    05/26/2023, 9:52 PM
    Copy code
    ts
        const response = await fetch(url, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${env.OPENAI_API_KEY}`,
          },
          body,
        })
          .then((res) => response.body)
          .then((body) => {
            const reader = body.getReader()
    
            // do stuff with reader
          })
  • k

    kian

    05/26/2023, 9:56 PM
    What is happening in
    do stuff with reader
    ?
  • k

    kian

    05/26/2023, 9:57 PM
    It'll probably use up CPU time, but I doubt it's anywhere near 30 seconds
  • h

    harris

    05/26/2023, 10:03 PM
    Copy code
    ts
    const startTime = Date.now()
    
        const response = await fetch(url, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${env.OPENAI_API_KEY}`,
          },
          body,
        })
    
        if (!response.ok) {
          console.log(
            `Unexpected response from OpenAI API: ${response.status} ${response.statusText}`
          )
          return
        }
    
        if (!response.body) {
          console.log("No response body")
          return
        }
        const reader = response.body.getReader()
    
        // Get first byte
        let chunk = await reader.read()
    
        // Log first byte time
        const firstByteTime = Date.now() - startTime
    
        // Read the rest of the response
        while (!chunk.done) {
          chunk = await reader.read()
        }
  • h

    harris

    05/26/2023, 10:03 PM
    the event stream might not finish within 30s, and this code would be executing on the main thread, wouldn't it?
  • k

    kian

    05/26/2023, 10:04 PM
    > the event stream might not finish within 30s It's 30 seconds of CPU time, none of this is particularly heavy compute.
  • h

    harris

    05/26/2023, 10:04 PM
    oooh i see
  • h

    harris

    05/26/2023, 10:04 PM
    basically CPU time = time to process
  • k

    kian

    05/26/2023, 10:04 PM
    You could just do your TTFB after the
    const response = await ...
    and then
    return response
    , couldn't you?
  • h

    harris

    05/26/2023, 10:04 PM
    there's more code underneath
  • h

    harris

    05/26/2023, 10:05 PM
    Copy code
    ts
     const totalTime = Date.now() - startTime
    
        console.log("Time to first byte: " + firstByteTime + "ms")
        console.log("Total response time: " + totalTime + "ms")
    
        // Log response time
        const client = new Client(env.DB_URL + "?sslmode=require")
        await client.connect()
    
        const text =
          "INSERT INTO response_times(model, date, ttfb, duration) VALUES($1, $2, $3, $4) RETURNING *"
        const values = [
          "gpt-4",
          new Date(event.scheduledTime),
          firstByteTime,
          totalTime,
        ]
    
        try {
          const res = await client.query(text, values)
          console.log(res.rows[0])
        } catch (err) {
          console.error(err)
        } finally {
          ctx.waitUntil(client.end())
        }
  • i

    Isaac McFadyen | YYZ01

    05/26/2023, 10:09 PM
    CPU time = the time an operation takes on the CPU
  • i

    Isaac McFadyen | YYZ01

    05/26/2023, 10:09 PM
    It's not related to real time at all, really
  • i

    Isaac McFadyen | YYZ01

    05/26/2023, 10:10 PM
    A CPU intensive operation taking 1 second could use up to 1 second of CPU time (ignoring Workers limits of course), but that would mean it's using 100% of the CPU for 100% of the time which never happens, so it's usually far less
  • i

    Isaac McFadyen | YYZ01

    05/26/2023, 10:10 PM
    Fetch requests take a few ms of CPU time, for example, even though they might wait far longer for a response from wherever you're fetching from
1...250125022503...2509Latest