fix: update env and PR template

This commit is contained in:
TAskMAster339
2026-10-10 22:35:17 +03:00
parent 715f6c4175
commit 18b3627ec8
22 changed files with 450 additions and 77 deletions
+47
View File
@@ -0,0 +1,47 @@
package repositories
import (
"errors"
"os"
"strconv"
"strings"
"time"
"github.com/redis/go-redis/v9"
)
func RedisClientFromEnvironment() (*redis.Client, error) {
if path := os.Getenv("CACHE_URL_FILE"); path != "" {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
options, err := redis.ParseURL(strings.TrimSpace(string(data)))
if err != nil {
return nil, errors.New("invalid Redis URL in CACHE_URL_FILE")
}
options.PoolSize = 10
options.MaxRetries = -1
options.DialTimeout = time.Second
options.ReadTimeout = time.Second
options.WriteTimeout = time.Second
return redis.NewClient(options), nil
}
address := os.Getenv("REDIS_ADDR")
if address == "" {
address = "redis:6379"
}
return NewRedisClient(address), nil
}
func IdempotencyTTL() (time.Duration, error) {
value := os.Getenv("IDEMPOTENCY_TTL_SECONDS")
if value == "" {
value = "86400"
}
seconds, err := strconv.ParseInt(value, 10, 64)
if err != nil || seconds < 1 || seconds > 31536000 {
return 0, errors.New("IDEMPOTENCY_TTL_SECONDS must be between 1 and 31536000")
}
return time.Duration(seconds) * time.Second, nil
}
+2 -2
View File
@@ -45,7 +45,7 @@ func (s *MessageStore) CreateIdempotent(ctx context.Context, chatID, senderID, t
return Message{}, err
}
// Expiry permits reuse after 24 hours; it never deletes the original message.
// Expiry permits key reuse; it never deletes the original message.
if _, err = tx.Exec(ctx, "DELETE FROM message_idempotency WHERE sender_id=$1 AND chat_id=$2 AND key=$3", senderID, chatID, key); err != nil {
return Message{}, err
}
@@ -57,7 +57,7 @@ func (s *MessageStore) CreateIdempotent(ctx context.Context, chatID, senderID, t
return Message{}, err
}
if _, err = tx.Exec(ctx, `INSERT INTO message_idempotency(sender_id,chat_id,key,message_id,expires_at)
VALUES($1,$2,$3,$4,clock_timestamp() + interval '24 hours')`, senderID, chatID, key, message.ID); err != nil {
VALUES($1,$2,$3,$4,clock_timestamp() + $5 * interval '1 second')`, senderID, chatID, key, message.ID, s.idempotencyTTL.Seconds()); err != nil {
return Message{}, err
}
return message, tx.Commit(ctx)
+7 -2
View File
@@ -17,11 +17,16 @@ type Message struct {
}
type MessageStore struct {
db *pgxpool.Pool
db *pgxpool.Pool
idempotencyTTL time.Duration
}
func NewMessageStore(db *pgxpool.Pool) *MessageStore {
return &MessageStore{db: db}
return NewMessageStoreWithTTL(db, 24*time.Hour)
}
func NewMessageStoreWithTTL(db *pgxpool.Pool, ttl time.Duration) *MessageStore {
return &MessageStore{db: db, idempotencyTTL: ttl}
}
func (s *MessageStore) Create(