chore: stabilize lint and verify builds
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
"quyun/v2/providers/grpc"
|
||||
"quyun/v2/providers/http"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
logrus "github.com/sirupsen/logrus"
|
||||
"github.com/soheilhy/cmux"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/opt"
|
||||
@@ -35,11 +35,12 @@ func (h *Config) Address() string {
|
||||
if h.Host == nil {
|
||||
return fmt.Sprintf(":%d", h.Port)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s:%d", *h.Host, h.Port)
|
||||
}
|
||||
|
||||
type CMux struct {
|
||||
Http *http.Service
|
||||
HTTP *http.Service
|
||||
Grpc *grpc.Grpc
|
||||
Mux cmux.CMux
|
||||
Base net.Listener
|
||||
@@ -54,7 +55,7 @@ func (c *CMux) Serve() error {
|
||||
if c.Base != nil && c.Base.Addr() != nil {
|
||||
addr = c.Base.Addr().String()
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"addr": addr,
|
||||
}).Info("cmux starting")
|
||||
|
||||
@@ -70,24 +71,27 @@ func (c *CMux) Serve() error {
|
||||
|
||||
var eg errgroup.Group
|
||||
eg.Go(func() error {
|
||||
log.WithField("addr", addr).Info("grpc serving via cmux")
|
||||
logrus.WithField("addr", addr).Info("grpc serving via cmux")
|
||||
|
||||
err := c.Grpc.ServeWithListener(grpcL)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("grpc server exited with error")
|
||||
logrus.WithError(err).Error("grpc server exited with error")
|
||||
} else {
|
||||
log.Info("grpc server exited")
|
||||
logrus.Info("grpc server exited")
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
eg.Go(func() error {
|
||||
log.WithField("addr", addr).Info("http serving via cmux")
|
||||
err := c.Http.Listener(httpL)
|
||||
logrus.WithField("addr", addr).Info("http serving via cmux")
|
||||
err := c.HTTP.Listener(httpL)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("http server exited with error")
|
||||
logrus.WithError(err).Error("http server exited with error")
|
||||
} else {
|
||||
log.Info("http server exited")
|
||||
logrus.Info("http server exited")
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
@@ -95,15 +99,17 @@ func (c *CMux) Serve() error {
|
||||
eg.Go(func() error {
|
||||
err := c.Mux.Serve()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("cmux exited with error")
|
||||
logrus.WithError(err).Error("cmux exited with error")
|
||||
} else {
|
||||
log.Info("cmux exited")
|
||||
logrus.Info("cmux exited")
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
err := eg.Wait()
|
||||
if err == nil {
|
||||
log.Info("cmux and sub-servers exited cleanly")
|
||||
logrus.Info("cmux and sub-servers exited cleanly")
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -17,20 +17,21 @@ func Provide(opts ...opt.Option) error {
|
||||
if err := o.UnmarshalConfig(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
return container.Container.Provide(func(http *http.Service, grpc *grpc.Grpc) (*CMux, error) {
|
||||
l, err := net.Listen("tcp", config.Address())
|
||||
|
||||
return container.Container.Provide(func(httpSvc *http.Service, grpcSvc *grpc.Grpc) (*CMux, error) {
|
||||
listener, err := net.Listen("tcp", config.Address())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mux := &CMux{
|
||||
Http: http,
|
||||
Grpc: grpc,
|
||||
Mux: cmux.New(l),
|
||||
Base: l,
|
||||
HTTP: httpSvc,
|
||||
Grpc: grpcSvc,
|
||||
Mux: cmux.New(listener),
|
||||
Base: listener,
|
||||
}
|
||||
// Ensure cmux stops accepting new connections on shutdown
|
||||
container.AddCloseAble(func() { _ = l.Close() })
|
||||
container.AddCloseAble(func() { _ = listener.Close() })
|
||||
|
||||
return mux, nil
|
||||
}, o.DiOptions()...)
|
||||
|
||||
Reference in New Issue
Block a user