forked from mtusi2026/mtusi2026lab3
на 4 готово
This commit is contained in:
@@ -2,6 +2,7 @@ services:
|
|||||||
db:
|
db:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
container_name: Lido-db
|
container_name: Lido-db
|
||||||
|
mem_limit: 128m
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: lido_db
|
POSTGRES_DB: lido_db
|
||||||
POSTGRES_USER: user
|
POSTGRES_USER: user
|
||||||
@@ -19,6 +20,7 @@ services:
|
|||||||
|
|
||||||
redis:
|
redis:
|
||||||
image: redis:7-alpine
|
image: redis:7-alpine
|
||||||
|
mem_limit: 128m
|
||||||
container_name: lido-redis
|
container_name: lido-redis
|
||||||
ports:
|
ports:
|
||||||
- "6379:6379"
|
- "6379:6379"
|
||||||
@@ -43,6 +45,9 @@ services:
|
|||||||
redis:
|
redis:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
networks: [messenger]
|
networks: [messenger]
|
||||||
|
mem_limit: 512m
|
||||||
|
cpus: 1.0
|
||||||
|
mem_reservation: 256m
|
||||||
environment:
|
environment:
|
||||||
FLYWAY_ENABLED: "false"
|
FLYWAY_ENABLED: "false"
|
||||||
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/lido_db
|
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/lido_db
|
||||||
@@ -64,6 +69,7 @@ services:
|
|||||||
|
|
||||||
nginx:
|
nginx:
|
||||||
image: nginx:1.27-alpine
|
image: nginx:1.27-alpine
|
||||||
|
mem_limit: 128m
|
||||||
ports:
|
ports:
|
||||||
- "8080:80"
|
- "8080:80"
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ http {
|
|||||||
proxy_connect_timeout 2s;
|
proxy_connect_timeout 2s;
|
||||||
proxy_send_timeout 30s;
|
proxy_send_timeout 30s;
|
||||||
proxy_read_timeout 30s;
|
proxy_read_timeout 30s;
|
||||||
|
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||||
|
proxy_next_upstream_tries 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package org.example.service;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.example.model.IdempotencyKey;
|
||||||
|
import org.example.model.Message;
|
||||||
|
import org.example.repository.IdempotencyKeyRepository;
|
||||||
|
import org.example.repository.MessageRepository;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class MessageIdempotencyWriter {
|
||||||
|
|
||||||
|
private final MessageRepository messageRepository;
|
||||||
|
private final IdempotencyKeyRepository idempotencyKeyRepository;
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||||
|
public Message insertNew(
|
||||||
|
UUID chatId,
|
||||||
|
UUID senderId,
|
||||||
|
String text,
|
||||||
|
String key,
|
||||||
|
String hash) {
|
||||||
|
|
||||||
|
Message message = messageRepository.save(
|
||||||
|
Message.builder()
|
||||||
|
.chatId(chatId)
|
||||||
|
.senderId(senderId)
|
||||||
|
.text(text)
|
||||||
|
.createdAt(Instant.now())
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
|
||||||
|
idempotencyKeyRepository.saveAndFlush(
|
||||||
|
IdempotencyKey.builder()
|
||||||
|
.userId(senderId)
|
||||||
|
.chatId(chatId)
|
||||||
|
.idempotencyKey(key)
|
||||||
|
.requestHash(hash)
|
||||||
|
.messageId(message.getId())
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,59 +31,47 @@ public class MessageService {
|
|||||||
private final MessageRepository messageRepository;
|
private final MessageRepository messageRepository;
|
||||||
private final IdempotencyKeyRepository idempotencyKeyRepository;
|
private final IdempotencyKeyRepository idempotencyKeyRepository;
|
||||||
private final ChatService chatService;
|
private final ChatService chatService;
|
||||||
|
private final MessageIdempotencyWriter writer;
|
||||||
|
|
||||||
@Transactional
|
@Transactional(readOnly = true) // или без write на этом уровне
|
||||||
public Message sendMessage(
|
public Message sendMessage(
|
||||||
UUID chatId,
|
UUID chatId,
|
||||||
UUID senderId,
|
UUID senderId,
|
||||||
CreateMessageDto messageObject,
|
CreateMessageDto messageObject,
|
||||||
String idempotencyKey){
|
String idempotencyKey) {
|
||||||
|
|
||||||
chatService.requireMember(chatId);
|
chatService.requireMember(chatId);
|
||||||
if (!chatService.isMember(chatId, senderId)){
|
if (!chatService.isMember(chatId, senderId)) {
|
||||||
throw new ForbiddenException("Пользователь не является участником чата");
|
throw new ForbiddenException("Пользователь не является участником чата");
|
||||||
}
|
}
|
||||||
|
|
||||||
String key = idempotencyKey.trim();
|
String key = idempotencyKey.trim();
|
||||||
String hash = sha256(messageObject.getText());
|
String hash = sha256(messageObject.getText());
|
||||||
Optional<IdempotencyKey> exists = idempotencyKeyRepository.findByUserIdAndChatIdAndIdempotencyKey(senderId, chatId, key);
|
|
||||||
|
Optional<IdempotencyKey> exists =
|
||||||
|
idempotencyKeyRepository.findByUserIdAndChatIdAndIdempotencyKey(senderId, chatId, key);
|
||||||
|
|
||||||
if (exists.isPresent()) {
|
if (exists.isPresent()) {
|
||||||
var row = exists.get();
|
return messageFromIdem(exists.get(), hash);
|
||||||
if (!row.getRequestHash().equals(hash)) {
|
|
||||||
throw new ConflictException("Idempotency key conflict");
|
|
||||||
}
|
|
||||||
return messageRepository.findById(row.getMessageId())
|
|
||||||
.orElseThrow(() -> new IllegalArgumentException("Jшибка при отправке сообщения") );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Message message = Message.builder()
|
|
||||||
.chatId(chatId)
|
|
||||||
.senderId(senderId)
|
|
||||||
.text(messageObject.getText())
|
|
||||||
.createdAt(Instant.now())
|
|
||||||
.build();
|
|
||||||
message = messageRepository.save(message);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
idempotencyKeyRepository.saveAndFlush(
|
return writer.insertNew(chatId, senderId, messageObject.getText(), key, hash);
|
||||||
IdempotencyKey.builder()
|
} catch (DataIntegrityViolationException e) {
|
||||||
.userId(senderId)
|
|
||||||
.chatId(chatId)
|
|
||||||
.idempotencyKey(key)
|
|
||||||
.requestHash(hash)
|
|
||||||
.messageId(message.getId())
|
|
||||||
.build()
|
|
||||||
);
|
|
||||||
} catch (DataIntegrityViolationException e){
|
|
||||||
IdempotencyKey rec = idempotencyKeyRepository
|
IdempotencyKey rec = idempotencyKeyRepository
|
||||||
.findByUserIdAndChatIdAndIdempotencyKey(senderId, chatId, key)
|
.findByUserIdAndChatIdAndIdempotencyKey(senderId, chatId, key)
|
||||||
.orElseThrow(() -> e);
|
.orElseThrow(() -> e);
|
||||||
return messageRepository.findById(rec.getUserId())
|
return messageFromIdem(rec, hash);
|
||||||
.orElseThrow(()-> e);
|
|
||||||
}
|
}
|
||||||
return message;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Message messageFromIdem(IdempotencyKey row, String hash) {
|
||||||
|
if (!row.getRequestHash().equals(hash)) {
|
||||||
|
throw new ConflictException("Idempotency key conflict");
|
||||||
|
}
|
||||||
|
return messageRepository.findById(row.getMessageId())
|
||||||
|
.orElseThrow(() -> new IllegalStateException("idempotency message missing"));
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public MessagePage getMessage(UUID chatId, UUID userId, String cursor, int limit) {
|
public MessagePage getMessage(UUID chatId, UUID userId, String cursor, int limit) {
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import http from 'k6/http';
|
||||||
|
import { check } from 'k6';
|
||||||
|
|
||||||
|
export const options = {
|
||||||
|
scenarios: {
|
||||||
|
steady: {
|
||||||
|
executor: 'constant-vus',
|
||||||
|
vus: 20,
|
||||||
|
duration: '120s',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const BASE = __ENV.BASE_URL || 'http://localhost:8080';
|
||||||
|
const TOKEN = __ENV.TOKEN;
|
||||||
|
const CHAT = __ENV.CHAT_ID;
|
||||||
|
|
||||||
|
export default function () {
|
||||||
|
const res = http.get(`${BASE}/health/live`);
|
||||||
|
check(res, { 'live 200': (r) => r.status === 200 });
|
||||||
|
http.get(`${BASE}/api/v1/users/me`, { headers: { Authorization: `Bearer ${TOKEN}` } });
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user