Сделать отправку сообщения идемпотентной по Idempotency-Key с общей таблицей ключей и их чисткой

This commit is contained in:
2026-11-02 17:11:04 +03:00
parent 7fa8881eb3
commit 951fd0d517
19 changed files with 856 additions and 40 deletions
+17
View File
@@ -8,6 +8,8 @@ import (
"net/http"
"net/url"
"time"
"github.com/google/uuid"
)
// client is a minimal typed HTTP client for the messenger API.
@@ -33,6 +35,21 @@ func (c *client) post(path, token string, body, out any) error {
return c.do(req, token, http.StatusCreated, out)
}
// postMessage sends one message under a fresh Idempotency-Key.
func (c *client) postMessage(chatID, token, text string, out any) error {
raw, err := json.Marshal(map[string]string{"text": text})
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, c.base+"/api/v1/chats/"+chatID+"/messages", bytes.NewReader(raw))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", uuid.NewString())
return c.do(req, token, http.StatusCreated, out)
}
// login opens a session with Basic credentials and returns its bearer token.
func (c *client) login(username, password string) (string, error) {
req, err := http.NewRequest(http.MethodPost, c.base+"/api/v1/auth/sessions", nil)
+1 -2
View File
@@ -174,8 +174,7 @@ func sendConcurrently(baseURL, chatID string, tokens []string, perClient int) (m
<-start
for m := range perClient {
var msg chat.MessageResponse
body := map[string]string{"text": fmt.Sprintf("client %d message %d", n, m)}
err := own.post("/api/v1/chats/"+chatID+"/messages", token, body, &msg)
err := own.postMessage(chatID, token, fmt.Sprintf("client %d message %d", n, m), &msg)
mu.Lock()
if err != nil {
failed++