major changes
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 43s
Build and Publish / BuildAndDeployArm64 (push) Successful in 4m43s

This commit is contained in:
matst80
2025-12-04 20:56:54 +01:00
parent 6d5358b53b
commit f67eeb3c49
21 changed files with 572 additions and 242 deletions

View File

@@ -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) {