Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/server/grpc/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
)

var (
defaultMetricsServer = prometheus.NewServerMetrics()
defaultMetricsServer = prometheus.NewServerMetrics(prometheus.WithServerCounterOptions())
)

// Option set options
Expand Down Expand Up @@ -178,7 +178,7 @@ func defaultOptions() *Options {
maxConnectionAgeGrace: defaultMaxServerConnectionAgeGrace,
maxConcurrentStreams: defaultMaxConcurrentStreams,
maxMsgSize: defaultMaxMsgSize,
metrcsServer: prometheus.NewServerMetrics(),
metrcsServer: defaultMetricsServer,
unaryServerInterceptors: []grpc.UnaryServerInterceptor{
//requesttag.UnaryServerInterceptor(),
//ctxtags.UnaryServerInterceptor(),
Expand Down
7 changes: 3 additions & 4 deletions core/server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"os"
"sync"

grpcPrometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -62,16 +61,16 @@ func (e *Server) Server() *grpc.Server {

// NewServer new a server
func (e *Server) NewServer() {
grpc.EnableTracing = false
grpc.EnableTracing = true
e.srv = grpc.NewServer(e.initGrpcServerOptions()...)
prometheus.MustRegister(e.options.metrcsServer)
e.options.metrcsServer.InitializeMetrics(e.srv)
reflection.Register(e.srv)
}

// Register register
func (e *Server) Register(do func(server *Server)) {
do(e)
registry := prometheus.NewPedanticRegistry()
registry.MustRegister(grpcPrometheus.NewServerMetrics(grpcPrometheus.WithServerHandlingTimeHistogram()))
}

func (e *Server) initGrpcServerOptions() []grpc.ServerOption {
Expand Down
36 changes: 17 additions & 19 deletions core/server/listener/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

// Option 参数设置类型
type Option func(*options)
type Option func(*Options)

type options struct {
type Options struct {
name, addr, certFile, keyFile string
handler http.Handler
startedHook func()
Expand All @@ -27,97 +27,95 @@ type options struct {
pprof bool
}

func setDefaultOption() options {
return options{
func defaultOptions() *Options {
return &Options{
name: "http",
addr: ":5000",
timeout: 10 * time.Second,
handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
}),
handler: http.DefaultServeMux,
}
}

// WithName set name
func WithName(name string) Option {
return func(o *options) {
return func(o *Options) {
o.name = name
}
}

// WithMetrics set metrics
func WithMetrics(enable bool) Option {
return func(o *options) {
return func(o *Options) {
o.metrics = enable
}
}

// WithHealthz set healthz
func WithHealthz(enable bool) Option {
return func(o *options) {
return func(o *Options) {
o.healthz = enable
}
}

// WithReadyz set readyz
func WithReadyz(enable bool) Option {
return func(o *options) {
return func(o *Options) {
o.readyz = enable
}
}

// WithPprof set pprof
func WithPprof(enable bool) Option {
return func(o *options) {
return func(o *Options) {
o.pprof = enable
}
}

// WithEndHook set EndHook
func WithEndHook(f func()) Option {
return func(o *options) {
return func(o *Options) {
o.endHook = f
}
}

// WithStartedHook 设置启动回调函数
func WithStartedHook(f func()) Option {
return func(o *options) {
return func(o *Options) {
o.startedHook = f
}
}

// WithAddr 设置addr
func WithAddr(s string) Option {
return func(o *options) {
return func(o *Options) {
o.addr = s
}
}

// WithHandler 设置handler
func WithHandler(handler http.Handler) Option {
return func(o *options) {
return func(o *Options) {
o.handler = handler
}
}

// WithCert 设置cert
func WithCert(s string) Option {
return func(o *options) {
return func(o *Options) {
o.certFile = s
}
}

// WithKey 设置key
func WithKey(s string) Option {
return func(o *options) {
return func(o *Options) {
o.keyFile = s
}
}

// WithTimeout 设置timeout
func WithTimeout(t int) Option {
return func(o *options) {
return func(o *Options) {
o.timeout = time.Second * time.Duration(t)
}
}
105 changes: 64 additions & 41 deletions core/server/listener/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"log/slog"
"net"
"net/http"
_ "net/http/pprof"
"net/http/pprof"

ginPprof "github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/mss-boot-io/mss-boot/core/server"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
Expand All @@ -22,88 +24,109 @@ import (
type Server struct {
ctx context.Context
srv *http.Server
opts options
options Options
started bool
}

// New 实例化
func New(opts ...Option) server.Runnable {
s := &Server{
opts: setDefaultOption(),
}
s := &Server{}

s.opts.handler = http.DefaultServeMux
s.Options(opts...)

//if s.opts.pprof {
// http.HandleFunc("/debug/pprof/", pprof.Index)
// http.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
// http.HandleFunc("/debug/pprof/profile", pprof.Profile)
// http.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
// http.HandleFunc("/debug/pprof/trace", pprof.Trace)
//}
if s.opts.metrics {
http.Handle("/metrics", promhttp.Handler())
}
if s.opts.healthz {
http.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
if s.options.handler == nil {
return nil
}
if s.opts.readyz {
http.HandleFunc("/readyz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
switch h := s.options.handler.(type) {
case *http.ServeMux:
if s.options.pprof && h != http.DefaultServeMux {
h.HandleFunc("/debug/pprof/", pprof.Index)
h.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
h.HandleFunc("/debug/pprof/profile", pprof.Profile)
h.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
h.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
if s.options.metrics {
h.Handle("/metrics", promhttp.Handler())
}
if s.options.healthz {
h.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
}
if s.options.readyz {
h.HandleFunc("/readyz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
}
s.options.handler = h
case *gin.Engine:
if s.options.pprof {
ginPprof.Register(h)
}
if s.options.metrics {
h.GET("/metrics", gin.WrapH(promhttp.Handler()))
}
if s.options.healthz {
h.GET("/healthz", func(c *gin.Context) {
c.AbortWithStatus(http.StatusOK)
})
}
if s.options.readyz {
h.GET("/readyz", func(c *gin.Context) {
c.AbortWithStatus(http.StatusOK)
})
}
}
return s
}

// Options 设置参数
func (e *Server) Options(opts ...Option) {
for _, o := range opts {
o(&(e.opts))
func (e *Server) Options(options ...Option) {
e.options = *defaultOptions()
for _, o := range options {
o(&e.options)
}
}

// String string
func (e *Server) String() string {
return e.opts.name
return e.options.name
}

// Start server
func (e *Server) Start(ctx context.Context) error {
l, err := net.Listen("tcp", e.opts.addr)
l, err := net.Listen("tcp", e.options.addr)
if err != nil {
return err
}
e.ctx = ctx
e.started = true
e.srv = &http.Server{Handler: e.opts.handler}
if e.opts.endHook != nil {
e.srv.RegisterOnShutdown(e.opts.endHook)
e.srv = &http.Server{Handler: e.options.handler}
if e.options.endHook != nil {
e.srv.RegisterOnShutdown(e.options.endHook)
}
e.srv.BaseContext = func(_ net.Listener) context.Context {
return ctx
}
slog.InfoContext(ctx, e.opts.name+" Server listening on "+l.Addr().String())
slog.InfoContext(ctx, e.options.name+" Server listening on "+l.Addr().String())
go func() {
if e.opts.keyFile == "" || e.opts.certFile == "" {
if e.options.keyFile == "" || e.options.certFile == "" {
if err = e.srv.Serve(l); err != nil {
slog.ErrorContext(ctx, e.opts.name+" Server start error", slog.Any("err", err.Error()))
slog.ErrorContext(ctx, e.options.name+" Server start error", slog.Any("err", err.Error()))
}
} else {
if err = e.srv.ServeTLS(l, e.opts.certFile, e.opts.keyFile); err != nil {
slog.ErrorContext(ctx, e.opts.name+" Server start error", slog.Any("err", err.Error()))
if err = e.srv.ServeTLS(l, e.options.certFile, e.options.keyFile); err != nil {
slog.ErrorContext(ctx, e.options.name+" Server start error", slog.Any("err", err.Error()))
}
}
<-ctx.Done()
err = e.Shutdown(ctx)
if err != nil {
slog.ErrorContext(ctx, e.opts.name+" Server shutdown error", slog.Any("err", err.Error()))
slog.ErrorContext(ctx, e.options.name+" Server shutdown error", slog.Any("err", err.Error()))
}
}()
if e.opts.startedHook != nil {
e.opts.startedHook()
if e.options.startedHook != nil {
e.options.startedHook()
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/casbin/gorm-adapter/v3 v3.20.0
github.com/casbin/mongodb-adapter/v3 v3.5.0
github.com/coreos/go-oidc/v3 v3.7.0
github.com/gin-contrib/pprof v1.4.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/google/uuid v1.4.0
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.0
Expand Down
Loading