Fixed a potential race condition in the default state storage

This commit is contained in:
diamondburned (Forefront) 2020-01-20 01:42:03 -08:00
parent aa4154e73c
commit 2cd9def778
2 changed files with 11 additions and 6 deletions

View File

@ -16,6 +16,11 @@ type Store interface {
// All methods in StoreGetter will be wrapped by the State. If the State can't
// find anything in the storage, it will call the API itself and automatically
// add what's missing into the storage.
//
// Methods that return with a slice should pay attention to race conditions that
// would mutate the underlying slice (and as a result the returned slice as
// well). The best way to avoid this is to copy the whole slice, like
// DefaultStore does.
type StoreGetter interface {
Self() (*discord.User, error)

View File

@ -114,7 +114,7 @@ func (s *DefaultStore) Channels(
return nil, ErrStoreNotFound
}
return chs, nil
return append([]discord.Channel{}, chs...), nil
}
func (s *DefaultStore) PrivateChannels() ([]discord.Channel, error) {
@ -221,7 +221,7 @@ func (s *DefaultStore) Emojis(
return nil, ErrStoreNotFound
}
return gd.Emojis, nil
return append([]discord.Emoji{}, gd.Emojis...), nil
}
func (s *DefaultStore) EmojiSet(
@ -353,7 +353,7 @@ func (s *DefaultStore) Members(
return nil, ErrStoreNotFound
}
return ms, nil
return append([]discord.Member{}, ms...), nil
}
func (s *DefaultStore) MemberSet(
@ -437,7 +437,7 @@ func (s *DefaultStore) Messages(
return nil, ErrStoreNotFound
}
return ms, nil
return append([]discord.Message{}, ms...), nil
}
func (s *DefaultStore) MaxMessages() int {
@ -561,7 +561,7 @@ func (s *DefaultStore) Presences(
return nil, ErrStoreNotFound
}
return ps, nil
return append([]discord.Presence{}, ps...), nil
}
func (s *DefaultStore) PresenceSet(
@ -640,7 +640,7 @@ func (s *DefaultStore) Roles(
return nil, ErrStoreNotFound
}
return gd.Roles, nil
return append([]discord.Role{}, gd.Roles...), nil
}
func (s *DefaultStore) RoleSet(