-
Notifications
You must be signed in to change notification settings - Fork 50
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
Unexported struct fields in the Config can lead to crash on startup #68
Comments
Hi, that said, the config structure is being created at different points in time by calling the note that it's normal to create config objects that are never actually used, and also they're dropped (and disposed by the GC) without notice. EDIT: after checking the PR I see what you mean. yes, unexported fields shouldn't be added to the schema. I'll do a review there. |
Thanks! But just to be clear holding state in the config objects should not be done? At least so far I have not noticed any issues with that. If so, how would you recommend holding some state in a plugin, with global vars? In a perfect world a plugin would be completely stateless, but for example being able to cache some things is almost required for some use cases. |
It's OK to use private fields for whatever purpose. The caveat is that config objects can be dropped by the framework without warning, so you can't depend on them being persistent. Also, there might be any number of them at the same time, so it's not a good place to put semaphores to arbitrate data stored somewhere else. Specifically:
In all, there's very few uses that would be best served by private fields in the config object. In most cases I guess global variables (protected by mutexes), internal service threads and/or database connections (if you want persistence) are more generally useful. |
Thanks for the explanation, that is really helpful! I will avoid storing things in the config struct then. |
In order to reuse an API client, I put it as an unexported field into the Config, like so:
go run . -dump
Note that the above might crash your computer when run! Be ready to ctrl-c to end the process. For some reason, this particular struct causes the whole memory to fill up pretty quickly. The struct contains lots of fields and nesting levels, but I was unable to reproduce the issue with a more simple struct.
Anyway, a simple fix would be to just ignore unexported fields. My understanding is that unexported fields won't work in the config anyway, since it needs to be Unmarshaled with
json.Unmarshal
later. I will post a PR with that fix but let me know if you would take a different approach here.The text was updated successfully, but these errors were encountered: