Skip to content

Commit

Permalink
xrootd/client: automatically infer xrootd port number
Browse files Browse the repository at this point in the history
Fixes go-hep#236.
  • Loading branch information
sbinet committed Jun 27, 2018
1 parent 23a716a commit 28fcfce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
29 changes: 29 additions & 0 deletions xrootd/client/port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package client // import "go-hep.org/x/hep/xrootd/client"

import (
"net"
"strconv"
)

func parseAddr(addr string) string {
_, _, err := net.SplitHostPort(addr)
if err == nil {
return addr
}
switch err := err.(type) {
case *net.AddrError:
switch err.Err {
case "missing port in address":
port, e := net.LookupPort("tcp", "rootd")
if e != nil {
return addr + ":1094"
}
return addr + ":" + strconv.Itoa(port)
}
}
return addr
}
5 changes: 3 additions & 2 deletions xrootd/client/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func newSession(ctx context.Context, address string, username string, client *Cl
ctx, cancel := context.WithCancel(ctx)

var d net.Dialer
conn, err := d.DialContext(ctx, "tcp", address)
addr := parseAddr(address)
conn, err := d.DialContext(ctx, "tcp", addr)
if err != nil {
cancel()
return nil, err
Expand All @@ -63,7 +64,7 @@ func newSession(ctx context.Context, address string, username string, client *Cl
mux: mux.New(),
requests: make(map[xrdproto.StreamID][]byte),
client: client,
sessionID: address,
sessionID: addr,
}

go sess.consume(ctx)
Expand Down

0 comments on commit 28fcfce

Please sign in to comment.