sessionmanager.go 3.66 KB
Newer Older
1 2 3
package sessionmanager

import (
4
	"context"
5 6
	"sync"

7 8 9 10
	blocks "github.com/ipfs/go-block-format"
	cid "github.com/ipfs/go-cid"

	bssession "github.com/ipfs/go-bitswap/session"
11
	exchange "github.com/ipfs/go-ipfs-exchange-interface"
12
	peer "github.com/libp2p/go-libp2p-peer"
13 14
)

15 16 17 18 19
// Session is a session that is managed by the session manager
type Session interface {
	exchange.Fetcher
	InterestedIn(cid.Cid) bool
	ReceiveBlockFrom(peer.ID, blocks.Block)
20
	UpdateReceiveCounters(blocks.Block)
21 22
}

23
type sesTrk struct {
24 25
	session Session
	pm      bssession.PeerManager
26
	srs     bssession.RequestSplitter
27 28
}

29
// SessionFactory generates a new session for the SessionManager to track.
30 31 32 33
type SessionFactory func(ctx context.Context, id uint64, pm bssession.PeerManager, srs bssession.RequestSplitter) Session

// RequestSplitterFactory generates a new request splitter for a session.
type RequestSplitterFactory func(ctx context.Context) bssession.RequestSplitter
34 35 36 37

// PeerManagerFactory generates a new peer manager for a session.
type PeerManagerFactory func(ctx context.Context, id uint64) bssession.PeerManager

38 39
// SessionManager is responsible for creating, managing, and dispatching to
// sessions.
40
type SessionManager struct {
41 42 43 44 45
	ctx                    context.Context
	sessionFactory         SessionFactory
	peerManagerFactory     PeerManagerFactory
	requestSplitterFactory RequestSplitterFactory

46 47
	// Sessions
	sessLk   sync.Mutex
48
	sessions []sesTrk
49 50 51 52 53 54

	// Session Index
	sessIDLk sync.Mutex
	sessID   uint64
}

55
// New creates a new SessionManager.
56
func New(ctx context.Context, sessionFactory SessionFactory, peerManagerFactory PeerManagerFactory, requestSplitterFactory RequestSplitterFactory) *SessionManager {
57
	return &SessionManager{
58 59 60 61
		ctx:                    ctx,
		sessionFactory:         sessionFactory,
		peerManagerFactory:     peerManagerFactory,
		requestSplitterFactory: requestSplitterFactory,
62
	}
63 64
}

65 66 67 68 69 70
// NewSession initializes a session with the given context, and adds to the
// session manager.
func (sm *SessionManager) NewSession(ctx context.Context) exchange.Fetcher {
	id := sm.GetNextSessionID()
	sessionctx, cancel := context.WithCancel(ctx)

71
	pm := sm.peerManagerFactory(sessionctx, id)
72 73 74
	srs := sm.requestSplitterFactory(sessionctx)
	session := sm.sessionFactory(sessionctx, id, pm, srs)
	tracked := sesTrk{session, pm, srs}
75
	sm.sessLk.Lock()
76
	sm.sessions = append(sm.sessions, tracked)
77
	sm.sessLk.Unlock()
78
	go func() {
hannahhoward's avatar
hannahhoward committed
79 80 81
		defer cancel()
		select {
		case <-sm.ctx.Done():
82
			sm.removeSession(tracked)
hannahhoward's avatar
hannahhoward committed
83
		case <-ctx.Done():
84
			sm.removeSession(tracked)
85 86 87 88
		}
	}()

	return session
89 90
}

91
func (sm *SessionManager) removeSession(session sesTrk) {
92 93 94 95 96 97 98 99 100 101 102
	sm.sessLk.Lock()
	defer sm.sessLk.Unlock()
	for i := 0; i < len(sm.sessions); i++ {
		if sm.sessions[i] == session {
			sm.sessions[i] = sm.sessions[len(sm.sessions)-1]
			sm.sessions = sm.sessions[:len(sm.sessions)-1]
			return
		}
	}
}

103
// GetNextSessionID returns the next sequentional identifier for a session.
104 105 106 107 108 109 110
func (sm *SessionManager) GetNextSessionID() uint64 {
	sm.sessIDLk.Lock()
	defer sm.sessIDLk.Unlock()
	sm.sessID++
	return sm.sessID
}

111 112 113
// ReceiveBlockFrom receives a block from a peer and dispatches to interested
// sessions.
func (sm *SessionManager) ReceiveBlockFrom(from peer.ID, blk blocks.Block) {
114 115 116
	sm.sessLk.Lock()
	defer sm.sessLk.Unlock()

117
	k := blk.Cid()
118
	for _, s := range sm.sessions {
119 120
		if s.session.InterestedIn(k) {
			s.session.ReceiveBlockFrom(from, blk)
121
		}
122 123
	}
}
124 125 126 127 128 129 130 131 132 133 134

// UpdateReceiveCounters records the fact that a block was received, allowing
// sessions to track duplicates
func (sm *SessionManager) UpdateReceiveCounters(blk blocks.Block) {
	sm.sessLk.Lock()
	defer sm.sessLk.Unlock()

	for _, s := range sm.sessions {
		s.session.UpdateReceiveCounters(blk)
	}
}