mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-12 03:33:43 +00:00
feat: make proposal responses durable
This commit is contained in:
+21
-8
@@ -45,6 +45,9 @@ type SessionIssuer interface {
|
||||
type ProposalBackend interface {
|
||||
Get(context.Context, string, string, time.Time) (domain.Proposal, error)
|
||||
}
|
||||
type ProposalMutationBackend interface {
|
||||
Respond(context.Context, string, string, string, bool, uint64, time.Time) (domain.Proposal, error)
|
||||
}
|
||||
|
||||
type AssignmentView struct {
|
||||
MatchID string `json:"match_id"`
|
||||
@@ -451,15 +454,25 @@ func (s *Service) proposalMutation(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "invalid_revision")
|
||||
return
|
||||
}
|
||||
s.proposalMu.Lock()
|
||||
defer s.proposalMu.Unlock()
|
||||
proposal, exists := s.Proposals[parts[0]]
|
||||
if !exists || proposal == nil {
|
||||
writeError(w, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
now := s.now()
|
||||
updated, err := proposal.Respond(playerID, key, parts[1] == "accept", revision, now)
|
||||
var updated domain.Proposal
|
||||
if s.ProposalBackend != nil {
|
||||
mutator, supportsMutation := s.ProposalBackend.(ProposalMutationBackend)
|
||||
if !supportsMutation {
|
||||
writeError(w, http.StatusServiceUnavailable, "proposal_unavailable")
|
||||
return
|
||||
}
|
||||
updated, err = mutator.Respond(r.Context(), playerID, parts[0], key, parts[1] == "accept", revision, now)
|
||||
} else {
|
||||
s.proposalMu.Lock()
|
||||
defer s.proposalMu.Unlock()
|
||||
proposal, exists := s.Proposals[parts[0]]
|
||||
if !exists || proposal == nil {
|
||||
writeError(w, http.StatusNotFound, "not_found")
|
||||
return
|
||||
}
|
||||
updated, err = proposal.Respond(playerID, key, parts[1] == "accept", revision, now)
|
||||
}
|
||||
if err != nil {
|
||||
writeDomainError(w, err)
|
||||
return
|
||||
|
||||
@@ -22,8 +22,18 @@ type queueBackendSpy struct{ createCalls, heartbeatCalls, cancelCalls, getCalls
|
||||
type sessionBackendSpy struct{ calls int }
|
||||
|
||||
type proposalBackendSpy struct {
|
||||
proposal domain.Proposal
|
||||
calls int
|
||||
proposal domain.Proposal
|
||||
calls int
|
||||
mutations int
|
||||
}
|
||||
|
||||
func (b *proposalBackendSpy) Respond(_ context.Context, playerID, _ string, key string, accept bool, revision uint64, now time.Time) (domain.Proposal, error) {
|
||||
b.mutations++
|
||||
updated, err := b.proposal.Respond(playerID, key, accept, revision, now)
|
||||
if err == nil {
|
||||
b.proposal = updated
|
||||
}
|
||||
return updated, err
|
||||
}
|
||||
|
||||
func (b *proposalBackendSpy) Get(_ context.Context, playerID, _ string, _ time.Time) (domain.Proposal, error) {
|
||||
@@ -389,6 +399,21 @@ func TestProposalRecoveryUsesDurableBackendAndRemainsParticipantScoped(t *testin
|
||||
if status := get(participantSession, participantToken); status != http.StatusOK {
|
||||
t.Fatalf("participant recovery status = %d", status)
|
||||
}
|
||||
respond, err := http.NewRequest(http.MethodPost, server.URL+"/v1/proposals/"+proposal.ProposalID+"/accept", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
respond.Header.Set("Authorization", "Bearer "+participantSession.SessionID+":"+participantToken)
|
||||
respond.Header.Set("Idempotency-Key", "proposal-durable-response-123456")
|
||||
respond.Header.Set("If-Match-Revision", "0")
|
||||
response, err := server.Client().Do(respond)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK || backend.mutations != 1 {
|
||||
t.Fatalf("durable response status = %d, mutations = %d", response.StatusCode, backend.mutations)
|
||||
}
|
||||
if status := get(outsiderSession, outsiderToken); status != http.StatusNotFound {
|
||||
t.Fatalf("outsider recovery status = %d", status)
|
||||
}
|
||||
|
||||
@@ -37,6 +37,10 @@ func (p postgresProposalBackend) Get(ctx context.Context, playerID, proposalID s
|
||||
return store.GetProposal(ctx, p.db, playerID, proposalID, now)
|
||||
}
|
||||
|
||||
func (p postgresProposalBackend) Respond(ctx context.Context, playerID, proposalID, idempotencyKey string, accept bool, expectedRevision uint64, now time.Time) (domain.Proposal, error) {
|
||||
return store.RespondToProposal(ctx, p.db, playerID, proposalID, idempotencyKey, accept, expectedRevision, now)
|
||||
}
|
||||
|
||||
func ProposalProviderFromStore(db *sql.DB) ProposalBackend {
|
||||
return postgresProposalBackend{db: db}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user