Skip to content

Commit

Permalink
wip (#16481)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaxim committed Mar 7, 2019
1 parent 8c4643b commit 6784070
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions go/chat/flipmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,25 @@ type FlipManager struct {
globals.Contextified
utils.DebugLabeler

dealer *flip.Dealer
visualizer *FlipVisualizer
clock clockwork.Clock
ri func() chat1.RemoteInterface
shutdownMu sync.Mutex
shutdownCh chan struct{}
forceCh chan struct{}
loadGameCh chan loadGameJob
dealer *flip.Dealer
visualizer *FlipVisualizer
clock clockwork.Clock
ri func() chat1.RemoteInterface
shutdownMu sync.Mutex
shutdownCh chan struct{}
forceCh chan struct{}
loadGameCh chan loadGameJob
maybeInjectCh chan func()

deck string
cardMap map[string]int
cardReverseMap map[int]string

gamesMu sync.Mutex
games *lru.Cache
dirtyGames map[string]chat1.FlipGameID
flipConvs *lru.Cache
gameMsgIDs *lru.Cache
injectLockTab *libkb.LockTable
gamesMu sync.Mutex
games *lru.Cache
dirtyGames map[string]chat1.FlipGameID
flipConvs *lru.Cache
gameMsgIDs *lru.Cache

partMu sync.Mutex
maxConvParticipations int
Expand Down Expand Up @@ -175,7 +175,7 @@ func NewFlipManager(g *globals.Context, ri func() chat1.RemoteInterface) *FlipMa
cardReverseMap: make(map[int]string),
flipConvs: flipConvs,
gameMsgIDs: gameMsgIDs,
injectLockTab: &libkb.LockTable{},
maybeInjectCh: make(chan func(), 2000),
}
dealer := flip.NewDealer(m)
m.dealer = dealer
Expand All @@ -199,6 +199,7 @@ func (m *FlipManager) Start(ctx context.Context, uid gregor1.UID) {
go m.updateLoop(shutdownCh)
go m.notificationLoop(shutdownCh)
go m.loadGameLoop(shutdownCh)
go m.maybeInjectLoop(shutdownCh)
}

func (m *FlipManager) Stop(ctx context.Context) (ch chan struct{}) {
Expand Down Expand Up @@ -1156,6 +1157,19 @@ func (m *FlipManager) updateActiveGame(ctx context.Context, uid gregor1.UID, con
return nil
}

func (m *FlipManager) maybeInjectLoop(shutdownCh chan struct{}) {
m.Debug(context.Background(), "maybeInjectLoop: starting")
for {
select {
case closure := <-m.maybeInjectCh:
closure()
case <-shutdownCh:
m.Debug(context.Background(), "maybeInjectLoop: exiting loop")
return
}
}
}

// MaybeInjectFlipMessage implements the types.CoinFlipManager interface
func (m *FlipManager) MaybeInjectFlipMessage(ctx context.Context, boxedMsg chat1.MessageBoxed,
inboxVers chat1.InboxVers, uid gregor1.UID, convID chat1.ConversationID, topicType chat1.TopicType) bool {
Expand Down Expand Up @@ -1191,19 +1205,17 @@ func (m *FlipManager) MaybeInjectFlipMessage(ctx context.Context, boxedMsg chat1
m.Debug(ctx, "MaybeInjectFlipMessage: bogus flip message with a non-flip body")
return true
}
go func(ctx context.Context) {
// Ignore anything from the current device
ctx = BackgroundContext(ctx, m.G())
select {
case m.maybeInjectCh <- func() {
defer m.Trace(ctx, func() error { return nil },
"MaybeInjectFlipMessage(goroutine): uid: %s convID: %s", uid, convID)()
lock := m.injectLockTab.AcquireOnName(ctx, m.G(), convID.String())
defer lock.Release(ctx)

// Ignore anything from the current device
"MaybeInjectFlipMessage(goroutine): uid: %s convID: %s id: %d",
uid, convID, msg.GetMessageID())()
if m.Me().Eq(flip.UserDevice{
U: msg.Valid().ClientHeader.Sender,
D: msg.Valid().ClientHeader.SenderDevice,
}) {
// If this is our own message, then we need to make sure to update the msgID of the flip
m.Debug(ctx, "MaybeInjectFlipMessage: own message, updating stored and exiting")
m.gameMsgIDs.Add(body.Flip().GameID.String(), msg.GetMessageID())
return
}
Expand All @@ -1221,7 +1233,10 @@ func (m *FlipManager) MaybeInjectFlipMessage(ctx context.Context, boxedMsg chat1
if err := m.updateActiveGame(ctx, uid, convID, hmi.ConvID, msg, body.Flip().GameID); err != nil {
m.Debug(ctx, "MaybeInjectFlipMessage: failed to rebuild non-active game: %s", err)
}
}(BackgroundContext(ctx, m.G()))
}:
default:
m.Debug(ctx, "MaybeInjectFlipMessage: failed to dispatch job, queue full!")
}
return true
}

Expand Down

0 comments on commit 6784070

Please sign in to comment.