Skip to content

Commit fa32991

Browse files
authored
Move dialer helpers from metricbeat/helper/dialer to transport package (#44)
1 parent 41693a4 commit fa32991

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

transport/dialer/dialer.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package dialer
19+
20+
import (
21+
"fmt"
22+
"time"
23+
24+
"github.com/elastic/elastic-agent-libs/transport"
25+
)
26+
27+
// Builder is a dialer builder.
28+
type Builder interface {
29+
fmt.Stringer
30+
Make(time.Duration) (transport.Dialer, error)
31+
}
32+
33+
// DefaultDialerBuilder create a builder to dialer over TCP and UDP.
34+
type DefaultDialerBuilder struct{}
35+
36+
// Make creates a dialer.
37+
func (t *DefaultDialerBuilder) Make(timeout time.Duration) (transport.Dialer, error) {
38+
return transport.NetDialer(timeout), nil
39+
}
40+
41+
func (t *DefaultDialerBuilder) String() string {
42+
return "TCP/UDP"
43+
}
44+
45+
// NewDefaultDialerBuilder creates a DefaultDialerBuilder.
46+
func NewDefaultDialerBuilder() *DefaultDialerBuilder {
47+
return &DefaultDialerBuilder{}
48+
}
49+
50+
// NewNpipeDialerBuilder creates a NpipeDialerBuilder.
51+
func NewNpipeDialerBuilder(path string) *NpipeDialerBuilder {
52+
return &NpipeDialerBuilder{Path: path}
53+
}
54+
55+
// NewUnixDialerBuilder returns a new TransportUnix instance that will allow the HTTP client to communicate
56+
// over a unix domain socket it require a valid path to the socket on the filesystem.
57+
func NewUnixDialerBuilder(path string) *UnixDialerBuilder {
58+
return &UnixDialerBuilder{Path: path}
59+
}

transport/dialer/dialer_posix.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//go:build !windows
19+
// +build !windows
20+
21+
package dialer
22+
23+
import (
24+
"errors"
25+
"strings"
26+
"time"
27+
28+
"github.com/elastic/elastic-agent-libs/transport"
29+
)
30+
31+
// UnixDialerBuilder creates a builder to dial over unix domain socket.
32+
type UnixDialerBuilder struct {
33+
Path string
34+
}
35+
36+
// Make creates a dialer.
37+
func (t *UnixDialerBuilder) Make(timeout time.Duration) (transport.Dialer, error) {
38+
return transport.UnixDialer(timeout, strings.TrimSuffix(t.Path, "/")), nil
39+
}
40+
41+
func (t *UnixDialerBuilder) String() string {
42+
return "Unix: " + t.Path
43+
}
44+
45+
// NpipeDialerBuilder creates a builder to dial over a named pipe.
46+
type NpipeDialerBuilder struct {
47+
Path string
48+
}
49+
50+
// Make creates a dialer.
51+
func (t *NpipeDialerBuilder) Make(_ time.Duration) (transport.Dialer, error) {
52+
return nil, errors.New("cannot the URI, named pipes are only supported on Windows")
53+
}
54+
55+
func (t *NpipeDialerBuilder) String() string {
56+
return "Npipe: " + t.Path
57+
}

transport/dialer/dialer_windows.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//go:build windows
19+
// +build windows
20+
21+
package dialer
22+
23+
import (
24+
"errors"
25+
"net"
26+
"strings"
27+
"time"
28+
29+
winio "github.com/Microsoft/go-winio"
30+
31+
"github.com/elastic/elastic-agent-libs/api/npipe"
32+
"github.com/elastic/elastic-agent-libs/transport"
33+
)
34+
35+
// UnixDialerBuilder creates a builder to dial over a unix domain socket.
36+
type UnixDialerBuilder struct {
37+
Path string
38+
}
39+
40+
// Make creates a dialer.
41+
func (t *UnixDialerBuilder) Make(_ time.Duration) (transport.Dialer, error) {
42+
return nil, errors.New(
43+
"cannot use the URI, unix sockets are not supported on Windows, use npipe instead",
44+
)
45+
}
46+
47+
func (t *UnixDialerBuilder) String() string {
48+
return "Unix: " + t.Path
49+
}
50+
51+
// NpipeDialerBuilder creates a builder to dial over a named pipe.
52+
type NpipeDialerBuilder struct {
53+
Path string
54+
}
55+
56+
func (t *NpipeDialerBuilder) String() string {
57+
return "Npipe: " + t.Path
58+
}
59+
60+
// Make creates a dialer.
61+
func (t *NpipeDialerBuilder) Make(timeout time.Duration) (transport.Dialer, error) {
62+
to := timeout
63+
return transport.DialerFunc(
64+
func(_, _ string) (net.Conn, error) {
65+
return winio.DialPipe(
66+
strings.TrimSuffix(npipe.TransformString(t.Path), "/"),
67+
&to,
68+
)
69+
},
70+
), nil
71+
}

0 commit comments

Comments
 (0)