mirror of
https://github.com/moby/moby.git
synced 2026-08-03 06:30:59 +00:00
daemon/libnetwork: Network.ResolveService: slight optimization
- use strings.Cut to avoid strings.Split + strings.Join - move vars closer to where used Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -2005,22 +2005,20 @@ func (n *Network) ResolveIP(_ context.Context, ip string) string {
|
||||
func (n *Network) ResolveService(ctx context.Context, name string) ([]*net.SRV, []net.IP) {
|
||||
c := n.getController()
|
||||
|
||||
srv := []*net.SRV{}
|
||||
ip := []net.IP{}
|
||||
|
||||
log.G(ctx).Debugf("Service name To resolve: %v", name)
|
||||
|
||||
// There are DNS implementations that allow SRV queries for names not in
|
||||
// the format defined by RFC 2782. Hence specific validations checks are
|
||||
// not done
|
||||
parts := strings.Split(name, ".")
|
||||
if len(parts) < 3 {
|
||||
// the format defined by RFC 2782. Hence specific validation checks are
|
||||
// not done.
|
||||
portName, protoService, ok := strings.Cut(name, ".")
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
portName := parts[0]
|
||||
proto := parts[1]
|
||||
svcName := strings.Join(parts[2:], ".")
|
||||
proto, svcName, ok := strings.Cut(protoService, ".")
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
networkID := n.ID()
|
||||
c.mu.Lock()
|
||||
@@ -2036,6 +2034,8 @@ func (n *Network) ResolveService(ctx context.Context, name string) ([]*net.SRV,
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
srv := []*net.SRV{}
|
||||
ip := []net.IP{}
|
||||
for _, svc := range svcs {
|
||||
if svc.portName != portName {
|
||||
continue
|
||||
|
||||
@@ -397,11 +397,6 @@ func (r *Resolver) handleSRVQuery(ctx context.Context, query *dns.Msg) (*dns.Msg
|
||||
}
|
||||
|
||||
func (r *Resolver) serveDNS(w dns.ResponseWriter, query *dns.Msg) {
|
||||
var (
|
||||
resp *dns.Msg
|
||||
err error
|
||||
)
|
||||
|
||||
if query == nil || len(query.Question) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -415,6 +410,11 @@ func (r *Resolver) serveDNS(w dns.ResponseWriter, query *dns.Msg) {
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
var (
|
||||
resp *dns.Msg
|
||||
err error
|
||||
)
|
||||
|
||||
switch queryType {
|
||||
case dns.TypeA:
|
||||
resp, err = r.handleIPQuery(ctx, query, types.IPv4)
|
||||
@@ -431,7 +431,7 @@ func (r *Resolver) serveDNS(w dns.ResponseWriter, query *dns.Msg) {
|
||||
}
|
||||
|
||||
reply := func(msg *dns.Msg) {
|
||||
if err = w.WriteMsg(msg); err != nil {
|
||||
if err := w.WriteMsg(msg); err != nil {
|
||||
r.log(ctx).WithError(err).Error("[resolver] failed to write response")
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, "WriteMsg failed")
|
||||
|
||||
Reference in New Issue
Block a user