Skip to content
Snippets Groups Projects
Commit 7e10abb7 authored by Josh Brooks's avatar Josh Brooks
Browse files

Fix lazy loader for UserMessageInternal

parent 69e95e2c
Branches
Tags
7 merge requests!510Release,!419rewrote the health tracker to both consider if there are waiting rounds and...,!371[Channel RSAtoPrivate] Implement Reverse Asymmetric in Client/Broadcast,!354Channels impl,!340Project/channels,!339Project/channels,!335Implement channel message protobufs
...@@ -14,35 +14,46 @@ import ( ...@@ -14,35 +14,46 @@ import (
// UserMessageInternal is the internal structure of a UserMessage protobuf. // UserMessageInternal is the internal structure of a UserMessage protobuf.
type UserMessageInternal struct { type UserMessageInternal struct {
mux sync.Mutex mux sync.RWMutex
*UserMessage userMessage *UserMessage
channelMsg *ChannelMessage channelMessage *ChannelMessage
} }
func NewUserMessageInternal(ursMsg *UserMessage) *UserMessageInternal { func NewUserMessageInternal(ursMsg *UserMessage) *UserMessageInternal {
return &UserMessageInternal{ return &UserMessageInternal{
mux: sync.Mutex{}, mux: sync.RWMutex{},
UserMessage: ursMsg, userMessage: ursMsg,
channelMsg: nil, channelMessage: nil,
} }
} }
// GetChannelMessage retrieves a serializes ChannelMessage within the // GetUserMessage retrieves the UserMessage within
// UserMessageInternal object. // UserMessageInternal.
func (umi *UserMessageInternal) GetUserMessage() *UserMessage {
umi.mux.RLock()
umi.mux.RUnlock()
return umi.userMessage
}
// GetChannelMessage retrieves the ChannelMessage within
// UserMessageInternal. This is a lazy getter which will
// deserialize the ChannelMessage within the UserMessage.Message field.
// This deserialized ChannelMessage will then be placed into
// UserMessageInternal's channelMessage field and return. On subsequent calls it will return
// the message stored in UserMessageInternal.
func (umi *UserMessageInternal) GetChannelMessage() (*ChannelMessage, error) { func (umi *UserMessageInternal) GetChannelMessage() (*ChannelMessage, error) {
umi.mux.Lock() umi.mux.Lock()
defer umi.mux.Unlock() defer umi.mux.Unlock()
// check if channel message if umi.channelMessage == nil {
if umi.channelMsg != nil { chanMessage := &ChannelMessage{}
return umi.channelMsg, nil err := proto.Unmarshal(umi.userMessage.Message, chanMessage)
}
// if not, deserialize and store
unmarshalledChannelMsg := &ChannelMessage{}
err := proto.Unmarshal(umi.UserMessage.Message, unmarshalledChannelMsg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return unmarshalledChannelMsg, nil umi.channelMessage = chanMessage
}
return umi.channelMessage, nil
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment