Skip to content

Commit

Permalink
fix: remove applies count check
Browse files Browse the repository at this point in the history
  • Loading branch information
GGXXLL committed Jun 4, 2021
1 parent b7d376d commit e13c343
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 46 deletions.
2 changes: 1 addition & 1 deletion c_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestC_NoServe(t *testing.T) {
defer cancel()
e := c.Serve(ctx)
assert.NoError(t, e)
assert.Equal(t, int32(0), atomic.LoadInt32(&called))
assert.Equal(t, int32(4), atomic.LoadInt32(&called))
}

func TestC_ServeDisable(t *testing.T) {
Expand Down
12 changes: 4 additions & 8 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,18 @@ type Container struct {

// ApplyRouter iterates through every HTTPProvider registered in the container,
// and introduce the router to everyone.
func (c *Container) ApplyRouter(router *mux.Router) int {
func (c *Container) ApplyRouter(router *mux.Router) {
for _, p := range c.httpProviders {
p(router)
}
return len(c.httpProviders)
}

// ApplyGRPCServer iterates through every GRPCProvider registered in the container,
// and introduce a *grpc.Server to everyone.
func (c *Container) ApplyGRPCServer(server *grpc.Server) int {
func (c *Container) ApplyGRPCServer(server *grpc.Server) {
for _, p := range c.grpcProviders {
p(server)
}
return len(c.grpcProviders)
}

// Shutdown iterates through every CloserProvider registered in the container,
Expand All @@ -94,11 +92,10 @@ func (c *Container) Shutdown() {

// ApplyRunGroup iterates through every RunProvider registered in the container,
// and introduce the *run.Group to everyone.
func (c *Container) ApplyRunGroup(g *run.Group) int {
func (c *Container) ApplyRunGroup(g *run.Group) {
for _, p := range c.runProviders {
p(g)
}
return len(c.runProviders)
}

// Modules returns all modules in the container. This method is used to scan for
Expand All @@ -122,11 +119,10 @@ func (c *Container) Modules() ifilter.Collection {

// ApplyCron iterates through every CronProvider registered in the container,
// and introduce the *cron.Cron to everyone.
func (c *Container) ApplyCron(crontab *cron.Cron) int {
func (c *Container) ApplyCron(crontab *cron.Cron) {
for _, p := range c.cronProviders {
p(crontab)
}
return len(c.cronProviders)
}

// ApplyRootCommand iterates through every CommandProvider registered in the container,
Expand Down
8 changes: 4 additions & 4 deletions contract/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

// Container holds modules.
type Container interface {
ApplyRouter(router *mux.Router) int
ApplyGRPCServer(server *grpc.Server) int
ApplyCron(crontab *cron.Cron) int
ApplyRunGroup(g *run.Group) int
ApplyRouter(router *mux.Router)
ApplyGRPCServer(server *grpc.Server)
ApplyCron(crontab *cron.Cron)
ApplyRunGroup(g *run.Group)
ApplyRootCommand(command *cobra.Command)
Shutdown()
Modules() ifilter.Collection
Expand Down
39 changes: 7 additions & 32 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/gorilla/mux"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/oklog/run"
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
Expand Down Expand Up @@ -65,10 +64,8 @@ func (s serveIn) httpServe(ctx context.Context, logger logging.LevelLogger) (fun
s.HTTPServer = &http.Server{}
}
router := mux.NewRouter()
if s.Container.ApplyRouter(router) == 0 {
logger.Info("no http service to apply")
return nil, nil, nil
}
s.Container.ApplyRouter(router)

router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
tpl, _ := route.GetPathTemplate()
level.Debug(logger).Log("service", "http", "path", tpl)
Expand Down Expand Up @@ -104,16 +101,9 @@ func (s serveIn) grpcServe(ctx context.Context, logger logging.LevelLogger) (fun
return nil, nil, nil
}
if s.GRPCServer == nil {
opts := []grpc.ServerOption{
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
}
s.GRPCServer = grpc.NewServer(opts...)
}
if s.Container.ApplyGRPCServer(s.GRPCServer) == 0 {
logger.Info("no grpc service to apply")
return nil, nil, nil
s.GRPCServer = grpc.NewServer()
}
s.Container.ApplyGRPCServer(s.GRPCServer)

for module, info := range s.GRPCServer.GetServiceInfo() {
for _, method := range info.Methods {
Expand Down Expand Up @@ -150,11 +140,8 @@ func (s serveIn) cronServe(ctx context.Context, logger logging.LevelLogger) (fun
if s.Cron == nil {
s.Cron = cron.New(cron.WithLogger(cronopts.CronLogAdapter{Logging: s.Logger}))
}
s.Container.ApplyCron(s.Cron)

if s.Container.ApplyCron(s.Cron) == 0 {
logger.Info("no cron service to apply")
return nil, nil, nil
}
return func() error {
logger.Infof("cron runner started")
s.Cron.Run()
Expand Down Expand Up @@ -191,10 +178,7 @@ func newServeCmd(s serveIn) *cobra.Command {
g run.Group
l = logging.WithLevel(s.Logger)
)
if len(s.Container.Modules()) == 0 {
l.Warn("there are no modules to run, please provide")
return nil
}

for _, m := range s.Container.Modules() {
l.Debugf("load module: %T", m)
}
Expand All @@ -207,28 +191,19 @@ func newServeCmd(s serveIn) *cobra.Command {
s.signalWatch,
}

serveCount := len(serves)

for _, serve := range serves {
execute, interrupt, err := serve(cmd.Context(), l)
if err != nil {
return err
}
if execute == nil {
serveCount--
continue
}
g.Add(execute, interrupt)
}

// Additional run groups
serveCount += s.Container.ApplyRunGroup(&g)

// At least one serve: signalWatch. It doesn't need to run alone
if serveCount <= 1 {
l.Warn("there are no services to run, please check module")
return nil
}
s.Container.ApplyRunGroup(&g)

if err := g.Run(); err != nil {
return err
Expand Down
9 changes: 8 additions & 1 deletion srvgrpc/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import (
)

// MetricsModule exposes prometheus metrics. Here only provides a simple call,
// more complex use, please refer to github.com/grpc-ecosystem/go-grpc-prometheus
// more complex use, please refer to github.com/grpc-ecosystem/go-grpc-prometheus.
//
// Need to actively provide grpc.Server:
// opts := []grpc.ServerOption{
// grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
// grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
// }
// server = grpc.NewServer(opts...)
type MetricsModule struct{}

// ProvideGRPC implements container.GRPCProvider
Expand Down

0 comments on commit e13c343

Please sign in to comment.