nactio / nact

nact ⇒ node.js + actors ⇒ your services have never been so µ
https://nact.xyz
Apache License 2.0
1.11k stars 54 forks source link

Stateless actors are not concurrent. #143

Open juanstiza opened 1 year ago

juanstiza commented 1 year ago

The documentation states:

Stateless actors can service multiple requests at the same time. Statelessness means that such actors do not have to cater for concurrency issues.

I assumed (and please correct me if I am wrong) that we could call these actors with any concurrency we desire.

Expected Behavior

Multiple messages being sent to a stateless actor should be processed in parallel

Current Behavior

Messages are being processed one at a time

Possible Solution

No clue

Steps to Reproduce (for bugs)

import nact from "nact";
const system = nact.start();
const delay = (time) => new Promise((res) => setTimeout(res, time));

const ping = nact.spawnStateless(
  system,
  async (msg, ctx) => {
    console.log(msg);
    console.log(new Date())
    await delay(500);

    throw new Error("oh no!");
  },
  "ping",
  {
    onCrash: (msg, e, ctx) => {
      console.log(e)
      return ctx.escalate
    }
  }
);

for (let i = 0; i < 5; i++) {
  nact.dispatch(ping, {
    i,
    createdAt: new Date()
  });
}

This outputs:

{ i: 0, createdAt: 2023-07-06T13:43:31.932Z }
2023-07-06T13:43:31.937Z
{ i: 1, createdAt: 2023-07-06T13:43:31.932Z }
2023-07-06T13:43:32.440Z
{ i: 2, createdAt: 2023-07-06T13:43:31.932Z }
2023-07-06T13:43:32.942Z
{ i: 3, createdAt: 2023-07-06T13:43:31.932Z }
2023-07-06T13:43:33.444Z
{ i: 4, createdAt: 2023-07-06T13:43:31.932Z }
2023-07-06T13:43:33.947Z

Context

I'm trying to run simple tasks in stateless actors and be able to run them with any desired cocurrency

Your Environment

Tested in node v14.21.3 and v18.14.2 on MacOS 13.4.1

ncthbrt commented 1 year ago

Thanks for reporting this @juanstiza.

larsw commented 10 months ago

Node.js is single-threaded, so it isn't possible to achieve true concurrency.

juanstiza commented 10 months ago

@larsw That is true, but it is not what this ticket is referencing to. Have you read it?