|
|
@ -26,6 +26,7 @@ type requestManager struct { |
|
|
|
waitings requestQueue // double linked list of requests that are on the waiting list
|
|
|
|
waitings requestQueue // double linked list of requests that are on the waiting list
|
|
|
|
|
|
|
|
|
|
|
|
// Stream events
|
|
|
|
// Stream events
|
|
|
|
|
|
|
|
sm streammanager.Reader |
|
|
|
newStreamC <-chan streammanager.EvtStreamAdded |
|
|
|
newStreamC <-chan streammanager.EvtStreamAdded |
|
|
|
rmStreamC <-chan streammanager.EvtStreamRemoved |
|
|
|
rmStreamC <-chan streammanager.EvtStreamRemoved |
|
|
|
// Request events
|
|
|
|
// Request events
|
|
|
@ -40,11 +41,11 @@ type requestManager struct { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewRequestManager creates a new request manager
|
|
|
|
// NewRequestManager creates a new request manager
|
|
|
|
func NewRequestManager(sm streammanager.Subscriber) RequestManager { |
|
|
|
func NewRequestManager(sm streammanager.ReaderSubscriber) RequestManager { |
|
|
|
return newRequestManager(sm) |
|
|
|
return newRequestManager(sm) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newRequestManager(sm streammanager.Subscriber) *requestManager { |
|
|
|
func newRequestManager(sm streammanager.ReaderSubscriber) *requestManager { |
|
|
|
// subscribe at initialize to prevent misuse of upper function which might cause
|
|
|
|
// subscribe at initialize to prevent misuse of upper function which might cause
|
|
|
|
// the bootstrap peers are ignored
|
|
|
|
// the bootstrap peers are ignored
|
|
|
|
newStreamC := make(chan streammanager.EvtStreamAdded) |
|
|
|
newStreamC := make(chan streammanager.EvtStreamAdded) |
|
|
@ -60,6 +61,7 @@ func newRequestManager(sm streammanager.Subscriber) *requestManager { |
|
|
|
pendings: make(map[uint64]*request), |
|
|
|
pendings: make(map[uint64]*request), |
|
|
|
waitings: newRequestQueue(), |
|
|
|
waitings: newRequestQueue(), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sm: sm, |
|
|
|
newStreamC: newStreamC, |
|
|
|
newStreamC: newStreamC, |
|
|
|
rmStreamC: rmStreamC, |
|
|
|
rmStreamC: rmStreamC, |
|
|
|
cancelReqC: make(chan cancelReqData, 16), |
|
|
|
cancelReqC: make(chan cancelReqData, 16), |
|
|
@ -182,13 +184,11 @@ func (rm *requestManager) loop() { |
|
|
|
case data := <-rm.cancelReqC: |
|
|
|
case data := <-rm.cancelReqC: |
|
|
|
rm.handleCancelRequest(data) |
|
|
|
rm.handleCancelRequest(data) |
|
|
|
|
|
|
|
|
|
|
|
case evt := <-rm.newStreamC: |
|
|
|
case <-rm.newStreamC: |
|
|
|
rm.logger.Info().Str("streamID", string(evt.Stream.ID())).Msg("add new stream") |
|
|
|
rm.refreshStreams() |
|
|
|
rm.addNewStream(evt.Stream) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case evt := <-rm.rmStreamC: |
|
|
|
case <-rm.rmStreamC: |
|
|
|
rm.logger.Info().Str("streamID", string(evt.ID)).Msg("remove stream") |
|
|
|
rm.refreshStreams() |
|
|
|
rm.removeStream(evt.ID) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case <-rm.stopC: |
|
|
|
case <-rm.stopC: |
|
|
|
rm.logger.Info().Msg("request manager stopped") |
|
|
|
rm.logger.Info().Msg("request manager stopped") |
|
|
@ -349,26 +349,49 @@ func (rm *requestManager) pickAvailableStream(req *request) (*stream, error) { |
|
|
|
return nil, errors.New("no more available streams") |
|
|
|
return nil, errors.New("no more available streams") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (rm *requestManager) addNewStream(st sttypes.Stream) { |
|
|
|
func (rm *requestManager) refreshStreams() { |
|
|
|
rm.lock.Lock() |
|
|
|
rm.lock.Lock() |
|
|
|
defer rm.lock.Unlock() |
|
|
|
defer rm.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
if _, ok := rm.streams[st.ID()]; !ok { |
|
|
|
added, removed := checkStreamUpdates(rm.streams, rm.sm.GetStreams()) |
|
|
|
rm.streams[st.ID()] = &stream{Stream: st} |
|
|
|
|
|
|
|
rm.available[st.ID()] = struct{}{} |
|
|
|
for _, st := range added { |
|
|
|
|
|
|
|
rm.logger.Info().Str("streamID", string(st.ID())).Msg("add new stream") |
|
|
|
|
|
|
|
rm.addNewStream(st) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for _, st := range removed { |
|
|
|
|
|
|
|
rm.logger.Info().Str("streamID", string(st.ID())).Msg("remove stream") |
|
|
|
|
|
|
|
rm.removeStream(st) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// removeStream remove the stream from request manager, clear the pending request
|
|
|
|
func checkStreamUpdates(exists map[sttypes.StreamID]*stream, targets []sttypes.Stream) (added []sttypes.Stream, removed []*stream) { |
|
|
|
// of the stream. Return whether a pending request is canceled in the stream,
|
|
|
|
targetM := make(map[sttypes.StreamID]sttypes.Stream) |
|
|
|
func (rm *requestManager) removeStream(id sttypes.StreamID) { |
|
|
|
|
|
|
|
rm.lock.Lock() |
|
|
|
|
|
|
|
defer rm.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st, ok := rm.streams[id] |
|
|
|
for _, target := range targets { |
|
|
|
if !ok { |
|
|
|
id := target.ID() |
|
|
|
return |
|
|
|
targetM[id] = target |
|
|
|
|
|
|
|
if _, ok := exists[id]; !ok { |
|
|
|
|
|
|
|
added = append(added, target) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for id, exist := range exists { |
|
|
|
|
|
|
|
if _, ok := targetM[id]; !ok { |
|
|
|
|
|
|
|
removed = append(removed, exist) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (rm *requestManager) addNewStream(st sttypes.Stream) { |
|
|
|
|
|
|
|
rm.streams[st.ID()] = &stream{Stream: st} |
|
|
|
|
|
|
|
rm.available[st.ID()] = struct{}{} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// removeStream remove the stream from request manager, clear the pending request
|
|
|
|
|
|
|
|
// of the stream.
|
|
|
|
|
|
|
|
func (rm *requestManager) removeStream(st *stream) { |
|
|
|
|
|
|
|
id := st.ID() |
|
|
|
delete(rm.available, id) |
|
|
|
delete(rm.available, id) |
|
|
|
delete(rm.streams, id) |
|
|
|
delete(rm.streams, id) |
|
|
|
|
|
|
|
|
|
|
|