postgres icon indicating copy to clipboard operation
postgres copied to clipboard

fix: default max connections in cloudflare to lower

Open Cherry opened this issue 10 months ago • 3 comments

Fixes https://github.com/porsager/postgres/issues/1023

Cherry avatar Jan 31 '25 15:01 Cherry

That's a compiled file, so the change will be removed on publish.

porsager avatar Jan 31 '25 17:01 porsager

Ah I see, sorry. Would it make sense to add this to the transpile.cf.js file instead?

Cherry avatar Jan 31 '25 18:01 Cherry

Let me know if you'd like to see any other changes here @porsager. It'd be good to get this merged to prevent this common footgun!

Cherry avatar Apr 06 '25 20:04 Cherry

Let me know if there's anything else I can do to get this landed @porsager, thanks!

Cherry avatar Nov 12 '25 13:11 Cherry

Hi James.. Yes - let's get this finished.

I'm wondering if maybe dynamic resolution is better? If we at one point can move the Cloudflare polyfill I'm afraid we'll forget the max: 3 limit.

Is there an env var we could rely on in cloudflare or perhaps a global?

porsager avatar Nov 12 '25 14:11 porsager

Cloudflare does make globalThis.Cloudflare available on any modern workers (I'm not 100% certain if this is available on older service worker formatted Workers), which might suffice for checking? Alternatively, we could add a test that just asserts the output for the cloudflare build to have max: 3 which should catch any regressions?

Cherry avatar Nov 12 '25 14:11 Cherry

Something like this should work after the defaults setting:

  // Adjust max connections for Cloudflare Workers
  // https://github.com/porsager/postgres/issues/1023
  if (typeof globalThis !== 'undefined' && 'Cloudflare' in globalThis) {
    defaults.max = 3;
  }

Happy to do that if preferred, let me know!

Cherry avatar Nov 12 '25 15:11 Cherry

Cool! globalThis is available on all supported platforms, so if you add it as a ternary to defaults like this instead that'd be great!

max : globalThis.Cloudflare ? 3 : 10

A nice and simple one line PR ;)

porsager avatar Nov 12 '25 15:11 porsager

Done, thanks! Let me know if any other changes are needed.

Cherry avatar Nov 12 '25 16:11 Cherry

Nice - just remove the changes to everything but src/index.js as those are built automatically anyway. Also remove the comment - git keeps enough history ;)

porsager avatar Nov 12 '25 17:11 porsager

Sounds good, done, thanks

Cherry avatar Nov 12 '25 17:11 Cherry