Can a Worker now call itself?

Hello,

There used to be an issue where a Worker could not call another Worker within the same zone (or indeed itself). That would return an error, likely a 1101. For example in this thread:

I was wondering whether that issue has now been solved with the addition of Worker Custom Domains? Only this blog post says that with those new Custom Domains, the Worker acts as an origin server (previously it wasn’t an origin). So … can you now call e.g api.example.com from api.example.com:

Finally, what if you need to talk to your Worker from another one? Since these Workers act as an origin server, you can just send a normal request to the associated endpoint, and we’ll invoke that Worker – even if it’s on the same Cloudflare zone.

Thanks.

Service Bindings exist for the purpose of calling your own Workers.

Using routes instead of custom domains, it would just fall-through to the underlying origin, or timeout if nothing was there (i.e 100:: is the DNS record for that subdomain).

It’s covered in the documentation.

Another benefit of integration with Cloudflare DNS is that you can use your Custom Domains like you would any external dependency. Your Workers can fetch() Custom Domains and invoke their associated Worker, even if the Worker is on the same Cloudflare zone. The newly invoked Worker is treated like a new top-level request and will execute in a separate thread.

1 Like

@KianNH Thanks, I’d seen the references but e.g Service Bindings say “invoke other workers” and I don’t believe the Custom Domains docs reference the same Worker, only others within the same zone. So it was to clarify. Why? Well I had a monolith (well, as far as a Worker could be, size-wise!) api in Express that I was thinking about converting to a Worker. But it awkwardly calls itself in places. Which works fine e.g on a Nodejs server (or on Lambda) because of how they work. But not (in the past) as a Worker. Since the api.example.com Worker could not call api.example.com Worker, doing a request to itself.

And a service binding to itself would not (I assume) make sense :slight_smile: That’s what I was thinking about Custom Domains. As that says the Worker is the origin. And thus could (should?) solve that problem.

Workers can technically bind to themselves via Service Bindings. There’s no constraint in the API as it stands today.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.