Move timeout to incoming queries

This commit is contained in:
Bryan Boreham committed 2014-10-07 12:47:18 +01:00
1 parent 2cab8415f2
commit a545d2738c
1 file changed
+24 -12
+24 -12
View File
@@ -30,7 +30,8 @@ type ResponseA struct {
} }
type responseInfo struct { type responseInfo struct {
ch chan<- *ResponseA timeout time.Time // if no answer by this time, give up
ch chan<- *ResponseA
} }
// Represents one query that we have sent for one name. // Represents one query that we have sent for one name.
@@ -40,7 +41,6 @@ type inflightQuery struct {
name string name string
Id uint16 // the DNS message ID Id uint16 // the DNS message ID
responseInfos []*responseInfo responseInfos []*responseInfo
timeout time.Time // if no answer by this time, give up
} }
type MDNSClient struct { type MDNSClient struct {
@@ -142,15 +142,25 @@ func (c *MDNSClient) queryLoop(queryChan <-chan *MDNSInteraction) {
now := time.Now() now := time.Now()
after := MaxDuration after := MaxDuration
for name, query := range c.inflight { for name, query := range c.inflight {
switch duration := query.timeout.Sub(now); { // Count down from end of slice to beginning
case duration <= 0: // timed out length := len(query.responseInfos)
log.Println("Query timed out:", name) for i := length - 1; i >= 0; i-- {
for _, item := range query.responseInfos { item := query.responseInfos[i]
switch duration := item.timeout.Sub(now); {
case duration <= 0: // timed out
close(item.ch) close(item.ch)
// Swap item from the end of the slice
length--
if i < length {
query.responseInfos[i] = query.responseInfos[length]
}
case duration < after:
after = duration
} }
}
query.responseInfos = query.responseInfos[:length]
if length == 0 {
delete(c.inflight, name) delete(c.inflight, name)
case duration < after:
after = duration
} }
} }
log.Println("Next timeout:", after) log.Println("Next timeout:", after)
@@ -199,14 +209,16 @@ func (c *MDNSClient) handleSendQuery(q mDNSQueryInfo) (err error) {
return err return err
} }
query = &inflightQuery{ query = &inflightQuery{
name: q.name, name: q.name,
Id: m.Id, Id: m.Id,
timeout: time.Now().Add(mDNSTimeout),
} }
c.inflight[q.name] = query c.inflight[q.name] = query
_, err = c.conn.WriteTo(buf, c.addr) _, err = c.conn.WriteTo(buf, c.addr)
} }
info := &responseInfo{ch: q.responseCh} info := &responseInfo{
ch: q.responseCh,
timeout: time.Now().Add(mDNSTimeout),
}
query.responseInfos = append(query.responseInfos, info) query.responseInfos = append(query.responseInfos, info)
return err return err