mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-10-28 15:02:28 +00:00
8d763f450d
This is a follow-up on the `go fix` run in commit
0210c5fd92.
That commit recorded the automated updates.
This one applies further simplifications that are mechanical and do not
involve logic changes, but are outside of what `go fix` did.
It also removes some now-obsolete comments and statements.
108 lines
1.8 KiB
Go
108 lines
1.8 KiB
Go
// SMTP connection generator, for testing purposes.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"net"
|
|
"net/http"
|
|
"net/smtp"
|
|
|
|
_ "net/http/pprof"
|
|
|
|
"blitiri.com.ar/go/chasquid/internal/nettrace"
|
|
"blitiri.com.ar/go/log"
|
|
)
|
|
|
|
var (
|
|
addr = flag.String("addr", "",
|
|
"server address")
|
|
httpAddr = flag.String("http_addr", "localhost:8011",
|
|
"monitoring HTTP server listening address")
|
|
wait = flag.Bool("wait", false,
|
|
"don't exit after --run_for has lapsed")
|
|
count = flag.Int("count", 1000,
|
|
"how many connections to open")
|
|
)
|
|
|
|
var (
|
|
host string
|
|
)
|
|
|
|
func main() {
|
|
var err error
|
|
|
|
flag.Parse()
|
|
log.Init()
|
|
|
|
host, _, err = net.SplitHostPort(*addr)
|
|
if err != nil {
|
|
log.Fatalf("failed to split --addr=%q: %v", *addr, err)
|
|
}
|
|
|
|
if *wait {
|
|
go http.ListenAndServe(*httpAddr, nil)
|
|
log.Infof("monitoring address: http://%v/debug/requests?fam=one&b=11",
|
|
*httpAddr)
|
|
}
|
|
|
|
log.Infof("creating %d simultaneous connections", *count)
|
|
conns := []*C{}
|
|
for i := 0; i < *count; i++ {
|
|
c, err := newC()
|
|
if err != nil {
|
|
log.Fatalf("failed to connect #%d: %v", i, err)
|
|
}
|
|
|
|
conns = append(conns, c)
|
|
|
|
if i%200 == 0 {
|
|
log.Infof(" ... %d connections", i)
|
|
|
|
}
|
|
}
|
|
log.Infof("done, created %d simultaneous connections", *count)
|
|
|
|
if *wait {
|
|
select {}
|
|
}
|
|
|
|
for _, c := range conns {
|
|
c.close()
|
|
}
|
|
}
|
|
|
|
// C represents a single connection.
|
|
type C struct {
|
|
tr nettrace.Trace
|
|
n net.Conn
|
|
s *smtp.Client
|
|
}
|
|
|
|
func newC() (*C, error) {
|
|
tr := nettrace.New("conn", *addr)
|
|
|
|
conn, err := net.Dial("tcp", *addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
client, err := smtp.NewClient(conn, host)
|
|
if err != nil {
|
|
conn.Close()
|
|
return nil, err
|
|
}
|
|
|
|
err = client.Hello(host)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &C{tr: tr, n: conn, s: client}, nil
|
|
}
|
|
|
|
func (c *C) close() {
|
|
c.tr.Finish()
|
|
c.s.Close()
|
|
c.n.Close()
|
|
}
|