Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: scheduler channel blocking #756

Merged
merged 5 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scheduler/core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func (e startReportPieceResultEvent) apply(s *state) {
}
if err := e.peer.SendSchedulePacket(constructSuccessPeerPacket(e.peer, parent, candidates)); err != nil {
e.peer.Log().Warnf("send schedule packet failed: %v", err)
s.waitScheduleParentPeerQueue.AddAfter(&rsPeer{peer: e.peer}, 10*time.Millisecond)
}
}

Expand Down
36 changes: 21 additions & 15 deletions scheduler/supervisor/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,13 @@ func (peer *Peer) Log() *logger.SugaredLoggerOnWith {
}

type Channel struct {
startOnce sync.Once
sender chan *scheduler.PeerPacket
receiver chan *scheduler.PieceResult
stream scheduler.Scheduler_ReportPieceResultServer
closed *atomic.Bool
done chan struct{}
wg sync.WaitGroup
err error
sender chan *scheduler.PeerPacket
receiver chan *scheduler.PieceResult
stream scheduler.Scheduler_ReportPieceResultServer
closed *atomic.Bool
done chan struct{}
wg sync.WaitGroup
err error
}

func newChannel(stream scheduler.Scheduler_ReportPieceResultServer) *Channel {
Expand All @@ -531,16 +530,19 @@ func newChannel(stream scheduler.Scheduler_ReportPieceResultServer) *Channel {
closed: atomic.NewBool(false),
done: make(chan struct{}),
}

c.wg.Add(2)
c.start()
return c
}

func (c *Channel) start() {
c.startOnce.Do(func() {
c.wg.Add(2)
go c.receiveLoop()
go c.sendLoop()
})
startWG := &sync.WaitGroup{}
startWG.Add(2)

go c.receiveLoop(startWG)
go c.sendLoop(startWG)
startWG.Wait()
}

func (c *Channel) Send(packet *scheduler.PeerPacket) error {
Expand Down Expand Up @@ -583,13 +585,15 @@ func (c *Channel) IsClosed() bool {
return c.closed.Load()
}

func (c *Channel) receiveLoop() {
func (c *Channel) receiveLoop(startWG *sync.WaitGroup) {
defer func() {
close(c.receiver)
c.wg.Done()
c.Close()
}()

startWG.Done()

for {
select {
case <-c.done:
Expand All @@ -608,12 +612,14 @@ func (c *Channel) receiveLoop() {
}
}

func (c *Channel) sendLoop() {
func (c *Channel) sendLoop(startWG *sync.WaitGroup) {
defer func() {
c.wg.Done()
c.Close()
}()

startWG.Done()

for {
select {
case <-c.done:
Expand Down