yesodweb / wai

Haskell Web Application Interface
MIT License
834 stars 263 forks source link

Connection Counter #651

Open Daniel-Diaz opened 7 years ago

Daniel-Diaz commented 7 years ago

Hello.

In an application I wanted to have available the number of open connections to the server. So I used setOnOpen and setOnClose together with a variable. This worked well.

For some reason, I started reading warp's code, and realized that there is already a counter set up in those same places, but this counter is hidden from the user. I know keeping an additional counter is not a big deal, but maybe I just don't like redundant work.

In my opinion, the best way to solve this is to export the Counter type, but only with construction and accessing functions so that the user can't mess up the internals, and then add setWithCounter :: Maybe Counter -> Settings -> Settings so that the user of the library can create a counter and keep track of it. When Nothing is provided, warp would create the counter just like it does today.

Well, this was my thought today. If it's regarded as a good idea, I can go ahead and write a patch.

Thanks.

snoyberg commented 7 years ago

I'd defer to @kazu-yamamoto on this one. My gut feeling is that this is an internal structure that we wouldn't really want to expose, but I don't have a strong feeling here.

kazu-yamamoto commented 7 years ago

Generally, I don't mind exporting Counter.

so that the user of the library can create a counter and keep track of it.

I don't understand this. So, please show me your patch.

Vlix commented 2 years ago

@Daniel-Diaz Could you explain what you meant with the following?

so that the user of the library can create a counter and keep track of it.

Do you mean you want the user to be able to provide a TVar which will be adjusted exactly the same as the internal one? I don't think we want to expose the internal one, since it is relied on for graceful shutdowns.

Daniel-Diaz commented 2 years ago

@Daniel-Diaz Could you explain what you meant with the following?

so that the user of the library can create a counter and keep track of it.

Do you mean you want the user to be able to provide a TVar which will be adjusted exactly the same as the internal one? I don't think we want to expose the internal one, since it is relied on for graceful shutdowns.

Hello!

Wow, this is really old. I totally forgot about it!

I'm not completely sure what I meant by that sentence, but I definitely meant for the counter to be read-only. I guess a function to read it without exposing the type would also do.