[erlang-questions] gen_server vs gen_fsm

Chandru chandrashekhar.mullaparthi@REDACTED
Mon Oct 8 22:12:02 CEST 2007


On 08/10/2007, YC <yinso.chen@REDACTED> wrote:
> Hi -
>
> Thanks, one more question inline.
>
> On 10/8/07, Chandru <chandrashekhar.mullaparthi@REDACTED >
> wrote:
> >
> > > gen_fsm can certainly be used to implement the logic behind an
> > > e-commerce site where you'd have one instance of the gen_fsm for each
> > > customer you are handling. You'd somehow have to map (Cookie to
> > > process-id) your HTTP request to a particular instance of the state
> > > machine. If you wanted to keep the state machine persistent, you'd
> > > have to save it to some database and resurrect it when your next
> > > request comes in and your state machine isn't around for whatever
> > > reason.
>
> Interesting - are you saying erlang processes can be serialized to say
> Mnesia and revive later?  If that's doable it's certainly a very intriguing
> technique.  Can you point me to some examples/guides on how this is done?
>

It isn't quite what  you are imagining it to be :-)

Assuming you identify a user's HTTP session by means of a cookie and
you start a state machine to guide the customer through a payment
process. Each time your state machine's State gets updated, you could
save it mnesia with the Cookie as the key. The next time another
request comes along, you can spawn a new state machine process which
initialises itself with the state in mnesia and then handles the
request.

There is a way to put a process to sleep so that it doesn't consume
too much resources  - see erlang:hibernate/3. But this method won't
survive a node restart.

cheers
Chandru



More information about the erlang-questions mailing list