From da448cedc8329246e0a4bf027c161dcd15d29a36 Mon Sep 17 00:00:00 2001 From: Corey Hulen Date: Fri, 17 Feb 2017 08:20:29 -0500 Subject: Fix issue with Aurora read replica (#5448) --- app/channel.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/app/channel.go b/app/channel.go index 4d02817f6..347c106a8 100644 --- a/app/channel.go +++ b/app/channel.go @@ -7,6 +7,7 @@ import ( "fmt" "net/http" "strings" + "time" l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/model" @@ -153,6 +154,8 @@ func CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *mo } else { channel := result.Data.(*model.Channel) + WaitForChannelMembership(channel.Id, userId) + InvalidateCacheForUser(userId) InvalidateCacheForUser(otherUserId) @@ -164,6 +167,31 @@ func CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *mo } } +func WaitForChannelMembership(channelId string, userId string) { + if len(utils.Cfg.SqlSettings.DataSourceReplicas) > 0 { + now := model.GetMillis() + + for model.GetMillis()-now < 12000 { + + time.Sleep(100 * time.Millisecond) + + result := <-Srv.Store.Channel().GetMember(channelId, userId) + + // If the membership was found then return + if result.Err == nil { + return + } + + // If we recieved a error but it wasn't a missing channel member then return + if result.Err.Id != store.MISSING_CHANNEL_MEMBER_ERROR { + return + } + } + + l4g.Error("WaitForChannelMembership giving up channelId=%v userId=%v", channelId, userId) + } +} + func UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppError) { if result := <-Srv.Store.Channel().Update(channel); result.Err != nil { return nil, result.Err @@ -327,6 +355,8 @@ func addUserToChannel(user *model.User, channel *model.Channel) (*model.ChannelM return nil, model.NewLocAppError("AddUserToChannel", "api.channel.add_user.to.channel.failed.app_error", nil, "") } + WaitForChannelMembership(channel.Id, user.Id) + InvalidateCacheForUser(user.Id) InvalidateCacheForChannelMembers(channel.Id) -- cgit v1.2.3-1-g7c22