I am creating a webserver using tokio
. Whenever a client connection comes in, a green thread is created via tokio::spawn
.
The main function of my web server is proxy. Target server information for proxy is stored as a global variable, and for proxy, all tasks must access the data. Since there are multiple target servers, they must be selected by round robin. So the global variable (struct) must have information of the recently selected server(by index).
Concurrency problems occur because shared information can be read/written by multiple tasks at the same time.
According to the docs, there seems to be a way to use Mutex
and Arc
or a way to use channel
to solve this.
I'm curious which one you usually prefer, or if there is another way to solve the problem.