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

Dependency context.Context #31

Merged
merged 4 commits into from
Sep 25, 2023
Merged

Conversation

SimonRichardson
Copy link
Member

@SimonRichardson SimonRichardson commented Sep 13, 2023

The following reworks the dependency engine to bring in a real
context.Context. The fallout of this means that we decouple the
concept of cancelation and where you get the dependency from. In
doing so, context has become snapshot. The original comments
around this type just state that it is a snapshot, so we might as
well just call it that.

The dependency Context becomes split. Removing the cross-cutting
concerns. The aborting of a worker in the start now becomes
context.Context and the retrieval of a dependency are now a Container.

The context creation is now tied to the lifecycle of the tomb. Doing
this ensures that when the engine is killed, then the context is also
killed. It is worth noting that cancel is idempotent so, calling it multiple
times does nothing after the first call (unlike channels). As the main
loop on tomb death is also attempting to cancel the context of the
snapshot worker, it essentially becomes a no-op.

JUJU-4623

The following reworks the dependency engine to bring in a real
context.Context. The fallout of this means that we decouple the
concept of cancelation and where you get the dependency from. In
doing so, context has become snapshot. The original comments
around this type just state that it is a snapshot, so we might as
well just call it that.
The dependency Context becomes split. Removing the cross cutting
concerns. The aborting of a worker in the start now becomes
context.Context and the retrival of a dependency is now a Container.
Currently we're just creating a adhoc context.Context, but in the
next commit, we'll tie this to the tomb directly, ensuring that upon
destruction of the engine, all manifolds will be killed.
The context creation is now tied to the lifecycle of the tomb. Doing
this ensures that when the engine is killed, then the context is also
killed. It worth noting that cancel is idempotent so, calling it multiple
times does nothing. As the main loop on tomb death is also attempting
to cancel the context of the snapshot worker, it essentially becomes a
no-op.
Copy link
Member

@jameinel jameinel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I like the replacement of dependency.Context with dependency.Container.
what about:
dependency.Getter
dependency.Dependencies
dependency.Others
dependency.Snapshot
?

// the tomb.
func (engine *Engine) scopedContext() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
return engine.tomb.Context(ctx), cancel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading the engine.tomb.Context() documentation, wouldn't it make more sense to just use engine.tomb.Context() ?
I guess you're trying to get access to its cancel function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

dependency/engine_test.go Outdated Show resolved Hide resolved
Renaming Container (DI) to Getter to prevent any confusion over what
a container is in this context.
This is a breaking change, so we should bump from v3 to v4.
@SimonRichardson
Copy link
Member Author

/merge

@jujubot jujubot merged commit 1ea0a7d into juju:master Sep 25, 2023
1 of 2 checks passed
@SimonRichardson SimonRichardson deleted the dependency-context branch September 25, 2023 13:56
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.

3 participants