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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"tailly_back_v2/internal/domain"
|
"tailly_back_v2/internal/domain"
|
||||||
"tailly_back_v2/proto"
|
"tailly_back_v2/proto"
|
||||||
"time"
|
"time"
|
||||||
@ -120,7 +121,6 @@ func (r *queryResolver) GetUserChats(ctx context.Context, userID int) ([]*domain
|
|||||||
|
|
||||||
// MessageStream реализация подписки на новые сообщения
|
// MessageStream реализация подписки на новые сообщения
|
||||||
func (r *subscriptionResolver) MessageStream(ctx context.Context, userID int) (<-chan *domain.Message, error) {
|
func (r *subscriptionResolver) MessageStream(ctx context.Context, userID int) (<-chan *domain.Message, error) {
|
||||||
|
|
||||||
if userID == 0 {
|
if userID == 0 {
|
||||||
return nil, fmt.Errorf("user not authenticated")
|
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)
|
messageChan := make(chan *domain.Message)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(messageChan)
|
defer func() {
|
||||||
for {
|
close(messageChan)
|
||||||
msg, err := stream.Recv()
|
if r := recover(); r != nil {
|
||||||
if err != nil {
|
log.Printf("Recovered from panic in MessageStream: %v", r)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
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-модель
|
// Преобразование proto-сообщения в domain-модель
|
||||||
func protoMessageToDomain(msg *proto.Message) *domain.Message {
|
func protoMessageToDomain(msg *proto.Message) *domain.Message {
|
||||||
|
if msg == nil {
|
||||||
|
return &domain.Message{}
|
||||||
|
}
|
||||||
|
|
||||||
return &domain.Message{
|
return &domain.Message{
|
||||||
ID: int(msg.Id),
|
ID: int(msg.Id),
|
||||||
ChatID: int(msg.ChatId),
|
ChatID: int(msg.ChatId),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user