Skip to content

Commit

Permalink
pass newHandler for ServeHTTP instead of http.Handler. add pprof sect…
Browse files Browse the repository at this point in the history
…ion to servers. move grpc section to base server.
  • Loading branch information
vany-egorov committed Oct 9, 2022
1 parent c2c25a2 commit 638ba34
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
linters:
enable-all: true
3 changes: 3 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

const (
defaultUNIXSocketFileMode os.FileMode = 0o666

// defaultPprofPrefix url prefix of pprof.
defaultPprofPrefix = "/debug/pprof"
)

var (
Expand Down
7 changes: 6 additions & 1 deletion kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var kindText = map[Kind]string{
KindGRPC: "gRPC",
}

// is power of two algorithm
// IsSingle is power of two algorithm.
func (knd Kind) IsSingle() bool {
return !knd.IsEmpty() && (knd&(knd-1) == 0)
}
Expand All @@ -45,7 +45,9 @@ func (knd Kind) String() string {
if knd.IsEmpty() {
return "~"
}

s, _ := knd.MarshalJSON()

return string(s)
}

Expand Down Expand Up @@ -100,12 +102,15 @@ func (knd Kind) ToStringSlice() (vv []string) {
if knd.Has(KindINET) {
vv = append(vv, kindText[KindINET])
}

if knd.Has(KindUNIX) {
vv = append(vv, kindText[KindUNIX])
}

if knd.Has(KindHTTP) {
vv = append(vv, kindText[KindHTTP])
}

if knd.Has(KindGRPC) {
vv = append(vv, kindText[KindGRPC])
}
Expand Down
5 changes: 3 additions & 2 deletions listen-serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (it iterator) Listen(fnArgs ...Arg) (ss Servers, errs []error) {
return ss, errs
}

func (it iterator) ServeHTTP(handler http.Handler, fnArgs ...Arg) error {
func (it iterator) ServeHTTP(fnNewHandler func(Server) http.Handler, fnArgs ...Arg) error {
it = it.FilterListener()

cfg := args{}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (it iterator) ServeHTTP(handler http.Handler, fnArgs ...Arg) error {

server := &http.Server{
Addr: addr,
Handler: handler,
Handler: fnNewHandler(s),
}

go func() {
Expand All @@ -161,6 +161,7 @@ func (it iterator) ServeHTTP(handler http.Handler, fnArgs ...Arg) error {

if err := server.Shutdown(ctxTimeout); err != nil {
fnLog(log.Info, "server (:addr %s) shutdown failed: %s", addr, err)

return
}

Expand Down
41 changes: 40 additions & 1 deletion server-base.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package servers

import (
"fmt"
"io"

"github.com/go-x-pkg/dumpctx"
)

type WithKind struct {
Knd Kind `json:"kind" yaml:"kind" bson:"kind"`
}
Expand Down Expand Up @@ -32,6 +39,15 @@ type WithNetwork struct {
type ServerBase struct {
WithKind `json:",inline" yaml:",inline" bson:",inline"`
WithNetwork `json:",inline" yaml:",inline" bson:",inline"`

GRPC struct {
Reflection bool `yaml:"reflection"`
} `yaml:"grpc"`

Pprof struct {
Enable bool `yaml:"enable"`
Prefix string `yaml:"prefix"`
} `yaml:"pprof"`
}

func (s *ServerBase) Network() string {
Expand All @@ -51,5 +67,28 @@ func (s *ServerBase) validate() error {
}

func (s *ServerBase) defaultize() error {
return s.WithKind.defaultize()
if err := s.WithKind.defaultize(); err != nil {
return err
}

if s.Pprof.Prefix == "" {
s.Pprof.Prefix = defaultPprofPrefix
}

return nil
}

func (s *ServerBase) Dump(ctx *dumpctx.Ctx, w io.Writer) {
fmt.Fprintf(w, "%sgrpc:\n", ctx.Indent())

ctx.Wrap(func() {
fmt.Fprintf(w, "%sreflection: %t\n", ctx.Indent(), s.GRPC.Reflection)
})

fmt.Fprintf(w, "%spprof:\n", ctx.Indent())

ctx.Wrap(func() {
fmt.Fprintf(w, "%senable: %t\n", ctx.Indent(), s.Pprof.Enable)
fmt.Fprintf(w, "%sprefix: %q\n", ctx.Indent(), s.Pprof.Prefix)
})
}
21 changes: 8 additions & 13 deletions server-inet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ type ServerINET struct {

TLS struct {
Enable bool `yaml:"enable"`
CertFile string `yaml:"cert-file"`
KeyFile string `yaml:"key-file"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
} `yaml:"tls"`

GRPC struct {
Reflection bool `yaml:"reflection"`
} `yaml:"grpc"`
}

func (s *ServerINET) Base() *ServerBase { return &s.ServerBase }

func (s *ServerINET) setPort(v int) { s.Port = v }

func (s *ServerINET) Addr() string {
Expand Down Expand Up @@ -66,19 +64,16 @@ func (s *ServerINET) validate() error {
func (s *ServerINET) Dump(ctx *dumpctx.Ctx, w io.Writer) {
fmt.Fprintf(w, "%shost: %s\n", ctx.Indent(), s.Host)
fmt.Fprintf(w, "%sport: %d\n", ctx.Indent(), s.Port)

if s.TLS.Enable {
fmt.Fprintf(w, "%stls:\n", ctx.Indent())

ctx.Wrap(func() {
fmt.Fprintf(w, "%senable: %t\n", ctx.Indent(), s.TLS.Enable)
fmt.Fprintf(w, "%scert-file: %s\n", ctx.Indent(), s.TLS.CertFile)
fmt.Fprintf(w, "%skey-file: %s\n", ctx.Indent(), s.TLS.KeyFile)
fmt.Fprintf(w, "%scertFile: %s\n", ctx.Indent(), s.TLS.CertFile)
fmt.Fprintf(w, "%skeyFile: %s\n", ctx.Indent(), s.TLS.KeyFile)
})
}

fmt.Fprintf(w, "%sgrpc:\n", ctx.Indent())

ctx.Wrap(func() {
fmt.Fprintf(w, "%sreflection: %t\n", ctx.Indent(), s.GRPC.Reflection)
})
s.ServerBase.Dump(ctx, w)
}
8 changes: 6 additions & 2 deletions server-unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ type ServerUNIX struct {
ServerBase `json:",inline" yaml:",inline" bson:",inline"`

Address string `yaml:"addr"`
SocketFileMode os.FileMode `yaml:"socket-file-mode"`
SocketFileMode os.FileMode `yaml:"socketFileMode"`
}

func (s *ServerUNIX) Base() *ServerBase { return &s.ServerBase }

func (s *ServerUNIX) Addr() string { return s.Address }

func (s *ServerUNIX) validate() error {
Expand Down Expand Up @@ -52,5 +54,7 @@ func (s *ServerUNIX) defaultize() error {

func (s *ServerUNIX) Dump(ctx *dumpctx.Ctx, w io.Writer) {
fmt.Fprintf(w, "%saddr: %s\n", ctx.Indent(), s.Addr())
fmt.Fprintf(w, "%ssocket-file-mode: %03o | %s\n", ctx.Indent(), s.SocketFileMode, s.SocketFileMode)
fmt.Fprintf(w, "%ssocketFileMode: %03o | %s\n", ctx.Indent(), s.SocketFileMode, s.SocketFileMode)

s.ServerBase.Dump(ctx, w)
}
2 changes: 1 addition & 1 deletion server-wrapped.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ServerWrapped struct {
}

func (sw *ServerWrapped) unmarshal(fn func(interface{}) error) error {
wk := WithKind{}
var wk WithKind

if err := fn(&wk); err != nil {
return fmt.Errorf("error unmarshal server-head-kind: %w", err)
Expand Down
17 changes: 9 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
)

type Server interface {
serverBaser
serverKinder
serverAddred
serverNetworker
}

type (
serverBaser interface{ Base() *ServerBase }
serverKinder interface{ Kind() Kind }
serverAddred interface{ Addr() string }
serverNetworker interface{ Network() string }
Expand All @@ -24,18 +26,17 @@ type (
serverDumper interface{ Dump(*dumpctx.Ctx, io.Writer) }
)

func runLogPrefix(s Server) string {
if s.Kind().IsEmpty() {
return "!!!"
} else if s.Kind().Has(KindUNIX) {
func runLogPrefix(server Server) string {
switch s := server.(type) {
case *ServerUNIX:
return "UNIX"
} else if s.Kind().Has(KindINET) {
if inet, ok := s.(*ServerINET); ok && inet.TLS.Enable {
case *ServerINET:
if s.TLS.Enable {
return "TLS"
}

return "TCP"
default:
panic("undefined server")
}

return "???"
}
37 changes: 13 additions & 24 deletions servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func (it iterator) Defaultize(inetHost string, inetPort int, unixAddr string) (e
if inet.Port == 0 {
inet.Port = inetPort
}

}

return err
Expand Down Expand Up @@ -167,41 +166,31 @@ func (ss Servers) Dump(ctx *dumpctx.Ctx, w io.Writer) { ss.IntoIter().Dump(ctx,

func (ss Servers) Validate() error { return ss.IntoIter().Validate() }

func (ss *Servers) PushINETIfNotExists(host string, port int) {
func (ss *Servers) PushINETIfNotExists(host string, port int, kind Kind) {
if ss.IntoIter().FilterInet().Len() != 0 {
return
}

s := &ServerINET{
ServerBase: ServerBase{
WithKind: WithKind{
Knd: KindINET,
},
},
var server ServerINET

Host: host,
Port: port,
}
server.Knd = KindINET
server.Host = host
server.Port = port

*ss = append(*ss, serverEnsureWrapped(s))
*ss = append(*ss, serverEnsureWrapped(&server))
}

func (ss *Servers) PushUnixIfNotExists(addr string) {
func (ss *Servers) PushUnixIfNotExists(addr string, kind Kind) {
if ss.IntoIter().FilterUnix().Len() != 0 {
return
}

s := &ServerUNIX{
ServerBase: ServerBase{
WithKind: WithKind{
Knd: KindUNIX,
},
},
var server ServerUNIX

Address: addr,
}
server.Knd = KindUNIX
server.Knd.Set(kind)

*ss = append(*ss, serverEnsureWrapped(s))
*ss = append(*ss, serverEnsureWrapped(&server))
}

func (ss *Servers) SetPortToFirstINET(port int) {
Expand All @@ -216,12 +205,12 @@ func (ss *Servers) Listen(fnArgs ...Arg) (Servers, []error) {
return ss.IntoIter().Listen(fnArgs...)
}

func (ss *Servers) ServeHTTP(handler http.Handler, fnArgs ...Arg) error {
func (ss *Servers) ServeHTTP(fnNewServer func(Server) http.Handler, fnArgs ...Arg) error {
return ss.
IntoIter().
FilterHTTP().
FilterListener().
ServeHTTP(handler, fnArgs...)
ServeHTTP(fnNewServer, fnArgs...)
}

func (ss *Servers) ServeGRPC(fnNewServer func(opts ...grpc.ServerOption) *grpc.Server, fnArgs ...Arg) error {
Expand Down
20 changes: 14 additions & 6 deletions servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestServers(t *testing.T) {
port: 8443
tls:
enable: false
cert-file: "/etc/acme/tls.cert"
key-file: "/etc/acme/tls.key"`},
certFile: "/etc/acme/tls.cert"
keyFile: "/etc/acme/tls.key"`},

{`- kind: unix
addr: /run/acme/acme.sock`},
Expand All @@ -45,8 +45,16 @@ func TestServers(t *testing.T) {
port: 8443
tls:
enable: false
cert-file: "/etc/acme/tls.cert"
key-file: "/etc/acme/tls.key"`},
certFile: "/etc/acme/tls.cert"
keyFile: "/etc/acme/tls.key"`},

{`- kind: [inet, grpc]
host: 0.0.0.0
port: 8000
tls:
enable: false
certFile: "/etc/acme/tls.cert"
keyFile: "/etc/acme/tls.key"`},

{`- kind: unix
- kind: inet`},
Expand Down Expand Up @@ -79,8 +87,8 @@ func TestServers(t *testing.T) {
}

ss.SetPortToFirstINET(9000)
ss.PushINETIfNotExists("10.1.1.2", 9001)
ss.PushUnixIfNotExists("/run/bar/buz.sock")
ss.PushINETIfNotExists("10.1.1.2", 9001, servers.KindGRPC)
ss.PushUnixIfNotExists("/run/bar/buz.sock", servers.KindHTTP)
}()
}
}

0 comments on commit 638ba34

Please sign in to comment.