diff --git a/pkg/internal/services/connections.go b/pkg/internal/services/connections.go index 408ffa9..6729f77 100644 --- a/pkg/internal/services/connections.go +++ b/pkg/internal/services/connections.go @@ -1,9 +1,13 @@ package services import ( + "context" "sync" + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "git.solsynth.dev/hydrogen/dealer/pkg/internal/directory" "git.solsynth.dev/hydrogen/dealer/pkg/internal/models" + "git.solsynth.dev/hydrogen/dealer/pkg/proto" "github.com/gofiber/contrib/websocket" ) @@ -19,6 +23,14 @@ func ClientRegister(user models.Account, conn *websocket.Conn) { } wsConn[user.ID][conn] = true wsMutex.Unlock() + + pc, err := directory.GetServiceInstanceByType(hyper.ServiceTypeAuthProvider).GetGrpcConn() + if err == nil { + proto.NewStreamControllerClient(pc).EmitStreamEvent(context.Background(), &proto.StreamEventRequest{ + Event: "ClientRegister", + UserId: uint64(user.ID), + }) + } } func ClientUnregister(user models.Account, conn *websocket.Conn) { @@ -28,6 +40,14 @@ func ClientUnregister(user models.Account, conn *websocket.Conn) { } delete(wsConn[user.ID], conn) wsMutex.Unlock() + + pc, err := directory.GetServiceInstanceByType(hyper.ServiceTypeAuthProvider).GetGrpcConn() + if err == nil { + proto.NewStreamControllerClient(pc).EmitStreamEvent(context.Background(), &proto.StreamEventRequest{ + Event: "ClientUnregister", + UserId: uint64(user.ID), + }) + } } func ClientCount(uid uint) int { diff --git a/pkg/proto/stream.pb.go b/pkg/proto/stream.pb.go index b3dfb12..95afa84 100644 --- a/pkg/proto/stream.pb.go +++ b/pkg/proto/stream.pb.go @@ -287,6 +287,99 @@ func (x *PushStreamResponse) GetFailedCount() int64 { return 0 } +type StreamEventRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Event string `protobuf:"bytes,1,opt,name=event,proto3" json:"event,omitempty"` + UserId uint64 `protobuf:"varint,2,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` +} + +func (x *StreamEventRequest) Reset() { + *x = StreamEventRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_stream_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *StreamEventRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*StreamEventRequest) ProtoMessage() {} + +func (x *StreamEventRequest) ProtoReflect() protoreflect.Message { + mi := &file_stream_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use StreamEventRequest.ProtoReflect.Descriptor instead. +func (*StreamEventRequest) Descriptor() ([]byte, []int) { + return file_stream_proto_rawDescGZIP(), []int{5} +} + +func (x *StreamEventRequest) GetEvent() string { + if x != nil { + return x.Event + } + return "" +} + +func (x *StreamEventRequest) GetUserId() uint64 { + if x != nil { + return x.UserId + } + return 0 +} + +type StreamEventResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *StreamEventResponse) Reset() { + *x = StreamEventResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_stream_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *StreamEventResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*StreamEventResponse) ProtoMessage() {} + +func (x *StreamEventResponse) ProtoReflect() protoreflect.Message { + mi := &file_stream_proto_msgTypes[6] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use StreamEventResponse.ProtoReflect.Descriptor instead. +func (*StreamEventResponse) Descriptor() ([]byte, []int) { + return file_stream_proto_rawDescGZIP(), []int{6} +} + var File_stream_proto protoreflect.FileDescriptor var file_stream_proto_rawDesc = []byte{ @@ -314,24 +407,35 @@ var file_stream_proto_rawDesc = []byte{ 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, 0x61, 0x66, 0x66, 0x65, 0x63, 0x74, 0x65, 0x64, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x21, 0x0a, 0x0c, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0b, 0x66, 0x61, - 0x69, 0x6c, 0x65, 0x64, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x32, 0x80, 0x02, 0x0a, 0x10, 0x53, 0x74, - 0x72, 0x65, 0x61, 0x6d, 0x43, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x12, 0x58, - 0x0a, 0x15, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x43, 0x6f, 0x6e, - 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1d, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, - 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, - 0x6f, 0x75, 0x6e, 0x74, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, - 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x43, 0x0a, 0x0a, 0x50, 0x75, 0x73, 0x68, - 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x18, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, - 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x19, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, - 0x65, 0x61, 0x6d, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x4d, 0x0a, - 0x0f, 0x50, 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x42, 0x61, 0x74, 0x63, 0x68, - 0x12, 0x1d, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, - 0x65, 0x61, 0x6d, 0x42, 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, - 0x19, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, 0x65, - 0x61, 0x6d, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, - 0x2e, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x69, 0x6c, 0x65, 0x64, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x43, 0x0a, 0x12, 0x53, 0x74, 0x72, + 0x65, 0x61, 0x6d, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, + 0x14, 0x0a, 0x05, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, + 0x65, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x22, 0x15, + 0x0a, 0x13, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0xcc, 0x02, 0x0a, 0x10, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, + 0x43, 0x6f, 0x6e, 0x74, 0x72, 0x6f, 0x6c, 0x6c, 0x65, 0x72, 0x12, 0x58, 0x0a, 0x15, 0x43, 0x6f, + 0x75, 0x6e, 0x74, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, + 0x69, 0x6f, 0x6e, 0x12, 0x1d, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x75, 0x6e, + 0x74, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x1e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x75, 0x6e, 0x74, + 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x22, 0x00, 0x12, 0x43, 0x0a, 0x0a, 0x50, 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, 0x65, + 0x61, 0x6d, 0x12, 0x18, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x75, 0x73, 0x68, 0x53, + 0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x4d, 0x0a, 0x0f, 0x50, 0x75, 0x73, + 0x68, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x42, 0x61, 0x74, 0x63, 0x68, 0x12, 0x1d, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x42, + 0x61, 0x74, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x75, 0x73, 0x68, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x4a, 0x0a, 0x0f, 0x45, 0x6d, 0x69, 0x74, + 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x19, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x2e, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1a, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x53, + 0x74, 0x72, 0x65, 0x61, 0x6d, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -346,23 +450,27 @@ func file_stream_proto_rawDescGZIP() []byte { return file_stream_proto_rawDescData } -var file_stream_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_stream_proto_msgTypes = make([]protoimpl.MessageInfo, 7) var file_stream_proto_goTypes = []any{ (*CountConnectionRequest)(nil), // 0: proto.CountConnectionRequest (*CountConnectionResponse)(nil), // 1: proto.CountConnectionResponse (*PushStreamRequest)(nil), // 2: proto.PushStreamRequest (*PushStreamBatchRequest)(nil), // 3: proto.PushStreamBatchRequest (*PushStreamResponse)(nil), // 4: proto.PushStreamResponse + (*StreamEventRequest)(nil), // 5: proto.StreamEventRequest + (*StreamEventResponse)(nil), // 6: proto.StreamEventResponse } var file_stream_proto_depIdxs = []int32{ 0, // 0: proto.StreamController.CountStreamConnection:input_type -> proto.CountConnectionRequest 2, // 1: proto.StreamController.PushStream:input_type -> proto.PushStreamRequest 3, // 2: proto.StreamController.PushStreamBatch:input_type -> proto.PushStreamBatchRequest - 1, // 3: proto.StreamController.CountStreamConnection:output_type -> proto.CountConnectionResponse - 4, // 4: proto.StreamController.PushStream:output_type -> proto.PushStreamResponse - 4, // 5: proto.StreamController.PushStreamBatch:output_type -> proto.PushStreamResponse - 3, // [3:6] is the sub-list for method output_type - 0, // [0:3] is the sub-list for method input_type + 5, // 3: proto.StreamController.EmitStreamEvent:input_type -> proto.StreamEventRequest + 1, // 4: proto.StreamController.CountStreamConnection:output_type -> proto.CountConnectionResponse + 4, // 5: proto.StreamController.PushStream:output_type -> proto.PushStreamResponse + 4, // 6: proto.StreamController.PushStreamBatch:output_type -> proto.PushStreamResponse + 6, // 7: proto.StreamController.EmitStreamEvent:output_type -> proto.StreamEventResponse + 4, // [4:8] is the sub-list for method output_type + 0, // [0:4] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name @@ -434,6 +542,30 @@ func file_stream_proto_init() { return nil } } + file_stream_proto_msgTypes[5].Exporter = func(v any, i int) any { + switch v := v.(*StreamEventRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_stream_proto_msgTypes[6].Exporter = func(v any, i int) any { + switch v := v.(*StreamEventResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -441,7 +573,7 @@ func file_stream_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_stream_proto_rawDesc, NumEnums: 0, - NumMessages: 5, + NumMessages: 7, NumExtensions: 0, NumServices: 1, }, diff --git a/pkg/proto/stream.proto b/pkg/proto/stream.proto index dea9faf..0ec8aa2 100644 --- a/pkg/proto/stream.proto +++ b/pkg/proto/stream.proto @@ -8,6 +8,7 @@ service StreamController { rpc CountStreamConnection(CountConnectionRequest) returns (CountConnectionResponse) {} rpc PushStream(PushStreamRequest) returns (PushStreamResponse) {} rpc PushStreamBatch(PushStreamBatchRequest) returns (PushStreamResponse) {} + rpc EmitStreamEvent(StreamEventRequest) returns (StreamEventResponse) {} } message CountConnectionRequest { @@ -33,3 +34,11 @@ message PushStreamResponse { int64 affected_count = 2; int64 failed_count = 3; } + +message StreamEventRequest { + string event = 1; + uint64 user_id = 2; +} + +message StreamEventResponse { +} diff --git a/pkg/proto/stream_grpc.pb.go b/pkg/proto/stream_grpc.pb.go index a0b71f5..ef641bd 100644 --- a/pkg/proto/stream_grpc.pb.go +++ b/pkg/proto/stream_grpc.pb.go @@ -22,6 +22,7 @@ const ( StreamController_CountStreamConnection_FullMethodName = "/proto.StreamController/CountStreamConnection" StreamController_PushStream_FullMethodName = "/proto.StreamController/PushStream" StreamController_PushStreamBatch_FullMethodName = "/proto.StreamController/PushStreamBatch" + StreamController_EmitStreamEvent_FullMethodName = "/proto.StreamController/EmitStreamEvent" ) // StreamControllerClient is the client API for StreamController service. @@ -31,6 +32,7 @@ type StreamControllerClient interface { CountStreamConnection(ctx context.Context, in *CountConnectionRequest, opts ...grpc.CallOption) (*CountConnectionResponse, error) PushStream(ctx context.Context, in *PushStreamRequest, opts ...grpc.CallOption) (*PushStreamResponse, error) PushStreamBatch(ctx context.Context, in *PushStreamBatchRequest, opts ...grpc.CallOption) (*PushStreamResponse, error) + EmitStreamEvent(ctx context.Context, in *StreamEventRequest, opts ...grpc.CallOption) (*StreamEventResponse, error) } type streamControllerClient struct { @@ -71,6 +73,16 @@ func (c *streamControllerClient) PushStreamBatch(ctx context.Context, in *PushSt return out, nil } +func (c *streamControllerClient) EmitStreamEvent(ctx context.Context, in *StreamEventRequest, opts ...grpc.CallOption) (*StreamEventResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(StreamEventResponse) + err := c.cc.Invoke(ctx, StreamController_EmitStreamEvent_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + // StreamControllerServer is the server API for StreamController service. // All implementations must embed UnimplementedStreamControllerServer // for forward compatibility @@ -78,6 +90,7 @@ type StreamControllerServer interface { CountStreamConnection(context.Context, *CountConnectionRequest) (*CountConnectionResponse, error) PushStream(context.Context, *PushStreamRequest) (*PushStreamResponse, error) PushStreamBatch(context.Context, *PushStreamBatchRequest) (*PushStreamResponse, error) + EmitStreamEvent(context.Context, *StreamEventRequest) (*StreamEventResponse, error) mustEmbedUnimplementedStreamControllerServer() } @@ -94,6 +107,9 @@ func (UnimplementedStreamControllerServer) PushStream(context.Context, *PushStre func (UnimplementedStreamControllerServer) PushStreamBatch(context.Context, *PushStreamBatchRequest) (*PushStreamResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method PushStreamBatch not implemented") } +func (UnimplementedStreamControllerServer) EmitStreamEvent(context.Context, *StreamEventRequest) (*StreamEventResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method EmitStreamEvent not implemented") +} func (UnimplementedStreamControllerServer) mustEmbedUnimplementedStreamControllerServer() {} // UnsafeStreamControllerServer may be embedded to opt out of forward compatibility for this service. @@ -161,6 +177,24 @@ func _StreamController_PushStreamBatch_Handler(srv interface{}, ctx context.Cont return interceptor(ctx, in, info, handler) } +func _StreamController_EmitStreamEvent_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(StreamEventRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(StreamControllerServer).EmitStreamEvent(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: StreamController_EmitStreamEvent_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(StreamControllerServer).EmitStreamEvent(ctx, req.(*StreamEventRequest)) + } + return interceptor(ctx, in, info, handler) +} + // StreamController_ServiceDesc is the grpc.ServiceDesc for StreamController service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -180,6 +214,10 @@ var StreamController_ServiceDesc = grpc.ServiceDesc{ MethodName: "PushStreamBatch", Handler: _StreamController_PushStreamBatch_Handler, }, + { + MethodName: "EmitStreamEvent", + Handler: _StreamController_EmitStreamEvent_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "stream.proto",