major changes
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.k6n.net/go-cart-actor/pkg/actor"
|
||||
messages "git.k6n.net/go-cart-actor/proto/control"
|
||||
"go.opentelemetry.io/contrib/bridges/otelslog"
|
||||
"go.opentelemetry.io/otel"
|
||||
@@ -130,25 +131,29 @@ func (h *RemoteHost[V]) Ping() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *RemoteHost[V]) Get(ctx context.Context, id uint64, grain any) error {
|
||||
func (h *RemoteHost[V]) Get(ctx context.Context, id uint64) (*V, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
reply, error := h.controlClient.Get(ctx, &messages.GetRequest{Id: id})
|
||||
if error != nil {
|
||||
return error
|
||||
return nil, error
|
||||
}
|
||||
return json.Unmarshal(reply.Grain.Value, grain)
|
||||
var grain V
|
||||
err := json.Unmarshal(reply.Grain.Value, &grain)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unpack state: %w", err)
|
||||
}
|
||||
return &grain, nil
|
||||
}
|
||||
|
||||
func (h *RemoteHost[V]) Apply(ctx context.Context, id uint64, mutation ...proto.Message) (bool, error) {
|
||||
func (h *RemoteHost[V]) Apply(ctx context.Context, id uint64, mutation ...proto.Message) (*actor.MutationResult[V], error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
toSend := make([]*anypb.Any, len(mutation))
|
||||
for i, msg := range mutation {
|
||||
anyMsg, err := anypb.New(msg)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to pack message: %w", err)
|
||||
return nil, fmt.Errorf("failed to pack message: %w", err)
|
||||
}
|
||||
toSend[i] = anyMsg
|
||||
}
|
||||
@@ -160,10 +165,34 @@ func (h *RemoteHost[V]) Apply(ctx context.Context, id uint64, mutation ...proto.
|
||||
if err != nil {
|
||||
h.missedPings++
|
||||
log.Printf("Apply %s failed: %v", h.host, err)
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
h.missedPings = 0
|
||||
return resp.Accepted, nil
|
||||
var grain V
|
||||
err = json.Unmarshal(resp.State.Value, &grain)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unpack state: %w", err)
|
||||
}
|
||||
var mutationList []actor.ApplyResult
|
||||
for _, msg := range resp.Mutations {
|
||||
|
||||
mutation, err := anypb.UnmarshalNew(msg.Message, proto.UnmarshalOptions{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unpack mutation: %w", err)
|
||||
}
|
||||
if msg.Error != nil {
|
||||
err = errors.New(*msg.Error)
|
||||
}
|
||||
mutationList = append(mutationList, actor.ApplyResult{
|
||||
Mutation: mutation,
|
||||
Error: err,
|
||||
})
|
||||
}
|
||||
res := &actor.MutationResult[V]{
|
||||
Result: grain,
|
||||
Mutations: mutationList,
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *RemoteHost[V]) Negotiate(knownHosts []string) ([]string, error) {
|
||||
|
||||
Reference in New Issue
Block a user