Skip to content

Commit

Permalink
feat: Optmize Shutdown (#84)
Browse files Browse the repository at this point in the history
* feat: Optmize Shutdown

* fix test cases
  • Loading branch information
hwbrzzl authored Sep 7, 2024
1 parent 3083dc4 commit b7169e5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
github.com/gin-gonic/gin v1.10.0
github.com/gookit/validate v1.5.2
github.com/goravel/framework v1.14.5-0.20240904064435-05476fa00c9c
github.com/goravel/framework v1.14.5
github.com/rs/cors v1.11.0
github.com/savioxavier/termlink v1.3.0
github.com/spf13/cast v1.6.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,8 @@ github.com/gookit/validate v1.5.2 h1:i5I2OQ7WYHFRPRATGu9QarR9snnNHydvwSuHXaRWAV0
github.com/gookit/validate v1.5.2/go.mod h1:yuPy2WwDlwGRa06fFJ5XIO8QEwhRnTC2LmxmBa5SE14=
github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7aoWSjZ51C0m4=
github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I=
github.com/goravel/framework v1.14.3 h1:H2sKrmsxCvVGRBbeewjXXLWsCAvk1o5kVskNymDfpfw=
github.com/goravel/framework v1.14.3/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
github.com/goravel/framework v1.14.5-0.20240904064435-05476fa00c9c h1:RiMpkbasXxmNmE/pLCy6lqmu3uOadyhICiqMtgroA7I=
github.com/goravel/framework v1.14.5-0.20240904064435-05476fa00c9c/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
github.com/goravel/framework v1.14.5 h1:FItqxRGkBK0h/TIknF24TuMZCtBRaSr3DnQLEzhfvXc=
github.com/goravel/framework v1.14.5/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
Expand Down
23 changes: 18 additions & 5 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func (r *Route) Run(host ...string) error {
MaxHeaderBytes: r.config.GetInt("http.drivers.gin.header_limit", 4096) << 10,
}

return r.server.ListenAndServe()
if err := r.server.ListenAndServe(); errors.Is(err, http.ErrServerClosed) {
return nil
} else {
return err
}
}

func (r *Route) RunTLS(host ...string) error {
Expand Down Expand Up @@ -144,19 +148,28 @@ func (r *Route) RunTLSWithCert(host, certFile, keyFile string) error {
MaxHeaderBytes: r.config.GetInt("http.drivers.gin.header_limit", 4096) << 10,
}

return r.tlsServer.ListenAndServeTLS(certFile, keyFile)
if err := r.tlsServer.ListenAndServeTLS(certFile, keyFile); errors.Is(err, http.ErrServerClosed) {
return nil
} else {
return err
}
}

func (r *Route) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
r.instance.ServeHTTP(writer, request)
}

func (r *Route) Shutdown(ctx context.Context) error {
func (r *Route) Shutdown(ctx ...context.Context) error {
c := context.Background()
if len(ctx) > 0 {
c = ctx[0]
}

if r.server != nil {
return r.server.Shutdown(ctx)
return r.server.Shutdown(c)
}
if r.tlsServer != nil {
return r.tlsServer.Shutdown(ctx)
return r.tlsServer.Shutdown(c)
}
return nil
}
Expand Down
9 changes: 4 additions & 5 deletions route_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gin

import (
"context"
"crypto/tls"
"errors"
"fmt"
Expand Down Expand Up @@ -409,14 +408,14 @@ func TestShutdown(t *testing.T) {
name: "no new requests will be accepted after shutdown",
setup: func() error {
go func() {
assert.EqualError(t, route.Run(), http.ErrServerClosed.Error())
assert.Nil(t, route.Run())
}()

time.Sleep(1 * time.Second)

assertHttpNormal(t, addr, true)

assert.Nil(t, route.Shutdown(context.Background()))
assert.Nil(t, route.Shutdown())

assertHttpNormal(t, addr, false)
return nil
Expand All @@ -426,7 +425,7 @@ func TestShutdown(t *testing.T) {
name: "Ensure that received requests are processed",
setup: func() error {
go func() {
assert.EqualError(t, route.Run(), http.ErrServerClosed.Error())
assert.Nil(t, route.Run())
}()

time.Sleep(1 * time.Second)
Expand All @@ -441,7 +440,7 @@ func TestShutdown(t *testing.T) {
}()
}
time.Sleep(100 * time.Millisecond)
assert.Nil(t, route.Shutdown(context.Background()))
assert.Nil(t, route.Shutdown())
assertHttpNormal(t, addr, false)
wg.Wait()
assert.Equal(t, count.Load(), int64(3))
Expand Down

0 comments on commit b7169e5

Please sign in to comment.