39 lines
787 B
Go
39 lines
787 B
Go
package cmux
|
|
|
|
import (
|
|
"net"
|
|
|
|
"quyun/v2/providers/grpc"
|
|
"quyun/v2/providers/http"
|
|
|
|
"github.com/soheilhy/cmux"
|
|
"go.ipao.vip/atom/container"
|
|
"go.ipao.vip/atom/opt"
|
|
)
|
|
|
|
func Provide(opts ...opt.Option) error {
|
|
o := opt.New(opts...)
|
|
var config Config
|
|
if err := o.UnmarshalConfig(&config); err != nil {
|
|
return err
|
|
}
|
|
|
|
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: httpSvc,
|
|
Grpc: grpcSvc,
|
|
Mux: cmux.New(listener),
|
|
Base: listener,
|
|
}
|
|
// Ensure cmux stops accepting new connections on shutdown
|
|
container.AddCloseAble(func() { _ = listener.Close() })
|
|
|
|
return mux, nil
|
|
}, o.DiOptions()...)
|
|
}
|