Compare commits

..

5 Commits

Author SHA1 Message Date
6315169aca update for latest micro logger changes
Some checks failed
build / test (push) Failing after 4m54s
build / lint (push) Successful in 9m28s
codeql / analyze (go) (push) Failing after 50s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-10-16 23:24:23 +03:00
78194a8863 update deps (#116)
Some checks failed
build / lint (push) Failing after 9s
build / test (push) Failing after 9s
codeql / analyze (go) (push) Failing after 1m45s
* update deps

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>

Reviewed-on: #116
2024-09-20 18:41:11 +03:00
881eec8e36 Merge pull request 'regen' (#115) from updates into v3
Reviewed-on: #115
2023-03-25 22:17:59 +03:00
f2092f3649 Merge branch 'v3' into updates 2023-03-25 22:17:39 +03:00
6c623d0149 regen
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-03-25 22:16:14 +03:00
9 changed files with 99 additions and 1437 deletions

View File

@ -1,3 +1,3 @@
package service
//go:generate protoc -I./proto -I. --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --go-micro_out=components=micro|rpc,standalone=true,paths=source_relative:./micro proto/broker.proto
//go:generate protoc -I./proto -I. --go_out=paths=source_relative:./proto --go-micro_out=components=micro|grpc,standalone=true,paths=source_relative:./micro proto/broker.proto

19
go.mod
View File

@ -1,9 +1,20 @@
module go.unistack.org/micro-broker-service/v3
go 1.16
go 1.22
toolchain go1.23.2
require (
go.unistack.org/micro/v3 v3.10.11
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
go.unistack.org/micro-server-http/v3 v3.11.31
go.unistack.org/micro/v3 v3.10.97
google.golang.org/protobuf v1.35.1
)
require (
github.com/google/go-cmp v0.6.0 // indirect
go.unistack.org/micro-proto/v3 v3.4.1 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect
google.golang.org/grpc v1.67.1 // indirect
)

1173
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,7 @@
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
// protoc-gen-go-micro version: v3.5.3
// versions:
// - protoc-gen-go-micro v3.10.2
// - protoc v3.21.12
// source: broker.proto
package servicepb
@ -7,19 +9,16 @@ package servicepb
import (
context "context"
proto "go.unistack.org/micro-broker-service/v3/proto"
api "go.unistack.org/micro/v3/api"
v3 "go.unistack.org/micro-server-http/v3"
client "go.unistack.org/micro/v3/client"
)
var (
BrokerServiceName = "BrokerService"
BrokerServiceEndpoints = []api.Endpoint{}
)
func NewBrokerServiceEndpoints() []api.Endpoint {
return BrokerServiceEndpoints
}
var (
BrokerServiceServerEndpoints = []v3.EndpointMetadata{}
)
type BrokerServiceClient interface {
Publish(ctx context.Context, req *proto.PublishRequest, opts ...client.CallOption) (*proto.PublishResponse, error)

View File

@ -1,5 +1,5 @@
// Code generated by protoc-gen-go-micro. DO NOT EDIT.
// protoc-gen-go-micro version: v3.5.3
// protoc-gen-go-micro version: v3.10.2
// source: broker.proto
package servicepb
@ -7,7 +7,6 @@ package servicepb
import (
context "context"
proto "go.unistack.org/micro-broker-service/v3/proto"
api "go.unistack.org/micro/v3/api"
client "go.unistack.org/micro/v3/client"
server "go.unistack.org/micro/v3/server"
)
@ -58,6 +57,10 @@ func (s *brokerServiceClientSubscribe) Close() error {
return s.stream.Close()
}
func (s *brokerServiceClientSubscribe) CloseSend() error {
return s.stream.CloseSend()
}
func (s *brokerServiceClientSubscribe) Context() context.Context {
return s.stream.Context()
}
@ -97,6 +100,10 @@ func (s *brokerServiceClientBatchSubscribe) Close() error {
return s.stream.Close()
}
func (s *brokerServiceClientBatchSubscribe) CloseSend() error {
return s.stream.CloseSend()
}
func (s *brokerServiceClientBatchSubscribe) Context() context.Context {
return s.stream.Context()
}
@ -205,8 +212,5 @@ func RegisterBrokerServiceServer(s server.Server, sh BrokerServiceServer, opts .
}
h := &brokerServiceServer{sh}
var nopts []server.HandlerOption
for _, endpoint := range BrokerServiceEndpoints {
nopts = append(nopts, api.WithEndpoint(&endpoint))
}
return s.Handle(s.NewHandler(&BrokerService{h}, append(nopts, opts...)...))
}

View File

@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.17.3
// protoc v3.21.12
// source: broker.proto
package servicepb

View File

@ -1,264 +0,0 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
package servicepb
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
// BrokerServiceClient is the client API for BrokerService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type BrokerServiceClient interface {
Publish(ctx context.Context, in *PublishRequest, opts ...grpc.CallOption) (*PublishResponse, error)
BatchPublish(ctx context.Context, in *BatchPublishRequest, opts ...grpc.CallOption) (*BatchPublishResponse, error)
Subscribe(ctx context.Context, in *SubscribeRequest, opts ...grpc.CallOption) (BrokerService_SubscribeClient, error)
BatchSubscribe(ctx context.Context, in *BatchSubscribeRequest, opts ...grpc.CallOption) (BrokerService_BatchSubscribeClient, error)
}
type brokerServiceClient struct {
cc grpc.ClientConnInterface
}
func NewBrokerServiceClient(cc grpc.ClientConnInterface) BrokerServiceClient {
return &brokerServiceClient{cc}
}
func (c *brokerServiceClient) Publish(ctx context.Context, in *PublishRequest, opts ...grpc.CallOption) (*PublishResponse, error) {
out := new(PublishResponse)
err := c.cc.Invoke(ctx, "/servicepb.BrokerService/Publish", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *brokerServiceClient) BatchPublish(ctx context.Context, in *BatchPublishRequest, opts ...grpc.CallOption) (*BatchPublishResponse, error) {
out := new(BatchPublishResponse)
err := c.cc.Invoke(ctx, "/servicepb.BrokerService/BatchPublish", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *brokerServiceClient) Subscribe(ctx context.Context, in *SubscribeRequest, opts ...grpc.CallOption) (BrokerService_SubscribeClient, error) {
stream, err := c.cc.NewStream(ctx, &BrokerService_ServiceDesc.Streams[0], "/servicepb.BrokerService/Subscribe", opts...)
if err != nil {
return nil, err
}
x := &brokerServiceSubscribeClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type BrokerService_SubscribeClient interface {
Recv() (*Message, error)
grpc.ClientStream
}
type brokerServiceSubscribeClient struct {
grpc.ClientStream
}
func (x *brokerServiceSubscribeClient) Recv() (*Message, error) {
m := new(Message)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func (c *brokerServiceClient) BatchSubscribe(ctx context.Context, in *BatchSubscribeRequest, opts ...grpc.CallOption) (BrokerService_BatchSubscribeClient, error) {
stream, err := c.cc.NewStream(ctx, &BrokerService_ServiceDesc.Streams[1], "/servicepb.BrokerService/BatchSubscribe", opts...)
if err != nil {
return nil, err
}
x := &brokerServiceBatchSubscribeClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type BrokerService_BatchSubscribeClient interface {
Recv() (*Message, error)
grpc.ClientStream
}
type brokerServiceBatchSubscribeClient struct {
grpc.ClientStream
}
func (x *brokerServiceBatchSubscribeClient) Recv() (*Message, error) {
m := new(Message)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
// BrokerServiceServer is the server API for BrokerService service.
// All implementations must embed UnimplementedBrokerServiceServer
// for forward compatibility
type BrokerServiceServer interface {
Publish(context.Context, *PublishRequest) (*PublishResponse, error)
BatchPublish(context.Context, *BatchPublishRequest) (*BatchPublishResponse, error)
Subscribe(*SubscribeRequest, BrokerService_SubscribeServer) error
BatchSubscribe(*BatchSubscribeRequest, BrokerService_BatchSubscribeServer) error
mustEmbedUnimplementedBrokerServiceServer()
}
// UnimplementedBrokerServiceServer must be embedded to have forward compatible implementations.
type UnimplementedBrokerServiceServer struct {
}
func (UnimplementedBrokerServiceServer) Publish(context.Context, *PublishRequest) (*PublishResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method Publish not implemented")
}
func (UnimplementedBrokerServiceServer) BatchPublish(context.Context, *BatchPublishRequest) (*BatchPublishResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method BatchPublish not implemented")
}
func (UnimplementedBrokerServiceServer) Subscribe(*SubscribeRequest, BrokerService_SubscribeServer) error {
return status.Errorf(codes.Unimplemented, "method Subscribe not implemented")
}
func (UnimplementedBrokerServiceServer) BatchSubscribe(*BatchSubscribeRequest, BrokerService_BatchSubscribeServer) error {
return status.Errorf(codes.Unimplemented, "method BatchSubscribe not implemented")
}
func (UnimplementedBrokerServiceServer) mustEmbedUnimplementedBrokerServiceServer() {}
// UnsafeBrokerServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to BrokerServiceServer will
// result in compilation errors.
type UnsafeBrokerServiceServer interface {
mustEmbedUnimplementedBrokerServiceServer()
}
func RegisterBrokerServiceServer(s grpc.ServiceRegistrar, srv BrokerServiceServer) {
s.RegisterService(&BrokerService_ServiceDesc, srv)
}
func _BrokerService_Publish_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(PublishRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(BrokerServiceServer).Publish(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/servicepb.BrokerService/Publish",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(BrokerServiceServer).Publish(ctx, req.(*PublishRequest))
}
return interceptor(ctx, in, info, handler)
}
func _BrokerService_BatchPublish_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(BatchPublishRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(BrokerServiceServer).BatchPublish(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/servicepb.BrokerService/BatchPublish",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(BrokerServiceServer).BatchPublish(ctx, req.(*BatchPublishRequest))
}
return interceptor(ctx, in, info, handler)
}
func _BrokerService_Subscribe_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(SubscribeRequest)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(BrokerServiceServer).Subscribe(m, &brokerServiceSubscribeServer{stream})
}
type BrokerService_SubscribeServer interface {
Send(*Message) error
grpc.ServerStream
}
type brokerServiceSubscribeServer struct {
grpc.ServerStream
}
func (x *brokerServiceSubscribeServer) Send(m *Message) error {
return x.ServerStream.SendMsg(m)
}
func _BrokerService_BatchSubscribe_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(BatchSubscribeRequest)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(BrokerServiceServer).BatchSubscribe(m, &brokerServiceBatchSubscribeServer{stream})
}
type BrokerService_BatchSubscribeServer interface {
Send(*Message) error
grpc.ServerStream
}
type brokerServiceBatchSubscribeServer struct {
grpc.ServerStream
}
func (x *brokerServiceBatchSubscribeServer) Send(m *Message) error {
return x.ServerStream.SendMsg(m)
}
// BrokerService_ServiceDesc is the grpc.ServiceDesc for BrokerService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var BrokerService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "servicepb.BrokerService",
HandlerType: (*BrokerServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Publish",
Handler: _BrokerService_Publish_Handler,
},
{
MethodName: "BatchPublish",
Handler: _BrokerService_BatchPublish_Handler,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "Subscribe",
Handler: _BrokerService_Subscribe_Handler,
ServerStreams: true,
},
{
StreamName: "BatchSubscribe",
Handler: _BrokerService_BatchSubscribe_Handler,
ServerStreams: true,
},
},
Metadata: "broker.proto",
}

View File

@ -14,12 +14,14 @@ import (
"go.unistack.org/micro/v3/metadata"
)
var _ broker.Broker = &serviceBroker{}
type serviceBroker struct {
addrs []string
service string
client pbmicro.BrokerServiceClient
init bool
service string
opts broker.Options
addrs []string
init bool
}
func (b *serviceBroker) Address() string {
@ -96,8 +98,8 @@ func (b *serviceBroker) Publish(ctx context.Context, topic string, msg *broker.M
func (b *serviceBroker) publish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
for _, msg := range msgs {
topic, _ := msg.Header.Get(metadata.HeaderTopic)
if logger.V(logger.TraceLevel) {
logger.Tracef(ctx, "Publishing to topic %s broker %v", topic, b.addrs)
if b.opts.Logger.V(logger.TraceLevel) {
b.opts.Logger.Trace(ctx, fmt.Sprintf("Publishing to topic %s broker %v", topic, b.addrs))
}
_, err := b.client.Publish(ctx, &pb.PublishRequest{
Topic: topic,
@ -120,8 +122,8 @@ func (b *serviceBroker) BatchSubscribe(ctx context.Context, topic string, handle
func (b *serviceBroker) Subscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
options := broker.NewSubscribeOptions(opts...)
if logger.V(logger.TraceLevel) {
logger.Tracef(ctx, "Subscribing to topic %s group %s broker %v", topic, options.Group, b.addrs)
if b.opts.Logger.V(logger.TraceLevel) {
b.opts.Logger.Trace(ctx, fmt.Sprintf("Subscribing to topic %s group %s broker %v", topic, options.Group, b.addrs))
}
stream, err := b.client.Subscribe(ctx, &pb.SubscribeRequest{
Topic: topic,
@ -138,32 +140,33 @@ func (b *serviceBroker) Subscribe(ctx context.Context, topic string, handler bro
stream: stream,
closed: make(chan bool),
options: options,
opts: b.opts,
}
go func() {
for {
select {
case <-sub.closed:
if logger.V(logger.TraceLevel) {
logger.Tracef(ctx, "Unsubscribed from topic %s", topic)
if b.opts.Logger.V(logger.TraceLevel) {
b.opts.Logger.Trace(ctx, "Unsubscribed from topic %s", topic)
}
return
default:
if logger.V(logger.TraceLevel) {
if b.opts.Logger.V(logger.TraceLevel) {
// run the subscriber
logger.Tracef(ctx, "Streaming from broker %v to topic [%s] group [%s]", b.addrs, topic, options.Group)
b.opts.Logger.Trace(ctx, fmt.Sprintf("Streaming from broker %v to topic [%s] group [%s]", b.addrs, topic, options.Group))
}
if err := sub.run(ctx); err != nil {
if logger.V(logger.TraceLevel) {
logger.Tracef(ctx, "Resubscribing to topic %s broker %v", topic, b.addrs)
if b.opts.Logger.V(logger.TraceLevel) {
b.opts.Logger.Trace(ctx, fmt.Sprintf("Resubscribing to topic %s broker %v", topic, b.addrs))
}
stream, err := b.client.Subscribe(ctx, &pb.SubscribeRequest{
Topic: topic,
Group: options.Group,
}, client.WithAddress(b.addrs...), client.WithRequestTimeout(time.Hour))
if err != nil {
if logger.V(logger.TraceLevel) {
logger.Tracef(ctx, "Failed to resubscribe to topic %s: %v", topic, err)
if b.opts.Logger.V(logger.TraceLevel) {
b.opts.Logger.Trace(ctx, fmt.Sprintf("Failed to resubscribe to topic %s: %v", topic, err))
}
time.Sleep(time.Second)
continue

View File

@ -2,10 +2,12 @@ package service
import (
"context"
"fmt"
pbmicro "go.unistack.org/micro-broker-service/v3/micro"
"go.unistack.org/micro/v3/broker"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/metadata"
)
type serviceSub struct {
@ -14,15 +16,21 @@ type serviceSub struct {
handler broker.Handler
stream pbmicro.BrokerService_SubscribeClient
closed chan bool
opts broker.Options
options broker.SubscribeOptions
}
type serviceEvent struct {
ctx context.Context
topic string
err error
message *broker.Message
}
func (s *serviceEvent) Context() context.Context {
return s.ctx
}
func (s *serviceEvent) Topic() string {
return s.topic
}
@ -67,9 +75,10 @@ func (s *serviceSub) run(ctx context.Context) error {
for {
// TODO: do not fail silently
msg, err := s.stream.Recv()
ctx := metadata.NewIncomingContext(context.Background(), msg.Header)
if err != nil {
if logger.V(logger.TraceLevel) {
logger.Tracef(ctx, "Streaming error for subcription to topic %s: %v", s.Topic(), err)
if s.opts.Logger.V(logger.TraceLevel) {
s.opts.Logger.Trace(ctx, fmt.Sprintf("streaming error for subcription to topic %s: %v", s.Topic(), err))
}
// close the exit channel
@ -85,6 +94,7 @@ func (s *serviceSub) run(ctx context.Context) error {
}
p := &serviceEvent{
ctx: ctx,
topic: s.topic,
message: &broker.Message{
Header: msg.Header,