Async, RPC and handling a global state

38 views
Skip to first unread message

Paolo Donadeo

unread,
Sep 15, 2016, 9:47:12 AM9/15/16
to Core mailing list
Hi all, I'm writing a server which is essentially an RPC server and I'm using the Tcp and Rpc module of Async.

If i well understand the framework, each incoming connection is bound to a function, which handle the request. But the server, as a whole, has a "shared mutable state": the "shared" part is very simple to be implemented, but the mutable one is not.

To be more concrete the clients will send physical data (temperatures, pressures, electrical quantities, etc.) collected in many customer's plants to the server, which will store the data in many ways (relational DB, raw files, and so on).

The first "shared mutable state" which come in mind is a map of all the opened files: every server callback (serving a client) wants to know if the file is already opened and, if not, open it in append and modify the "global map". Another example is the connection to PostgreSQL or InfluxDB.

I found an old post by Jon Harrop (https://groups.google.com/forum/#!topic/ocaml-core/dxAVAj33TDE) which exposed more or less the same problem, and an example by Drew Atkin about the use of the Bus module, but I don't really understand how to use Pipes and Buses to avoid a global ref which is of course the easy way to solve the problem.

In Async a mutable global is perfectly safe but I don't really like the paradigm.

Any suggestion about how to solve the problem?


--
Paolo

Yaron Minsky

unread,
Sep 15, 2016, 10:56:40 AM9/15/16
to ocaml...@googlegroups.com
Having mutable state that is shared by the things that need to cause
state transitions is not in my view a paradigm to be avoided.
Concurrency is fundamentally imperative, and having a single ref that
points to the current version of your (functional) state is I think a
reasonable way of bridging the functional and imperative worlds.

y
> --
> You received this message because you are subscribed to the Google Groups
> "ocaml-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ocaml-core+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Paolo Donadeo

unread,
Sep 16, 2016, 7:32:38 PM9/16/16
to Core mailing list
Thanks Yaron, maybe I was over-engineering.


--
Paolo
Reply all
Reply to author
Forward
0 new messages