Skip to content

Commit d8d7675

Browse files
committed
blockservice: remove session embeding in context
This brings us to a state before #549 back then I also did cleanup in this session code, that I have kept, I only removed the sessions in context feature.
1 parent 4b8ade1 commit d8d7675

File tree

4 files changed

+0
-127
lines changed

4 files changed

+0
-127
lines changed

CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ The following emojis are used to highlight certain changes:
1616

1717
### Added
1818

19-
- `blockservice` now has `ContextWithSession` and `EmbedSessionInContext` functions, which allows to embed a session in a context. Future calls to `BlockGetter.GetBlock`, `BlockGetter.GetBlocks` and `NewSession` will use the session in the context.
2019
- `blockservice.NewWritethrough` deprecated function has been removed, instead you can do `blockservice.New(..., ..., WriteThrough())` like previously.
2120
- `gateway`: a new header configuration middleware has been added to replace the existing header configuration, which can be used more generically.
2221
- `namesys` now has a `WithMaxCacheTTL` option, which allows you to define a maximum TTL that will be used for caching IPNS entries.

blockservice/blockservice.go

-55
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,6 @@ func (s *blockService) Allowlist() verifcid.Allowlist {
140140
// directly.
141141
// Sessions are lazily setup, this is cheap.
142142
func NewSession(ctx context.Context, bs BlockService) *Session {
143-
ses := grabSessionFromContext(ctx, bs)
144-
if ses != nil {
145-
return ses
146-
}
147-
148-
return newSession(ctx, bs)
149-
}
150-
151-
// newSession is like [NewSession] but it does not attempt to reuse session from the existing context.
152-
func newSession(ctx context.Context, bs BlockService) *Session {
153143
return &Session{bs: bs, sesctx: ctx}
154144
}
155145

@@ -232,10 +222,6 @@ func (s *blockService) AddBlocks(ctx context.Context, bs []blocks.Block) error {
232222
// GetBlock retrieves a particular block from the service,
233223
// Getting it from the datastore using the key (hash).
234224
func (s *blockService) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
235-
if ses := grabSessionFromContext(ctx, s); ses != nil {
236-
return ses.GetBlock(ctx, c)
237-
}
238-
239225
ctx, span := internal.StartSpan(ctx, "blockService.GetBlock", trace.WithAttributes(attribute.Stringer("CID", c)))
240226
defer span.End()
241227

@@ -295,10 +281,6 @@ func getBlock(ctx context.Context, c cid.Cid, bs BlockService, fetchFactory func
295281
// the returned channel.
296282
// NB: No guarantees are made about order.
297283
func (s *blockService) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block {
298-
if ses := grabSessionFromContext(ctx, s); ses != nil {
299-
return ses.GetBlocks(ctx, ks)
300-
}
301-
302284
ctx, span := internal.StartSpan(ctx, "blockService.GetBlocks")
303285
defer span.End()
304286

@@ -474,43 +456,6 @@ func (s *Session) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Blo
474456

475457
var _ BlockGetter = (*Session)(nil)
476458

477-
// ContextWithSession is a helper which creates a context with an embded session,
478-
// future calls to [BlockGetter.GetBlock], [BlockGetter.GetBlocks] and [NewSession] with the same [BlockService]
479-
// will be redirected to this same session instead.
480-
// Sessions are lazily setup, this is cheap.
481-
// It wont make a new session if one exists already in the context.
482-
func ContextWithSession(ctx context.Context, bs BlockService) context.Context {
483-
if grabSessionFromContext(ctx, bs) != nil {
484-
return ctx
485-
}
486-
return EmbedSessionInContext(ctx, NewSession(ctx, bs))
487-
}
488-
489-
// EmbedSessionInContext is like [ContextWithSession] but it allows to embed an existing session.
490-
func EmbedSessionInContext(ctx context.Context, ses *Session) context.Context {
491-
// use ses.bs as a key, so if multiple blockservices use embeded sessions it gets dispatched to the matching blockservice.
492-
return context.WithValue(ctx, ses.bs, ses)
493-
}
494-
495-
// grabSessionFromContext returns nil if the session was not found
496-
// This is a private API on purposes, I dislike when consumers tradeoff compiletime typesafety with runtime typesafety,
497-
// if this API is public it is too easy to forget to pass a [BlockService] or [Session] object around in your app.
498-
// By having this private we allow consumers to follow the trace of where the blockservice is passed and used.
499-
func grabSessionFromContext(ctx context.Context, bs BlockService) *Session {
500-
s := ctx.Value(bs)
501-
if s == nil {
502-
return nil
503-
}
504-
505-
ss, ok := s.(*Session)
506-
if !ok {
507-
// idk what to do here, that kinda sucks, giveup
508-
return nil
509-
}
510-
511-
return ss
512-
}
513-
514459
// grabAllowlistFromBlockservice never returns nil
515460
func grabAllowlistFromBlockservice(bs BlockService) verifcid.Allowlist {
516461
if bbs, ok := bs.(BoundedBlockService); ok {

blockservice/blockservice_test.go

-65
Original file line numberDiff line numberDiff line change
@@ -288,68 +288,3 @@ func TestAllowlist(t *testing.T) {
288288
check(blockservice.GetBlock)
289289
check(NewSession(ctx, blockservice).GetBlock)
290290
}
291-
292-
type fakeIsNewSessionCreateExchange struct {
293-
ses exchange.Fetcher
294-
newSessionWasCalled bool
295-
}
296-
297-
var _ exchange.SessionExchange = (*fakeIsNewSessionCreateExchange)(nil)
298-
299-
func (*fakeIsNewSessionCreateExchange) Close() error {
300-
return nil
301-
}
302-
303-
func (*fakeIsNewSessionCreateExchange) GetBlock(context.Context, cid.Cid) (blocks.Block, error) {
304-
panic("should call on the session")
305-
}
306-
307-
func (*fakeIsNewSessionCreateExchange) GetBlocks(context.Context, []cid.Cid) (<-chan blocks.Block, error) {
308-
panic("should call on the session")
309-
}
310-
311-
func (f *fakeIsNewSessionCreateExchange) NewSession(context.Context) exchange.Fetcher {
312-
f.newSessionWasCalled = true
313-
return f.ses
314-
}
315-
316-
func (*fakeIsNewSessionCreateExchange) NotifyNewBlocks(context.Context, ...blocks.Block) error {
317-
return nil
318-
}
319-
320-
func TestContextSession(t *testing.T) {
321-
t.Parallel()
322-
a := assert.New(t)
323-
324-
ctx, cancel := context.WithCancel(context.Background())
325-
defer cancel()
326-
327-
bgen := butil.NewBlockGenerator()
328-
block1 := bgen.Next()
329-
block2 := bgen.Next()
330-
331-
bs := blockstore.NewBlockstore(ds.NewMapDatastore())
332-
a.NoError(bs.Put(ctx, block1))
333-
a.NoError(bs.Put(ctx, block2))
334-
sesEx := &fakeIsNewSessionCreateExchange{ses: offline.Exchange(bs)}
335-
336-
service := New(blockstore.NewBlockstore(ds.NewMapDatastore()), sesEx)
337-
338-
ctx = ContextWithSession(ctx, service)
339-
340-
b, err := service.GetBlock(ctx, block1.Cid())
341-
a.NoError(err)
342-
a.Equal(b.RawData(), block1.RawData())
343-
a.True(sesEx.newSessionWasCalled, "new session from context should be created")
344-
sesEx.newSessionWasCalled = false
345-
346-
bchan := service.GetBlocks(ctx, []cid.Cid{block2.Cid()})
347-
a.Equal((<-bchan).RawData(), block2.RawData())
348-
a.False(sesEx.newSessionWasCalled, "session should be reused in context")
349-
350-
a.Equal(
351-
NewSession(ctx, service),
352-
NewSession(ContextWithSession(ctx, service), service),
353-
"session must be deduped in all invocations on the same context",
354-
)
355-
}

gateway/blocks_backend.go

-6
Original file line numberDiff line numberDiff line change
@@ -689,12 +689,6 @@ func (bb *BlocksBackend) IsCached(ctx context.Context, p path.Path) bool {
689689
return has
690690
}
691691

692-
var _ WithContextHint = (*BlocksBackend)(nil)
693-
694-
func (bb *BlocksBackend) WrapContextForRequest(ctx context.Context) context.Context {
695-
return blockservice.ContextWithSession(ctx, bb.blockService)
696-
}
697-
698692
func (bb *BlocksBackend) ResolvePath(ctx context.Context, path path.ImmutablePath) (ContentPathMetadata, error) {
699693
roots, lastSeg, remainder, err := bb.getPathRoots(ctx, path)
700694
if err != nil {

0 commit comments

Comments
 (0)