GithubHelp home page GithubHelp logo

Comments (4)

javierguerragiraldez avatar javierguerragiraldez commented on July 18, 2024

Hi,
it's hard to tell what is happening here, but yes; unexported fields should be ignored by the (un)marshalling.

that said, the config structure is being created at different points in time by calling the New() function. there you can ensure those non-exported fields are correctly dealt with.

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.

from go-pdk.

ctrox avatar ctrox commented on July 18, 2024

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.

from go-pdk.

javierguerragiraldez avatar javierguerragiraldez commented on July 18, 2024

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?

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:

  • your New() function is called a number of times. There you can set any field before returning a new config instance.
  • the exported fields are filled with the configuration data from the Kong config (either database, declarative or from a Control Plane)
  • the config objects are used to call phase handlers. They're free to modify them too, but it would probably be unwise to change exported fields.
  • if any config object has been unused for a while (default 60 secs), it can be discarded.
  • if the configuration changes (either from the admin API or from a Control Plane), new config objects are created (by calling New() and filling exported fields with the new configuration data). the old objects will be unused and eventually discarded.
  • If the Kong configuration contains different instances of the same plugin, they're associated with different config objects.
  • For each configuration instance, there will be more than one config object instance at the same time, even if they're identical (this simplifies having multiple requests "on flight" at the same time).
  • There's no guarantee that different phases of the same request would get the same config object instance.

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.

from go-pdk.

ctrox avatar ctrox commented on July 18, 2024

Thanks for the explanation, that is really helpful! I will avoid storing things in the config struct then.

from go-pdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.