Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

switch to storing sessions in the context #9

Closed
wants to merge 2 commits into from

Conversation

Stebalien
Copy link
Member

This patch allows associating a "session key" with a context by calling NewSession(ctx). When the session is used in a request, the exchange can call GetSession(ctx) to make the request in the appropriate session, or create a new session with the given session key.

Implements ipfs/kubo#7198

@Stebalien Stebalien force-pushed the feat/context-sessions branch 4 times, most recently from 0a60364 to ccb5de3 Compare April 23, 2020 05:11
This patch allows associating a "session key" with a context by calling
`NewSession(ctx)`. When the session is used in a request, the exchange can call
`GetOrCreateSession(ctx)` to make the request in the appropriate session, or create a
new session with the given session key.

Implements ipfs/kubo#7198
// session should be stopped when this context is canceled.
func GetOrCreateSession(ctx context.Context) (SessionID, context.Context) {
if s, ok := ctx.Value(sessionContextKey{}).(*sessionContextValue); ok {
return s.sesID, s.sesCtx

Choose a reason for hiding this comment

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

the semantics between this returning s.sesCtx vs NewSession above returning the queried ctx feels non-obvious.
If they're called within an existing session, NewSession will return the child context with additional cancel/timeout/values applied while GetOrCreateSession will return a parent context back up at the full session scope.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, we need to explain this better.

Basically, the lifetime of the session is the lifetime of the context where the session was created. That's the context we return here.

However, NewSession is supposed to start a new session on the context, if it doesn't already exist. In that case, we want the child context because we want to cancel the request when the context is canceled. However, if we do have a child context, we don't want to cancel the entire session.

Copy link

@willscott willscott left a comment

Choose a reason for hiding this comment

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

A test would be useful to validate expected use of this. Structurally, looks good.

@Stebalien
Copy link
Member Author

You're right, we definitely need tests. I've had to go through several very confusing iterations trying to get this right.

interface.go Outdated Show resolved Hide resolved
Co-Authored-By: Ian Lopshire <[email protected]>
@aschmahmann
Copy link

This repository is no longer maintained and has been copied over to Boxo. In an effort to avoid noise and crippling in the Boxo repo from the weight of issues of the past, we are closing most issues and PRs in this repo. Please feel free to open a new issue in Boxo (and reference this PR) if resolving this issue is still critical for unblocking or improving your usecase.

You can learn more in the FAQs for the Boxo repo copying/consolidation effort.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants