Skip to content

context: allow reuse in auxiliary packages#278

Closed
ffromani wants to merge 2 commits into
jaypipes:mainfrom
ffromani:context-reuse
Closed

context: allow reuse in auxiliary packages#278
ffromani wants to merge 2 commits into
jaypipes:mainfrom
ffromani:context-reuse

Conversation

@ffromani

@ffromani ffromani commented Sep 21, 2021

Copy link
Copy Markdown
Collaborator

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 topology and pci.
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 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. topology from pci), always use the NewWithContext function to get a handle for the auxiliary package, passing through the context from the top-level package.
  3. Within a NewWithContext function, all calls to other NewWithContext functions should be made to preserve the properties.

@ffromani ffromani mentioned this pull request Sep 21, 2021
@ffromani ffromani changed the title Context reuse allow context reuse Sep 21, 2021
@ffromani ffromani requested a review from jaypipes September 21, 2021 11:25
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>
@ffromani ffromani changed the title allow context reuse context: allow reuse in auxiliary packages Sep 24, 2021
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>
@ffromani

Copy link
Copy Markdown
Collaborator Author

replaced by #280

@ffromani ffromani closed this Sep 29, 2021
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 this pull request may close these issues.

1 participant