🎉 Initial Commit
This commit is contained in:
27
pkg/internal/grpc/health.go
Normal file
27
pkg/internal/grpc/health.go
Normal file
@ -0,0 +1,27 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
health "google.golang.org/grpc/health/grpc_health_v1"
|
||||
)
|
||||
|
||||
func (v *GrpcServer) Check(ctx context.Context, request *health.HealthCheckRequest) (*health.HealthCheckResponse, error) {
|
||||
return &health.HealthCheckResponse{
|
||||
Status: health.HealthCheckResponse_SERVING,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *GrpcServer) Watch(request *health.HealthCheckRequest, server health.Health_WatchServer) error {
|
||||
for {
|
||||
if server.Send(&health.HealthCheckResponse{
|
||||
Status: health.HealthCheckResponse_SERVING,
|
||||
}) != nil {
|
||||
break
|
||||
}
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
44
pkg/internal/grpc/server.go
Normal file
44
pkg/internal/grpc/server.go
Normal file
@ -0,0 +1,44 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/directory"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
|
||||
"google.golang.org/grpc/reflection"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
health "google.golang.org/grpc/health/grpc_health_v1"
|
||||
)
|
||||
|
||||
type GrpcServer struct {
|
||||
proto.UnimplementedStreamControllerServer
|
||||
|
||||
srv *grpc.Server
|
||||
}
|
||||
|
||||
func NewServer() *GrpcServer {
|
||||
server := &GrpcServer{
|
||||
srv: grpc.NewServer(),
|
||||
}
|
||||
|
||||
proto.RegisterServiceDirectoryServer(server.srv, &directory.DirectoryRpcServer{})
|
||||
proto.RegisterStreamControllerServer(server.srv, server)
|
||||
health.RegisterHealthServer(server.srv, server)
|
||||
|
||||
reflection.Register(server.srv)
|
||||
|
||||
return server
|
||||
}
|
||||
|
||||
func (v *GrpcServer) Listen() error {
|
||||
listener, err := net.Listen("tcp", viper.GetString("grpc_bind"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return v.srv.Serve(listener)
|
||||
}
|
86
pkg/internal/grpc/stream.go
Normal file
86
pkg/internal/grpc/stream.go
Normal file
@ -0,0 +1,86 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/internal/services"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func (v *GrpcServer) CountStreamConnection(ctx context.Context, request *proto.CountConnectionRequest) (*proto.CountConnectionResponse, error) {
|
||||
out := services.ClientCount(uint(request.GetUserId()))
|
||||
return &proto.CountConnectionResponse{
|
||||
Count: int64(out),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *GrpcServer) PushStream(ctx context.Context, request *proto.PushStreamRequest) (*proto.PushStreamResponse, error) {
|
||||
var cnt int
|
||||
var success int
|
||||
var errs []error
|
||||
if request.UserId != nil {
|
||||
cnt, success, errs = services.WebsocketPush(uint(request.GetUserId()), request.GetBody())
|
||||
} else if request.ClientId != nil {
|
||||
cnt, success, errs = services.WebsocketPushDirect(request.GetClientId(), request.GetBody())
|
||||
} else {
|
||||
return nil, fmt.Errorf("you must give one of the user id or client id")
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
// Partial fail
|
||||
return &proto.PushStreamResponse{
|
||||
IsAllSuccess: false,
|
||||
AffectedCount: int64(success),
|
||||
FailedCount: int64(cnt - success),
|
||||
}, nil
|
||||
} else if cnt > 0 && success == 0 {
|
||||
// All fail
|
||||
return nil, fmt.Errorf("all push request failed: %v", errs)
|
||||
}
|
||||
|
||||
return &proto.PushStreamResponse{
|
||||
IsAllSuccess: true,
|
||||
AffectedCount: int64(success),
|
||||
FailedCount: int64(cnt - success),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (v *GrpcServer) PushStreamBatch(ctx context.Context, request *proto.PushStreamBatchRequest) (*proto.PushStreamResponse, error) {
|
||||
var cnt int
|
||||
var success int
|
||||
var errs []error
|
||||
if len(request.UserId) != 0 {
|
||||
cnt, success, errs = services.WebsocketPushBatch(
|
||||
lo.Map(request.GetUserId(), func(item uint64, idx int) uint {
|
||||
return uint(item)
|
||||
},
|
||||
), request.GetBody(),
|
||||
)
|
||||
}
|
||||
if len(request.ClientId) != 0 {
|
||||
cCnt, cSuccess, cErrs := services.WebsocketPushBatchDirect(request.GetClientId(), request.GetBody())
|
||||
cnt += cCnt
|
||||
success += cSuccess
|
||||
errs = append(errs, cErrs...)
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
// Partial fail
|
||||
return &proto.PushStreamResponse{
|
||||
IsAllSuccess: false,
|
||||
AffectedCount: int64(success),
|
||||
FailedCount: int64(cnt - success),
|
||||
}, nil
|
||||
} else if cnt > 0 && success == 0 {
|
||||
// All fail
|
||||
return nil, fmt.Errorf("all push request failed: %v", errs)
|
||||
}
|
||||
|
||||
return &proto.PushStreamResponse{
|
||||
IsAllSuccess: true,
|
||||
AffectedCount: int64(success),
|
||||
FailedCount: int64(cnt - success),
|
||||
}, nil
|
||||
}
|
Reference in New Issue
Block a user