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

proposal: add Reset field to sync.Pool to reset item before putting it back to the pool #18638

Closed
mantzas opened this issue Jan 12, 2017 · 4 comments

Comments

@mantzas
Copy link

mantzas commented Jan 12, 2017

When putting an item back to the pool the fields are left with the any values it has at this point.
The next caller to the Get will get this dirty object.

My proposal is to create a Reset function field, much like the New one, which takes as an argument the object in question. This reset function should be called right before the item is put back to the internal store.

@mvdan
Copy link
Member

mvdan commented Jan 12, 2017

Why not simply wrap Put in a function that does this?

@josharian josharian added this to the Proposal milestone Jan 12, 2017
@josharian josharian changed the title proposal sync.Pool: Reset item before putting it back to the pool proposal: add Reset field to sync.Pool to reset item before putting it back to the pool Jan 12, 2017
@josharian
Copy link
Contributor

The Reset function will have to accept an interface{}, which means that you'll have to type assert inside it, at which point I suspect most people will choose to simply reset inline, or have a typed helper func that calls Put, as @mvdan suggested.

@bradfitz
Copy link
Contributor

sync.Pool is a low-level interface. It's a feature that you can get a "dirty" (read: initialized) item out of the pool again. If you need to zero some things, you can do so before Put or after Get.

@mantzas
Copy link
Author

mantzas commented Jan 12, 2017

@bradfitz makes sense

@golang golang locked and limited conversation to collaborators Jan 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants