ipfs / ipfs/kubo

Abstract Sessions

Open
#6,525 0 comments 2 reactions 0 assignees View on GitHub
kind/enhancement
Dominant language
Go
Stars
17.1k
Forks
3.2k
Avg merge
3d 18h
Merged PRs (30d)
11

Description

Currently, bitswap has a concept of "sessions" for related requests. However:

1. Using usually means reconstructing the storage stack starting at the exchange. I.e., make the session, create a new blockservice, wrap it in a dagservice, etc.
2. Sessions are hard to compose. We currently handle this by creating the session in the top-level command but it would be nice if commands could recursively "create" sessions kind of like a re-entrant mutex.
3. Sessions are specific to the exchange, even when we have multiple components that should all _logically_ be associated with the session.

Proposal: Extract the session concept from bitswap and stash the session handle in the _context_. Ideally, sessions would be generic peer/request trackers that track how useful each connected peer is over the course of the session.

Pros:

* We can share session information between subsystems.
* We can pre-seed these sessions with additional information (e.g. providers) from the request.
* Sessions become entirely implicit. We only need to care about them at the _very_ top level where we decide that we're making an independent request.

Cons:

* Will take some refactoring. We can make a simple version that just stashes a shared ID in the context and then slowly move from there.

---

Basic Interface:

```go
// Begin begins a new session if there are no sessions associated with the context.
func Begin(ctx context.Context) (context.Context, context.CancelFunc) {}

// Leave dissociates the context with the current session.
func Leave(ctx context.Context) (context.Context) {}

// Fork forks off a new session, inheriting the state of the current session.
// Ok, we can probably leave this till later...
func Fork(ctx context.Context) (context.Context, context.CancelFunc) {}

// GetSession returns the session associated with this context, if any.
func GetSession(ctx context.Context) Session {}

type Session interface {
func AddPeer(pi AddrInfo) // or maybe just a peer ID?
// Eventually, this will contain other peer-management methods.
}
```

Usage:

```go
ctx, cancel := session.Begin(ctx)
defer cancel()

block, err := myDag.Get(ctx, thing)

// look ma, no sessions!
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.