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

import (
4
	"context"
5
	"sync"
6
	"time"
7

8 9
	blocks "github.com/ipfs/go-block-format"
	cid "github.com/ipfs/go-cid"
10
	delay "github.com/ipfs/go-ipfs-delay"
11 12

	bssession "github.com/ipfs/go-bitswap/session"
13
	exchange "github.com/ipfs/go-ipfs-exchange-interface"
Raúl Kripalani's avatar
Raúl Kripalani committed
14
	peer "github.com/libp2p/go-libp2p-core/peer"
15 16
)

17 18 19 20 21
// 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)
22
	UpdateReceiveCounters(peer.ID, blocks.Block)
23 24
}

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

31
// SessionFactory generates a new session for the SessionManager to track.
32
type SessionFactory func(ctx context.Context, id uint64, pm bssession.PeerManager, srs bssession.RequestSplitter, provSearchDelay time.Duration, rebroadcastDelay delay.D) Session
33 34 35

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

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

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

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

	// Session Index
	sessIDLk sync.Mutex
	sessID   uint64
}

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

67 68
// NewSession initializes a session with the given context, and adds to the
// session manager.
69 70 71
func (sm *SessionManager) NewSession(ctx context.Context,
	provSearchDelay time.Duration,
	rebroadcastDelay delay.D) exchange.Fetcher {
72 73 74
	id := sm.GetNextSessionID()
	sessionctx, cancel := context.WithCancel(ctx)

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

	return session
93 94
}

95
func (sm *SessionManager) removeSession(session sesTrk) {
96 97 98 99 100 101 102 103 104 105 106
	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
		}
	}
}

107
// GetNextSessionID returns the next sequentional identifier for a session.
108 109 110 111 112 113 114
func (sm *SessionManager) GetNextSessionID() uint64 {
	sm.sessIDLk.Lock()
	defer sm.sessIDLk.Unlock()
	sm.sessID++
	return sm.sessID
}

115 116 117
// ReceiveBlockFrom receives a block from a peer and dispatches to interested
// sessions.
func (sm *SessionManager) ReceiveBlockFrom(from peer.ID, blk blocks.Block) {
118 119 120
	sm.sessLk.Lock()
	defer sm.sessLk.Unlock()

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

// UpdateReceiveCounters records the fact that a block was received, allowing
// sessions to track duplicates
131
func (sm *SessionManager) UpdateReceiveCounters(from peer.ID, blk blocks.Block) {
132 133 134 135
	sm.sessLk.Lock()
	defer sm.sessLk.Unlock()

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