mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 10:54:28 +00:00
Streams Fixes (#69)
* Add debugging to streams * More logging * Fix streams handler
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,4 @@
|
|||||||
api-protobuf.json
|
api-protobuf.json
|
||||||
api-*.json
|
api-*.json
|
||||||
redoc-static.html
|
redoc-static.html
|
||||||
!docs
|
!docs
|
||||||
5
Makefile
Normal file
5
Makefile
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
run:
|
||||||
|
find . -name "main.go" | xargs -L 1 dirname | grep -v -E 'test|cmd|vendor' | sort | xargs -L 1 micro run
|
||||||
|
|
||||||
|
kill:
|
||||||
|
find . -name "main.go" | xargs -L 1 dirname | grep -v -E 'test|cmd|vendor' | xargs basename | xargs -L 1 micro kill
|
||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres@localhost:5432/chats?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/chats?sslmode=disable"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create service
|
// Create service
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres@localhost:5432/groups?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/groups?sslmode=disable"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create service
|
// Create service
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres@localhost:5432/invites?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/invites?sslmode=disable"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create service
|
// Create service
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres@localhost:5432/places?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/places?sslmode=disable"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create service
|
// Create service
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres@localhost:5432/seen?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/seen?sslmode=disable"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create service
|
// Create service
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/micro/micro/v3/service/logger"
|
||||||
pb "github.com/micro/services/streams/proto"
|
pb "github.com/micro/services/streams/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,5 +17,6 @@ func (s *Streams) Publish(ctx context.Context, req *pb.Message, rsp *pb.PublishR
|
|||||||
}
|
}
|
||||||
|
|
||||||
// publish the message
|
// publish the message
|
||||||
|
logger.Infof("Publishing message to topic: %v", req.Topic)
|
||||||
return s.Events.Publish(req.Topic, req.Message)
|
return s.Events.Publish(req.Topic, req.Message)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
"github.com/micro/micro/v3/service/events"
|
"github.com/micro/micro/v3/service/events"
|
||||||
@@ -12,6 +13,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *Streams) Subscribe(ctx context.Context, req *pb.SubscribeRequest, stream pb.Streams_SubscribeStream) error {
|
func (s *Streams) Subscribe(ctx context.Context, req *pb.SubscribeRequest, stream pb.Streams_SubscribeStream) error {
|
||||||
|
logger.Infof("Recieved subscribe request. Topic: '%v', Token: '%v'", req.Topic, req.Token)
|
||||||
|
|
||||||
// validate the request
|
// validate the request
|
||||||
if len(req.Token) == 0 {
|
if len(req.Token) == 0 {
|
||||||
return ErrMissingToken
|
return ErrMissingToken
|
||||||
@@ -38,27 +41,31 @@ func (s *Streams) Subscribe(ctx context.Context, req *pb.SubscribeRequest, strea
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start the subscription
|
// start the subscription
|
||||||
|
logger.Infof("Subscribing to %v via queue %v", req.Topic, token.Token)
|
||||||
evChan, err := s.Events.Consume(req.Topic, events.WithGroup(token.Token))
|
evChan, err := s.Events.Consume(req.Topic, events.WithGroup(token.Token))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("Error connecting to events stream: %v", err)
|
logger.Errorf("Error connecting to events stream: %v", err)
|
||||||
return errors.InternalServerError("EVENTS_ERROR", "Error connecting to events stream")
|
return errors.InternalServerError("EVENTS_ERROR", "Error connecting to events stream")
|
||||||
}
|
}
|
||||||
go func() {
|
defer stream.Close()
|
||||||
defer stream.Close()
|
|
||||||
for {
|
|
||||||
msg, ok := <-evChan
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := stream.Send(&pb.Message{
|
|
||||||
Topic: msg.Topic,
|
|
||||||
Message: string(msg.Payload),
|
|
||||||
SentAt: timestamppb.New(msg.Timestamp),
|
|
||||||
}); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return nil
|
for {
|
||||||
|
msg, ok := <-evChan
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof("Sending message to subscriber %v", token.Topic)
|
||||||
|
pbMsg := &pb.Message{
|
||||||
|
Topic: msg.Topic,
|
||||||
|
Message: string(msg.Payload),
|
||||||
|
SentAt: timestamppb.New(msg.Timestamp),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := stream.Send(pbMsg); err == io.EOF {
|
||||||
|
return nil
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres@localhost:5432/streams?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/streams?sslmode=disable"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create service
|
// Create service
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres@localhost:5432/threads?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/threads?sslmode=disable"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create service
|
// Create service
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres@localhost:5432/users?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/users?sslmode=disable"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create service
|
// Create service
|
||||||
|
|||||||
Reference in New Issue
Block a user