Skip to content

Commit

Permalink
Merge pull request #42 from ugexe/patch-1
Browse files Browse the repository at this point in the history
Make rawQuery timeout handling more thread-safe
  • Loading branch information
likexian authored Sep 2, 2024
2 parents 259e6b0 + 027c73e commit c01ca63
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions whois.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ var DefaultClient = NewClient()
type Client struct {
dialer proxy.Dialer
timeout time.Duration
elapsed time.Duration
disableStats bool
disableReferral bool
}
Expand Down Expand Up @@ -183,7 +182,6 @@ func (c *Client) Whois(domain string, servers ...string) (result string, err err

// rawQuery do raw query to the server
func (c *Client) rawQuery(domain, server, port string) (string, error) {
c.elapsed = 0
start := time.Now()

if server == "whois.arin.net" {
Expand All @@ -210,24 +208,22 @@ func (c *Client) rawQuery(domain, server, port string) (string, error) {
}

defer conn.Close()
c.elapsed = time.Since(start)
elapsed := time.Since(start)

_ = conn.SetWriteDeadline(time.Now().Add(c.timeout - c.elapsed))
_ = conn.SetWriteDeadline(time.Now().Add(c.timeout - elapsed))
_, err = conn.Write([]byte(domain + "\r\n"))
if err != nil {
return "", fmt.Errorf("whois: send to whois server failed: %w", err)
}

c.elapsed = time.Since(start)
elapsed = time.Since(start)

_ = conn.SetReadDeadline(time.Now().Add(c.timeout - c.elapsed))
_ = conn.SetReadDeadline(time.Now().Add(c.timeout - elapsed))
buffer, err := io.ReadAll(conn)
if err != nil {
return "", fmt.Errorf("whois: read from whois server failed: %w", err)
}

c.elapsed = time.Since(start)

return string(buffer), nil
}

Expand Down

0 comments on commit c01ca63

Please sign in to comment.