Skip to content

Commit

Permalink
add: 完善server
Browse files Browse the repository at this point in the history
  • Loading branch information
moocss committed Jul 18, 2024
1 parent 2bcaf8e commit ac93779
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 19 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/dgraph-io/ristretto v0.1.1
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-gonic/gin v1.10.0
github.com/go-sql-driver/mysql v1.8.1
github.com/golang/mock v1.6.0
github.com/google/uuid v1.6.0
github.com/iancoleman/strcase v0.3.0
Expand All @@ -20,7 +21,6 @@ require (
github.com/redis/go-redis/v9 v9.5.4
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
github.com/xo/dburl v0.23.2
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.25.0
golang.org/x/sync v0.7.0
Expand Down Expand Up @@ -54,7 +54,6 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
github.com/xo/dburl v0.23.2 h1:Fl88cvayrgE56JA/sqhNMLljCW/b7RmG1mMkKMZUFgA=
github.com/xo/dburl v0.23.2/go.mod h1:uazlaAQxj4gkshhfuuYyvwCBouOmNnG2aDxTCFZpmL4=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
11 changes: 7 additions & 4 deletions server/grpc/grpc.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package grpc

import (
"context"
"github.com/apus-run/van/server"
"google.golang.org/grpc"
"net"

"github.com/apus-run/van/server"
)

var _ server.Server = (*Server)(nil)
Expand All @@ -23,15 +23,18 @@ func NewServer(grpcServer *grpc.Server, opts ...Option) *Server {
return srv
}

func (s *Server) Start() error {
func (s *Server) Start(ctx context.Context) error {
lis, err := net.Listen("tcp", s.opts.Addr)
if err != nil {
return err
}
return s.srv.Serve(lis)
}

func (s *Server) Stop() error {
func (s *Server) Stop(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, s.opts.ShutdownTimeout)
defer cancel()

s.srv.GracefulStop()
return nil
}
13 changes: 12 additions & 1 deletion server/grpc/types.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package grpc

import "time"

// Option is server option.
type Option func(*Options)

// Options is server options.
type Options struct {
Addr string

ShutdownTimeout time.Duration
}

// DefaultOptions is server default options.
func DefaultOptions() *Options {
return &Options{
Addr: ":8090",
Addr: ":8090",
ShutdownTimeout: 10 * time.Second,
}
}

Expand All @@ -29,3 +34,9 @@ func WithAddr(addr string) Option {
options.Addr = addr
}
}

func WithShutdownTimeout(t time.Duration) Option {
return func(options *Options) {
options.ShutdownTimeout = t
}
}
12 changes: 6 additions & 6 deletions server/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ func NewServer(handler http.Handler, opts ...Option) *Server {
return srv
}

func (s *Server) Start() error {
func (s *Server) Start(ctx context.Context) error {
err := s.srv.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
return err
if err != nil && errors.Is(err, http.ErrServerClosed) {
return nil
}
return nil
return err
}

func (s *Server) Stop() error {
func (s *Server) Stop(ctx context.Context) error {
// 创建 ctx 用于通知服务器 goroutine, 它有 10 秒时间完成当前正在处理的请求
ctx, cancel := context.WithTimeout(context.Background(), s.opts.ShutdownTimeout)
ctx, cancel := context.WithTimeout(ctx, s.opts.ShutdownTimeout)
defer cancel()
return s.srv.Shutdown(ctx)
}
5 changes: 3 additions & 2 deletions server/http/http_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"context"
"net/http"
"testing"
"time"
Expand All @@ -25,10 +26,10 @@ func TestNewServer(t *testing.T) {
assert.Equal(t, resp.StatusCode, http.StatusOK)

time.Sleep(1 * time.Second)
err = server.Stop()
err = server.Stop(context.Background())
assert.NoError(t, err)
t.Log("shutdown completed")
}()

_ = server.Start()
_ = server.Start(context.Background())
}
14 changes: 12 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package server

import (
"context"
"net/url"
)

// Server is transport server.
type Server interface {
Start() error
Stop() error
Start(context.Context) error
Stop(context.Context) error
}

// Endpointer is registry endpoint.
type Endpointer interface {
Endpoint() (*url.URL, error)
}

0 comments on commit ac93779

Please sign in to comment.