代码拉取完成,页面将自动刷新
package grpc
import (
"context"
"crypto/tls"
"gitee.com/llakcs/agile-go/log"
"gitee.com/llakcs/agile-go/middleware"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
"net"
"time"
)
// ServerOption is gRPC server option.
type ServerOption func(o *Server)
// Address with server address.
func Address(addr string) ServerOption {
return func(s *Server) {
s.address = addr
}
}
// Timeout with server timeout.
func Timeout(timeout time.Duration) ServerOption {
return func(s *Server) {
s.timeout = timeout
}
}
func TLSConfig(t *tls.Config) ServerOption {
return func(o *Server) {
o.tls = t
}
}
func CustomHealth() ServerOption {
return func(s *Server) {
s.customHealth = true
}
}
// UnaryInterceptor returns a ServerOption that sets the UnaryServerInterceptor for the server.
func UnaryInterceptor(in ...grpc.UnaryServerInterceptor) ServerOption {
return func(s *Server) {
s.unaryInts = in
}
}
// StreamInterceptor returns a ServerOption that sets the StreamServerInterceptor for the server.
func StreamInterceptor(in ...grpc.StreamServerInterceptor) ServerOption {
return func(s *Server) {
s.streamInts = in
}
}
// Options with grpc options.
func Options(opts ...grpc.ServerOption) ServerOption {
return func(s *Server) {
s.grpcOpts = opts
}
}
func (s *Server) Use(selector string, m ...middleware.Middleware) {
s.middleware.Add(selector, m...)
}
type Server struct {
*grpc.Server
health *health.Server
baseCtx context.Context
address string
tls *tls.Config
timeout time.Duration
unaryInts []grpc.UnaryServerInterceptor
streamInts []grpc.StreamServerInterceptor
grpcOpts []grpc.ServerOption
middleware middleware.Matcher
customHealth bool
}
func NewServer(opts ...ServerOption) *Server {
srv := &Server{
baseCtx: context.Background(),
middleware: middleware.New(),
address: ":0",
timeout: 5 * time.Second,
health: health.NewServer(),
}
for _, o := range opts {
o(srv)
}
unaryInts := []grpc.UnaryServerInterceptor{
srv.unaryServerInterceptor(),
}
streamInts := []grpc.StreamServerInterceptor{
srv.streamServerInterceptor(),
}
if len(srv.unaryInts) > 0 {
unaryInts = append(unaryInts, srv.unaryInts...)
}
if len(srv.streamInts) > 0 {
streamInts = append(streamInts, srv.streamInts...)
}
grpcOpts := []grpc.ServerOption{
grpc.ChainUnaryInterceptor(unaryInts...),
grpc.ChainStreamInterceptor(streamInts...),
}
if srv.tls != nil {
grpcOpts = append(grpcOpts, grpc.Creds(credentials.NewTLS(srv.tls)))
}
if len(srv.grpcOpts) > 0 {
grpcOpts = append(grpcOpts, srv.grpcOpts...)
}
srv.Server = grpc.NewServer(grpcOpts...)
if !srv.customHealth {
grpc_health_v1.RegisterHealthServer(srv.Server, srv.health)
}
reflection.Register(srv.Server)
return srv
}
func (s *Server) Start(ctx context.Context) error {
lis, err := net.Listen("tcp", s.address)
if err != nil {
return err
}
log.Infof("[gRPC] server listening on: %s", lis.Addr().String())
s.health.Resume()
return s.Serve(lis)
}
func (s *Server) Stop(ctx context.Context) error {
s.health.Shutdown()
s.GracefulStop()
log.Info("[gRPC] server stopping")
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。