context: allow reuse in auxiliary packages#278
Closed
ffromani wants to merge 2 commits into
Closed
Conversation
81b63b2 to
564d199
Compare
Closed
564d199 to
c455327
Compare
Fix wrong comment, likely due to trivial copy/paste mistake. No code changes. Signed-off-by: Francesco Romani <fromani@redhat.com>
The PR jaypipes#236 fixes a snapshot directory leakage issue, but also highlight a context lifecycle/ownership issue. In general in ghw each package creates its own context, including the cases on which a package is consumed by another, for example `topology` and `pci`. However, in this case, it would make more sense to reuse the context from the parent package, because the two packages are tightly coupled. Before the introduction of the the transparent snapshot support (jaypipes#202), the above mechanism worked, because each context, each time, was consuming over and over again the same parameters (e.g. environment variables); besides some resource waste, this had no negative effect. When introducing snapshots in the picture, repeatedly unpacking the same snapshot to consume the same data is much more wasteful. So it makes sense now to introduce explicitly the concept of dependent context. To move forward and make the ownership more explicit, we add a new function `NewWithContext` in all the subpackages. The function `NewWithContext` will assume the context it gets as argument is ready to be consumed and it will use as-is, attempting no setup or teardown in any circumstances. The usage rules are pretty simple: 1. In your client code, you most likely just need to use `New` as usual. 2. When consuming a package from another (e.g. `toppology` from `pci`), always use the `NewWithContext` function to get a handle for the auxiliary package, passin through the context from the top-level package. 3. Within a `NewWithContext` function, ally calls to other `NewWithContext` functions should be made to preserve the properties. Signed-off-by: Francesco Romani <fromani@redhat.com>
c455327 to
691659d
Compare
jaypipes
added a commit
that referenced
this pull request
Sep 27, 2021
This patch simplifies some internal-only code that was added to ghw to
prevent re-creating Context objects inefficiently during snapshot
creation. We add a `context.WithContext` function that returns an
`option.Option` containing a pre-existing `Context` struct pointer,
embedded in the `Option` as a raw `interface{}` to get around package
recursive import issues.
Now, when packages like `pkg/gpu` or `pkg/pci` need to load the
`pkg/topology` package, they call
`topology.New(context.WithContext(ctx))`, passing in the
already-existing `Context` object. This preserves the original `New`
function signature without adding a new `NewWithContext` function
signature to all packages.
Alternative to #278
jaypipes
added a commit
that referenced
this pull request
Sep 27, 2021
This patch simplifies some internal-only code that was added to ghw to
prevent re-creating Context objects inefficiently during snapshot
creation. We add a `context.WithContext` function that returns an
`option.Option` containing a pre-existing `Context` struct pointer,
embedded in the `Option` as a raw `interface{}` to get around package
recursive import issues.
Now, when packages like `pkg/gpu` or `pkg/pci` need to load the
`pkg/topology` package, they call
`topology.New(context.WithContext(ctx))`, passing in the
already-existing `Context` object. This preserves the original `New`
function signature without adding a new `NewWithContext` function
signature to all packages.
Alternative to #278
jaypipes
added a commit
that referenced
this pull request
Sep 27, 2021
This patch simplifies some internal-only code that was added to ghw to
prevent re-creating Context objects inefficiently during snapshot
creation. We add a `context.WithContext` function that returns an
`option.Option` containing a pre-existing `Context` struct pointer,
embedded in the `Option` as a raw `interface{}` to get around package
recursive import issues.
Now, when packages like `pkg/gpu` or `pkg/pci` need to load the
`pkg/topology` package, they call
`topology.New(context.WithContext(ctx))`, passing in the
already-existing `Context` object. This preserves the original `New`
function signature without adding a new `NewWithContext` function
signature to all packages.
Alternative to #278
Signed-off-by: Jay Pipes <jaypipes@gmail.com>
jaypipes
added a commit
that referenced
this pull request
Sep 29, 2021
This patch simplifies some internal-only code that was added to ghw to
prevent re-creating Context objects inefficiently during snapshot
creation. We add a `context.WithContext` function that returns an
`option.Option` containing a pre-existing `Context` struct pointer,
embedded in the `Option` as a raw `interface{}` to get around package
recursive import issues.
Now, when packages like `pkg/gpu` or `pkg/pci` need to load the
`pkg/topology` package, they call
`topology.New(context.WithContext(ctx))`, passing in the
already-existing `Context` object. This preserves the original `New`
function signature without adding a new `NewWithContext` function
signature to all packages.
Alternative to #278
Signed-off-by: Jay Pipes <jaypipes@gmail.com>
Collaborator
Author
|
replaced by #280 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The PR #236 fixes a snapshot directory leakage issue, but also highlight a context lifecycle/ownership issue.
In general in ghw each package creates its own context, including the cases on which a package is consumed by another, for example
topologyandpci.However, in this case, it would make more sense to reuse the context from the parent package.
Before the introduction of the the transparent snapshot support (#202), the above mechanism worked,
because each context, each time, was consuming over and over again the same parameters (e.g. environment variables); besides some resource waste, this had no negative effect.
When introducing snapshots in the picture, repeatedly unpacking the same snapshot to consume the same data is much more wasteful. So it makes sense now to introduce explicitly the concept of dependent context.
To move forward and make the ownership more explicit, we add a new function
NewWithContextin all the subpackages.The function
NewWithContextwill assume the context it gets as argument is ready to be consumed and it will use as-is, attempting no setup or teardown in any circumstances.The usage rules are pretty simple:
Newas usual.topologyfrompci), always use theNewWithContextfunction to get a handle for the auxiliary package, passing through the context from the top-level package.NewWithContextfunction, all calls to otherNewWithContextfunctions should be made to preserve the properties.