From aa7959f0442a22c4b35c4aee04ee3c971c5ce9a1 Mon Sep 17 00:00:00 2001 From: Jeff Foley Date: Sat, 27 Jan 2024 00:41:34 -0500 Subject: [PATCH] small updates --- ecs.go | 23 ++++++++-------- ecs_test.go | 2 +- rate.go | 2 +- resolvers.go | 67 ++++++++++++++++++++++++----------------------- resolvers_test.go | 2 +- thresholds.go | 2 +- xchg.go | 2 +- xchg_test.go | 2 +- 8 files changed, 52 insertions(+), 50 deletions(-) diff --git a/ecs.go b/ecs.go index e9754c5..fa82964 100644 --- a/ecs.go +++ b/ecs.go @@ -1,10 +1,12 @@ -// Copyright © by Jeff Foley 2021-2023. All rights reserved. +// Copyright © by Jeff Foley 2021-2024. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0 package resolve import ( + "time" + "github.com/miekg/dns" ) @@ -26,14 +28,18 @@ func (r *Resolvers) ClientSubnetCheck() { }) } + var count int for _, res := range all { send(res) + count++ + if count == 100 { + count = 0 + time.Sleep(100 * time.Millisecond) + } } - retries := make(map[string]struct{}) for i := 0; i < alen; i++ { resp := <-ch - // pull the resolver associated with this message key := xchgKey(resp.Id, resp.Question[0].Name) res, found := msgsToRes[key] @@ -41,16 +47,11 @@ func (r *Resolvers) ClientSubnetCheck() { continue } delete(msgsToRes, key) - // give resolvers one additional chance to respond - if _, already := retries[res.address.IP.String()]; !already && resp.Rcode == RcodeNoResponse { - i-- - retries[res.address.IP.String()] = struct{}{} - send(res) - continue - } // check if the resolver responded, but did not return a successful response if resp.Rcode != dns.RcodeSuccess || (!resp.Authoritative && !resp.RecursionAvailable) { - res.stop() + if res != nil { + res.stop() + } continue } diff --git a/ecs_test.go b/ecs_test.go index 12cd83f..6123b80 100644 --- a/ecs_test.go +++ b/ecs_test.go @@ -1,4 +1,4 @@ -// Copyright © by Jeff Foley 2022-2023. All rights reserved. +// Copyright © by Jeff Foley 2022-2024. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0 diff --git a/rate.go b/rate.go index 7df6c14..629c2f7 100644 --- a/rate.go +++ b/rate.go @@ -1,4 +1,4 @@ -// Copyright © by Jeff Foley 2022-2023. All rights reserved. +// Copyright © by Jeff Foley 2022-2024. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0 diff --git a/resolvers.go b/resolvers.go index 9e50876..739ccd3 100644 --- a/resolvers.go +++ b/resolvers.go @@ -1,4 +1,4 @@ -// Copyright © by Jeff Foley 2017-2023. All rights reserved. +// Copyright © by Jeff Foley 2017-2024. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0 @@ -321,43 +321,42 @@ func (r *Resolvers) processResponses() { case <-r.resps.Signal(): } - var response *resp - if element, ok := r.resps.Next(); ok { - if r, valid := element.(*resp); valid { - response = r + r.resps.Process(func(element interface{}) { + if response, ok := element.(*resp); ok && r != nil { + r.processSingleResp(response) } - } - if response == nil { - continue - } + }) + } +} - var res *resolver - addr := response.Addr.IP.String() - if res = r.pool.LookupResolver(addr); res == nil { - detector := r.getDetectionResolver() +func (r *Resolvers) processSingleResp(response *resp) { + var res *resolver + addr := response.Addr.IP.String() - if detector != nil && addr == detector.address.IP.String() { - res = detector - } - } - if res == nil { - continue + if res = r.pool.LookupResolver(addr); res == nil { + detector := r.getDetectionResolver() + + if detector != nil && addr == detector.address.IP.String() { + res = detector } + } + if res == nil { + return + } - msg := response.Msg - name := msg.Question[0].Name - if req := res.xchgs.remove(msg.Id, name); req != nil { - req.Resp = msg - if req.Resp.Truncated { - go req.Res.tcpExchange(req) - } else { - req.Result <- req.Resp - req.Res.collectStats(req.Resp) - if r.servRates != nil { - r.servRates.Success(name) - } - req.release() + msg := response.Msg + name := msg.Question[0].Name + if req := res.xchgs.remove(msg.Id, name); req != nil { + req.Resp = msg + if req.Resp.Truncated { + go req.Res.tcpExchange(req) + } else { + req.Result <- req.Resp + req.Res.collectStats(req.Resp) + if r.servRates != nil { + r.servRates.Success(name) } + req.release() } } } @@ -394,7 +393,9 @@ func (r *Resolvers) timeouts() { r.Lock() d := r.timeout / 2 r.Unlock() - time.Sleep(d) + if d > 0 { + time.Sleep(d) + } } } diff --git a/resolvers_test.go b/resolvers_test.go index 284349a..90ec5b1 100644 --- a/resolvers_test.go +++ b/resolvers_test.go @@ -1,4 +1,4 @@ -// Copyright © by Jeff Foley 2017-2023. All rights reserved. +// Copyright © by Jeff Foley 2017-2024. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0 diff --git a/thresholds.go b/thresholds.go index 2fd9d9a..d84fe90 100644 --- a/thresholds.go +++ b/thresholds.go @@ -1,4 +1,4 @@ -// Copyright © by Jeff Foley 2017-2023. All rights reserved. +// Copyright © by Jeff Foley 2017-2024. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0 diff --git a/xchg.go b/xchg.go index 57fbbf4..0e6f494 100644 --- a/xchg.go +++ b/xchg.go @@ -1,4 +1,4 @@ -// Copyright © by Jeff Foley 2021-2023. All rights reserved. +// Copyright © by Jeff Foley 2021-2024. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0 diff --git a/xchg_test.go b/xchg_test.go index 667503f..2d36892 100644 --- a/xchg_test.go +++ b/xchg_test.go @@ -1,4 +1,4 @@ -// Copyright © by Jeff Foley 2021-2023. All rights reserved. +// Copyright © by Jeff Foley 2021-2024. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // SPDX-License-Identifier: Apache-2.0