Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutex in CaptureMetricsFn #13

Closed
ebati opened this issue Mar 1, 2021 · 6 comments · Fixed by #14
Closed

Mutex in CaptureMetricsFn #13

ebati opened this issue Mar 1, 2021 · 6 comments · Fixed by #14

Comments

@ebati
Copy link
Contributor

ebati commented Mar 1, 2021

I opened an issue in opentelemetry-go-contrib about using this library.

I will send a PR to add another hook for ReadFrom but i am not sure why the mutex in your implementation of CaptureMetricsFn is used. Is concurrent call to those functions possible or is there any reason to use mutex in this context?

@felixge
Copy link
Owner

felixge commented Mar 1, 2021

IIRC I added this mutex because the application could definitely call w.Write() on the same ResponseWriter from two goroutines. Arguably that wouldn't be a good idea, and maybe it shouldn't be the job of my library to deal with such pathological application behavior. What do you think?

@ebati
Copy link
Contributor Author

ebati commented Mar 1, 2021

I think the same that concurrent write calls are not a normal use.

I just have a hesitation even if the order of written data is incorrect, one might implement some kind of packet data and write packets concurrently. If a call to Write somehow writes whole data to the underlying connection at once and ensures this property (I couldn't see anything in the documentation), that might be a valid edge case. For example Logger has this kind of guarantee.

@felixge
Copy link
Owner

felixge commented Mar 2, 2021

I'm not sure I completely follow your last comment.

Anyway, the way I look at it is: In the worst case the mutex is redundant overhead. In the best case it will prevent bugs. Precautionary principle slightly guides me towards keeping it. But I could be convinced otherwise if somebody wants to do some more research.

Is the presence of the mutex causing you any issues for now?

@ebati
Copy link
Contributor Author

ebati commented Mar 2, 2021

No, there is not any issue for now. I just want to learn why mutex is used.

I didn't checked the http code yet but httptest.ResponseRecorder does nothing to prevent race in Write suggest me that concurrent Write is not an allowed.

@7fffffff
Copy link

I think ebati was pointing out that if an application has multiple goroutines writing to the same Writer, a mutex is enough to ensure the correct ordering when each Write is a complete unit of data, like a log line. If the goroutines Write partial data, only the application knows how those goroutines need to be synchronized and the mutex does not add safety.

I'm inclined to say that it's the application's responsibility to serialize access to a ResponseWriter, so the mutex is unnecessary.

@felixge
Copy link
Owner

felixge commented Mar 15, 2021

@ebati @7fffffff ok, I think y'all convinced me : ). If any of you wants to send a PR to remove the mutex, I'd be happy to merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants