v0.0.18.6 проверка сообщения на nil в messageStream
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
0ab6bde370
commit
2f822df519
@ -3,6 +3,7 @@ package graph
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"tailly_back_v2/internal/domain"
|
||||
"tailly_back_v2/proto"
|
||||
"time"
|
||||
@ -120,7 +121,6 @@ func (r *queryResolver) GetUserChats(ctx context.Context, userID int) ([]*domain
|
||||
|
||||
// MessageStream реализация подписки на новые сообщения
|
||||
func (r *subscriptionResolver) MessageStream(ctx context.Context, userID int) (<-chan *domain.Message, error) {
|
||||
|
||||
if userID == 0 {
|
||||
return nil, fmt.Errorf("user not authenticated")
|
||||
}
|
||||
@ -134,16 +134,34 @@ func (r *subscriptionResolver) MessageStream(ctx context.Context, userID int) (<
|
||||
|
||||
messageChan := make(chan *domain.Message)
|
||||
go func() {
|
||||
defer close(messageChan)
|
||||
for {
|
||||
msg, err := stream.Recv()
|
||||
if err != nil {
|
||||
return
|
||||
defer func() {
|
||||
close(messageChan)
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Recovered from panic in MessageStream: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case messageChan <- protoMessageToDomain(msg.Message):
|
||||
default:
|
||||
msg, err := stream.Recv()
|
||||
if err != nil {
|
||||
log.Printf("Stream receive error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Добавляем проверку на nil сообщение
|
||||
if msg == nil || msg.Message == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case messageChan <- protoMessageToDomain(msg.Message):
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
@ -153,6 +171,10 @@ func (r *subscriptionResolver) MessageStream(ctx context.Context, userID int) (<
|
||||
|
||||
// Преобразование proto-сообщения в domain-модель
|
||||
func protoMessageToDomain(msg *proto.Message) *domain.Message {
|
||||
if msg == nil {
|
||||
return &domain.Message{}
|
||||
}
|
||||
|
||||
return &domain.Message{
|
||||
ID: int(msg.Id),
|
||||
ChatID: int(msg.ChatId),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user