-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Use workers instead spawning goroutines for each incoming DNS request #565
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package dns | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func BenchmarkServer(b *testing.B) { | ||
HandleFunc(".", erraticHandler) | ||
defer HandleRemove(".") | ||
benchmark(0, b) | ||
for _, workers := range []int{50, 100, 200, 1000} { | ||
benchmark(workers, b) | ||
} | ||
} | ||
|
||
func benchmark(workers int, b *testing.B) { | ||
s, addr, err := runLocalUDPServer(workers) | ||
if err != nil { | ||
b.Fatalf("unable to run test server: %v", err) | ||
} | ||
defer s.Shutdown() | ||
|
||
m := new(Msg) | ||
m.SetQuestion("domain.local.", TypeA) | ||
|
||
conn, err := net.Dial("udp", addr) | ||
if err != nil { | ||
b.Fatalf("client Dial() failed: %v", err) | ||
} | ||
defer conn.Close() | ||
|
||
test := fmt.Sprintf("%d_workers", workers) | ||
b.Run(test, func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_, err = exchange(conn, m) | ||
if err != nil { | ||
b.Fatalf("exchange() failed: %v", err) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
var ( | ||
rrA, _ = NewRR(". IN 0 A 192.0.2.53") | ||
rrAAAA, _ = NewRR(". IN 0 AAAA 2001:DB8::53") | ||
) | ||
|
||
func erraticHandler(w ResponseWriter, r *Msg) { | ||
r.Response = true | ||
|
||
switch r.Question[0].Qtype { | ||
case TypeA: | ||
rr := *(rrA.(*A)) | ||
rr.Header().Name = r.Question[0].Name | ||
r.Answer = []RR{&rr} | ||
r.Rcode = RcodeSuccess | ||
case TypeAAAA: | ||
rr := *(rrAAAA.(*AAAA)) | ||
rr.Header().Name = r.Question[0].Name | ||
r.Answer = []RR{&rr} | ||
r.Rcode = RcodeSuccess | ||
default: | ||
r.Rcode = RcodeServerFailure | ||
} | ||
|
||
w.WriteMsg(r) | ||
} | ||
|
||
func runLocalUDPServer(workers int) (*Server, string, error) { | ||
pc, err := net.ListenPacket("udp", "127.0.0.1:0") | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
server := &Server{ | ||
PacketConn: pc, | ||
NotifyStartedFunc: wg.Done, | ||
Workers: workers, | ||
} | ||
|
||
go func() { | ||
server.ActivateAndServe() | ||
pc.Close() | ||
}() | ||
|
||
wg.Wait() | ||
return server, pc.LocalAddr().String(), nil | ||
} | ||
|
||
func exchange(conn net.Conn, m *Msg) (r *Msg, err error) { | ||
c := Conn{Conn: conn} | ||
if err = c.WriteMsg(m); err != nil { | ||
return nil, err | ||
} | ||
r, err = c.ReadMsg() | ||
if err == nil && r.Id != m.Id { | ||
err = ErrId | ||
} | ||
return r, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this on needed?
scrolls down
Ah you're pulling it out from the serveX functions; sensible change; can you make that also a new PR?
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.
serveUDP() and serveTCP() have:
Why do we need to set handler each time on serveUDP() or serveTCP() call and then pass handler into serve() if we can set srv.Handler only once and serve() can just use srv.Handler?