Skip to content

Commit

Permalink
Load components in bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
acroca committed Apr 8, 2021
1 parent 7a92823 commit 2eafc02
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 177 deletions.
4 changes: 2 additions & 2 deletions cmd/server/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *server) move(ctx context.Context, offsetX, offsetY int64) (*models.Posi
}

newPos := &components.Position{}
err = s.registry.LoadComponent(ctx, playerData.Entity, newPos)
err = s.registry.LoadComponents(ctx, playerData.Entity, newPos)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func (s *server) Build(ctx context.Context, _ *models.BuildReq) (*models.BuildRe

playerData := s.players[playerID]
pos := &components.Position{}
err := s.registry.LoadComponent(ctx, playerData.Entity, pos)
err := s.registry.LoadComponents(ctx, playerData.Entity, pos)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions components/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestRegistrySaveLoad(t *testing.T) {
require.NoError(t, registry.CreateComponents(context.Background(), entity, position))

loadedPosition := &Position{}
require.NoError(t, registry.LoadComponent(context.Background(), entity, loadedPosition))
require.NoError(t, registry.LoadComponents(context.Background(), entity, loadedPosition))

require.True(t, proto.Equal(position, loadedPosition))
}
Expand All @@ -40,7 +40,7 @@ func TestRegistryLoadMissingComponentType(t *testing.T) {
require.NoError(t, err)

loadedPosition := &Position{}
require.Error(t, registry.LoadComponent(context.Background(), entity, loadedPosition))
require.Error(t, registry.LoadComponents(context.Background(), entity, loadedPosition))
}

func TestRegistryLoadComponents(t *testing.T) {
Expand Down
29 changes: 15 additions & 14 deletions components/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,25 @@ func (s *RedisStore) HDelProto(ctx context.Context, key string, v proto.Message)
return nil
}

func (s *RedisStore) HReadProto(ctx context.Context, key string, v proto.Message) error {
name := string(v.ProtoReflect().Descriptor().FullName().Name())
res := s.client.HGet(ctx, key, name)
if err := res.Err(); err != nil {
s.logger.Error("error getting hash member", zap.Error(err), zap.String("key", key), zap.String("name", name))
return err
func (s *RedisStore) HReadProtos(ctx context.Context, key string, values ...proto.Message) error {
names := make([]string, len(values))
for i, v := range values {
names[i] = string(v.ProtoReflect().Descriptor().FullName().Name())
}
b, err := res.Bytes()
if err != nil {
s.logger.Error("error getting bytes", zap.Error(err), zap.String("key", key), zap.String("name", name))
res := s.client.HMGet(ctx, key, names...)
if err := res.Err(); err != nil {
s.logger.Error("error getting hash members", zap.Error(err), zap.String("key", key), zap.Strings("names", names))
return err
}
err = proto.Unmarshal(b, v)
if err != nil {
s.logger.Error("error unmarshalling protos", zap.Error(err), zap.String("key", key), zap.String("name", name))
return err
for i, item := range res.Val() {
b := []byte(item.(string))
err := proto.Unmarshal(b, values[i])
if err != nil {
s.logger.Error("error unmarshalling protos", zap.Error(err), zap.String("key", key), zap.Strings("names", names))
return err
}
s.logger.Debug("loaded proto", zap.String("key", key), zap.Strings("names", names))
}
s.logger.Debug("loaded proto", zap.String("key", key), zap.String("name", name))
return nil
}

Expand Down
19 changes: 11 additions & 8 deletions components/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,27 @@ func (b *Registry) DeleteEntity(parentCtx context.Context, entity Entity) error
return nil
}

func (b *Registry) LoadComponent(parentCtx context.Context, entity Entity, component proto.Message) error {
componentType := string(component.ProtoReflect().Descriptor().FullName().Name())
func (b *Registry) LoadComponents(parentCtx context.Context, entity Entity, components ...proto.Message) error {
componentTypes := make([]string, len(components))
for i, component := range components {
componentTypes[i] = string(component.ProtoReflect().Descriptor().FullName().Name())
}

logger := b.logger.With(zap.Int64("entity_id", int64(entity)), zap.String("component_type", componentType))
ctx, span := registryTracer.Start(parentCtx, "LoadComponent")
logger := b.logger.With(zap.Int64("entity_id", int64(entity)), zap.Strings("component_type", componentTypes))
ctx, span := registryTracer.Start(parentCtx, "LoadComponents")
span.SetAttributes(
attribute.Int64("entity_id", int64(entity)),
attribute.String("componentType", componentType),
attribute.Array("componentType", componentTypes),
)
defer span.End()

logger.Debug("loading component")
err := b.redisStore.HReadProto(ctx, strconv.FormatInt(int64(entity), 10), component)
logger.Debug("loading components")
err := b.redisStore.HReadProtos(ctx, strconv.FormatInt(int64(entity), 10), components...)
if err != nil {
if err == redis.Nil {
logger.Warn("component not found")
} else {
logger.Error("error loading component", zap.Error(err))
logger.Error("error loading components", zap.Error(err))
}
return err
}
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ module github.com/code-cell/esive
go 1.15

require (
github.com/AsynkronIT/protoactor-go v0.0.0-20210305101446-d68990342ece
github.com/gdamore/tcell/v2 v2.2.0
github.com/go-redis/redis/extra/redisotel v0.3.0 // indirect
github.com/go-redis/redis/extra/redisotel v0.3.0
github.com/go-redis/redis/v8 v8.8.0
github.com/google/uuid v1.2.0
github.com/kr/pretty v0.2.0 // indirect
github.com/rivo/tview v0.0.0-20210312174852-ae9464cc3598
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.19.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.19.0
go.opentelemetry.io/otel v0.19.0
go.opentelemetry.io/otel/exporters/trace/jaeger v0.19.0
go.opentelemetry.io/otel/sdk v0.19.0
go.opentelemetry.io/otel/trace v0.19.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0
google.golang.org/grpc v1.36.0
google.golang.org/protobuf v1.25.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
Loading

0 comments on commit 2eafc02

Please sign in to comment.