Skip to content

Commit bf4947a

Browse files
committed
session: add new session package
1 parent 4c3a1f2 commit bf4947a

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

session/session.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// session implements a finality independent session system. It let session producers transparently integrate with any session consumer without prequired shared state.
2+
// A session bundles a logical set of related requests, it let download clients more efficiently send requests in the network, the assumption is that the same nodes should host most of the data in the related requests.
3+
// To create a new session use [ContextWithSession], compatible consumers like bitswap will automatically pick it up from the context.
4+
package session
5+
6+
import (
7+
"context"
8+
"sync"
9+
)
10+
11+
type contextKey struct{}
12+
13+
// ContextWithSession attach a session to the context, it may reuse an existing session if one already exists in the context.
14+
// The context must be canceled in some way to ensure all sessions are cleaned up.
15+
func ContextWithSession(ctx context.Context) context.Context {
16+
if s := ctx.Value(contextKey{}); s != nil {
17+
if _, ok := s.(*sessionRegistry); ok {
18+
return ctx
19+
}
20+
}
21+
22+
return context.WithValue(ctx, contextKey{}, &sessionRegistry{sesCtx: ctx})
23+
}
24+
25+
// sessionRegistry is meant for session consumers.
26+
// This type allows session consumers to inject their own session object within an existing context.
27+
// This exists as it allows to satisfy theses two requirements:
28+
// - Session producers must not need to hold some prexisting session factory object to inject into their contextes.
29+
// - Session consumers must be able to not to maintain any state to track inflight sessions, instead they can store their session object in the sessionRegistry and rely on GC to cleanup.
30+
// It is threadsafe.
31+
type sessionRegistry struct {
32+
sesCtx context.Context
33+
lk sync.Mutex // protects sessions
34+
sessions map[any]any
35+
}
36+
37+
func getRegistryFromContext(ctx context.Context) (_ *sessionRegistry, ok bool) {
38+
r := ctx.Value(contextKey{})
39+
if r == nil {
40+
return nil, false
41+
}
42+
43+
rr, ok := r.(*sessionRegistry)
44+
return rr, ok
45+
}
46+
47+
// Get attempt to fetch the session for the producer.
48+
func Get[P comparable, S any](ctx context.Context, producer P) (session S, ok bool) {
49+
r, ok := getRegistryFromContext(ctx)
50+
if !ok {
51+
return
52+
}
53+
54+
r.lk.Lock()
55+
s, ok := r.sessions[producer]
56+
r.lk.Unlock()
57+
if !ok {
58+
return
59+
}
60+
61+
ss, ok := s.(S)
62+
return ss, ok
63+
}
64+
65+
// GetOrCreate atomically fetch a session from the context, if none is present the create function is invoked and saved in the context.
66+
// All contextes scoped to the first [ContextWithSession] will be able to reuse this session.
67+
// create must not block.
68+
// sessionContext is scoped to the session, it allows consumers to cleanup session scoped goroutines or other similar things.
69+
func GetOrCreate[P comparable, S any](ctx context.Context, producer P, create func(sessionContext context.Context) (session S, ok bool)) (session S, ok bool) {
70+
r, ok := getRegistryFromContext(ctx)
71+
if !ok {
72+
return
73+
}
74+
75+
r.lk.Lock()
76+
defer r.lk.Unlock()
77+
s, ok := r.sessions[producer]
78+
if ok {
79+
if ss, ok := s.(S); ok {
80+
return ss, true
81+
}
82+
// Overwrite with a new session if types don't match ?
83+
}
84+
85+
ss, ok := create(r.sesCtx)
86+
if !ok {
87+
return
88+
}
89+
90+
if r.sessions == nil {
91+
r.sessions = make(map[any]any, 1)
92+
}
93+
r.sessions[producer] = ss
94+
95+
return ss, true
96+
}

0 commit comments

Comments
 (0)