-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor gRPC implementation in block package (#20)
- Loading branch information
Showing
4 changed files
with
90 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,59 @@ | ||
package grpc | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/blackhorseya/ryze/adapter/block/wirex" | ||
"github.com/blackhorseya/ryze/app/infra/transports/grpcx" | ||
"github.com/blackhorseya/ryze/entity/domain/block/model" | ||
"github.com/blackhorseya/ryze/pkg/adapterx" | ||
"github.com/blackhorseya/ryze/pkg/contextx" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type grpc struct { | ||
type impl struct { | ||
injector *wirex.Injector | ||
server *grpcx.Server | ||
} | ||
|
||
// NewGRPC creates a new impl service. | ||
func NewGRPC(injector *wirex.Injector, server *grpcx.Server) adapterx.Service { | ||
return &impl{ | ||
injector: injector, | ||
server: server, | ||
} | ||
} | ||
|
||
// NewGRPC creates a new grpc service. | ||
func NewGRPC(injector *wirex.Injector) adapterx.Service { | ||
return &grpc{} | ||
func (i *impl) Start() error { | ||
ctx := contextx.Background() | ||
|
||
err := i.server.Start(ctx) | ||
if err != nil { | ||
ctx.Error("Failed to start grpc server", zap.Error(err)) | ||
return err | ||
} | ||
|
||
ctx.Info("start grpc server") | ||
|
||
return nil | ||
} | ||
|
||
func (i *grpc) Start() error { | ||
// TODO: 2024/7/29|sean|add grpc logic here | ||
panic("implement me") | ||
func (i *impl) AwaitSignal() error { | ||
ctx := contextx.Background() | ||
ctx.Info("receive signal to stop server") | ||
|
||
if err := i.server.Stop(ctx); err != nil { | ||
ctx.Error("Failed to stop server", zap.Error(err)) | ||
return fmt.Errorf("failed to stop server: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *grpc) AwaitSignal() error { | ||
// TODO: 2024/7/29|sean|add grpc logic here | ||
panic("implement me") | ||
// NewInitServersFn creates a new impl server init function. | ||
func NewInitServersFn(injector *wirex.Injector) grpcx.InitServers { | ||
return func(s *grpc.Server) { | ||
model.RegisterBlockServiceServer(s, injector.BlockService) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//go:build external | ||
|
||
package grpc | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
service, err := New(viper.New()) | ||
if err != nil { | ||
t.Fatalf("New() error = %v", err) | ||
} | ||
|
||
err = service.Start() | ||
if err != nil { | ||
t.Fatalf("Start() error = %v", err) | ||
} | ||
|
||
signalChan := make(chan os.Signal, 1) | ||
signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT) | ||
|
||
<-signalChan | ||
|
||
err = service.AwaitSignal() | ||
if err != nil { | ||
t.Fatalf("AwaitSignal() error = %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.