Controlling readers and writers inside a Tokio server

Hi . I'm so newbie in Tokio, in particular, and in Rust, in general :slight_smile: .

I would like to control the number of readers inside a TCP server developed with Tokio to avoid having problems with attacks . I want to have a poll with a predefined number of threads . Those threads will represent readers .

On the other hand, I want to have just one writer that access concurrently to a resource inside the server .

I have seen tokio::sync::RwLock, that let me have multiple readers and one writer . What I'm scared is having problems to receive an attack of having more readers than machine can handle .

This TCP server will represent a multiple reader/one writer paradigm sharing a dictionary to control key / value elements .

Is there any way using RwLock to control this ? is it better to investigate through channels ?

Thanks in advance :slight_smile:

I'm not quite sure I understand your question. What is a "reader" and a "writer". Is it a certain kind of tcp connection?

Besides the rw lock, Tokio also provides a Semaphore. Maybe this fits your need more closely?

Maybe I should explain myself a little bit better . Sorry .

Taking from https://docs.rs/tokio/0.2.11/tokio/sync/struct.RwLock.html ...

This type of lock allows a number of readers or at most one writer at any point in time.
The write portion of this lock typically allows modification of the underlying data
(exclusive access) and the read portion of this lock typically allows for read-only
access (shared access).

In comparison, a [ Mutex ]
(https://docs.rs/tokio/0.2.11/tokio/sync/struct.Mutex.html) does not distinguish between
readers or writers that acquire the lock, therefore blocking any tasks waiting for the
lock to become available. An RwLock will allow any number of readers to acquire
the lock as long as a writer is not holding the lock.

What I'm trying to control is that number of readers . Semaphore might be a solution . I'll give a try . Thanks Alice .

You can indeed use a semaphore to limit the number of readers. Note that you are viewing the documentation for an old version of Tokio.

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