Skip to content

Commit b7169e5

Browse files
authored
feat: Optmize Shutdown (#84)
* feat: Optmize Shutdown * fix test cases
1 parent 3083dc4 commit b7169e5

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.21
55
require (
66
github.com/gin-gonic/gin v1.10.0
77
github.com/gookit/validate v1.5.2
8-
github.com/goravel/framework v1.14.5-0.20240904064435-05476fa00c9c
8+
github.com/goravel/framework v1.14.5
99
github.com/rs/cors v1.11.0
1010
github.com/savioxavier/termlink v1.3.0
1111
github.com/spf13/cast v1.6.0

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,8 @@ github.com/gookit/validate v1.5.2 h1:i5I2OQ7WYHFRPRATGu9QarR9snnNHydvwSuHXaRWAV0
398398
github.com/gookit/validate v1.5.2/go.mod h1:yuPy2WwDlwGRa06fFJ5XIO8QEwhRnTC2LmxmBa5SE14=
399399
github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7aoWSjZ51C0m4=
400400
github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I=
401-
github.com/goravel/framework v1.14.3 h1:H2sKrmsxCvVGRBbeewjXXLWsCAvk1o5kVskNymDfpfw=
402-
github.com/goravel/framework v1.14.3/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
403-
github.com/goravel/framework v1.14.5-0.20240904064435-05476fa00c9c h1:RiMpkbasXxmNmE/pLCy6lqmu3uOadyhICiqMtgroA7I=
404-
github.com/goravel/framework v1.14.5-0.20240904064435-05476fa00c9c/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
401+
github.com/goravel/framework v1.14.5 h1:FItqxRGkBK0h/TIknF24TuMZCtBRaSr3DnQLEzhfvXc=
402+
github.com/goravel/framework v1.14.5/go.mod h1:rScDXGQZdoVfyxemNPmijlz/2a+lWNOa4jTuak5GGVg=
405403
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
406404
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
407405
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=

route.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ func (r *Route) Run(host ...string) error {
107107
MaxHeaderBytes: r.config.GetInt("http.drivers.gin.header_limit", 4096) << 10,
108108
}
109109

110-
return r.server.ListenAndServe()
110+
if err := r.server.ListenAndServe(); errors.Is(err, http.ErrServerClosed) {
111+
return nil
112+
} else {
113+
return err
114+
}
111115
}
112116

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

147-
return r.tlsServer.ListenAndServeTLS(certFile, keyFile)
151+
if err := r.tlsServer.ListenAndServeTLS(certFile, keyFile); errors.Is(err, http.ErrServerClosed) {
152+
return nil
153+
} else {
154+
return err
155+
}
148156
}
149157

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

154-
func (r *Route) Shutdown(ctx context.Context) error {
162+
func (r *Route) Shutdown(ctx ...context.Context) error {
163+
c := context.Background()
164+
if len(ctx) > 0 {
165+
c = ctx[0]
166+
}
167+
155168
if r.server != nil {
156-
return r.server.Shutdown(ctx)
169+
return r.server.Shutdown(c)
157170
}
158171
if r.tlsServer != nil {
159-
return r.tlsServer.Shutdown(ctx)
172+
return r.tlsServer.Shutdown(c)
160173
}
161174
return nil
162175
}

route_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gin
22

33
import (
4-
"context"
54
"crypto/tls"
65
"errors"
76
"fmt"
@@ -409,14 +408,14 @@ func TestShutdown(t *testing.T) {
409408
name: "no new requests will be accepted after shutdown",
410409
setup: func() error {
411410
go func() {
412-
assert.EqualError(t, route.Run(), http.ErrServerClosed.Error())
411+
assert.Nil(t, route.Run())
413412
}()
414413

415414
time.Sleep(1 * time.Second)
416415

417416
assertHttpNormal(t, addr, true)
418417

419-
assert.Nil(t, route.Shutdown(context.Background()))
418+
assert.Nil(t, route.Shutdown())
420419

421420
assertHttpNormal(t, addr, false)
422421
return nil
@@ -426,7 +425,7 @@ func TestShutdown(t *testing.T) {
426425
name: "Ensure that received requests are processed",
427426
setup: func() error {
428427
go func() {
429-
assert.EqualError(t, route.Run(), http.ErrServerClosed.Error())
428+
assert.Nil(t, route.Run())
430429
}()
431430

432431
time.Sleep(1 * time.Second)
@@ -441,7 +440,7 @@ func TestShutdown(t *testing.T) {
441440
}()
442441
}
443442
time.Sleep(100 * time.Millisecond)
444-
assert.Nil(t, route.Shutdown(context.Background()))
443+
assert.Nil(t, route.Shutdown())
445444
assertHttpNormal(t, addr, false)
446445
wg.Wait()
447446
assert.Equal(t, count.Load(), int64(3))

0 commit comments

Comments
 (0)