New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add ShutdownWithContext #1383
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"runtime" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -3597,6 +3598,57 @@ func TestShutdownCloseIdleConns(t *testing.T) { | |
} | ||
} | ||
|
||
func TestShutdownWithContext(t *testing.T) { | ||
t.Parallel() | ||
|
||
ln := fasthttputil.NewInmemoryListener() | ||
s := &Server{ | ||
Handler: func(ctx *RequestCtx) { | ||
time.Sleep(5 * time.Second) | ||
ctx.Success("aaa/bbb", []byte("real response")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @erikdubbelboer PTAL |
||
}, | ||
} | ||
go func() { | ||
if err := s.Serve(ln); err != nil { | ||
t.Errorf("unexepcted error: %v", err) | ||
} | ||
}() | ||
time.Sleep(1 * time.Second) | ||
go func() { | ||
conn, err := ln.Dial() | ||
if err != nil { | ||
t.Errorf("unexepcted error: %v", err) | ||
} | ||
|
||
if _, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")); err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
br := bufio.NewReader(conn) | ||
verifyResponse(t, br, StatusOK, "aaa/bbb", "real response") | ||
}() | ||
|
||
time.Sleep(1 * time.Second) | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond) | ||
defer cancel() | ||
shutdownErr := make(chan error) | ||
go func() { | ||
shutdownErr <- s.ShutdownWithContext(ctx) | ||
}() | ||
|
||
timer := time.NewTimer(time.Second) | ||
select { | ||
case <-timer.C: | ||
t.Fatal("idle connections not closed on shutdown") | ||
case err := <-shutdownErr: | ||
if err == nil || err != context.DeadlineExceeded { | ||
t.Fatalf("unexpected err %v. Expecting %v", err, context.DeadlineExceeded) | ||
} | ||
} | ||
if atomic.LoadInt32(&s.open) != 1 { | ||
t.Fatalf("unexpected open connection num: %#v. Expecting %#v", atomic.LoadInt32(&s.open), 1) | ||
} | ||
} | ||
|
||
func TestMultipleServe(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering, should this then force close all connections? What does
net/http
do in this case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://cs.opensource.google/go/go/+/refs/tags/go1.19.2:src/net/http/server.go;l=2731
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
Shutdown
does not force all connections to close, but rather uses context to control the timeout after alllistener
have been closed.