sessionmanager.go 2.89 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 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
type sesTrk struct {
23 24
	session Session
	pm      bssession.PeerManager
25 26
}

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

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

33 34
// SessionManager is responsible for creating, managing, and dispatching to
// sessions.
35
type SessionManager struct {
36 37 38
	ctx                context.Context
	sessionFactory     SessionFactory
	peerManagerFactory PeerManagerFactory
39 40
	// Sessions
	sessLk   sync.Mutex
41
	sessions []sesTrk
42 43 44 45 46 47

	// Session Index
	sessIDLk sync.Mutex
	sessID   uint64
}

48
// New creates a new SessionManager.
49
func New(ctx context.Context, sessionFactory SessionFactory, peerManagerFactory PeerManagerFactory) *SessionManager {
50
	return &SessionManager{
51 52 53
		ctx:                ctx,
		sessionFactory:     sessionFactory,
		peerManagerFactory: peerManagerFactory,
54
	}
55 56
}

57 58 59 60 61 62
// 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)

63 64
	pm := sm.peerManagerFactory(sessionctx, id)
	session := sm.sessionFactory(sessionctx, id, pm)
65
	tracked := sesTrk{session, pm}
66
	sm.sessLk.Lock()
67
	sm.sessions = append(sm.sessions, tracked)
68
	sm.sessLk.Unlock()
69
	go func() {
hannahhoward's avatar
hannahhoward committed
70 71 72
		defer cancel()
		select {
		case <-sm.ctx.Done():
73
			sm.removeSession(tracked)
hannahhoward's avatar
hannahhoward committed
74
		case <-ctx.Done():
75
			sm.removeSession(tracked)
76 77 78 79
		}
	}()

	return session
80 81
}

82
func (sm *SessionManager) removeSession(session sesTrk) {
83 84 85 86 87 88 89 90 91 92 93
	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
		}
	}
}

94
// GetNextSessionID returns the next sequentional identifier for a session.
95 96 97 98 99 100 101
func (sm *SessionManager) GetNextSessionID() uint64 {
	sm.sessIDLk.Lock()
	defer sm.sessIDLk.Unlock()
	sm.sessID++
	return sm.sessID
}

102 103 104
// ReceiveBlockFrom receives a block from a peer and dispatches to interested
// sessions.
func (sm *SessionManager) ReceiveBlockFrom(from peer.ID, blk blocks.Block) {
105 106 107
	sm.sessLk.Lock()
	defer sm.sessLk.Unlock()

108
	k := blk.Cid()
109
	for _, s := range sm.sessions {
110 111
		if s.session.InterestedIn(k) {
			s.session.ReceiveBlockFrom(from, blk)
112
		}
113 114
	}
}