@@ -15,8 +15,9 @@ import (
15
15
"bytes"
16
16
"encoding/base64"
17
17
"encoding/binary"
18
- "github.com/tinylib/msgp/msgp"
19
18
"math/rand"
19
+
20
+ "github.com/tinylib/msgp/msgp"
20
21
)
21
22
22
23
const (
@@ -84,6 +85,7 @@ type msgToSend struct {
84
85
type Fluent struct {
85
86
Config
86
87
88
+ dialer dialer
87
89
stopRunning chan bool
88
90
pending chan * msgToSend
89
91
wg sync.WaitGroup
@@ -93,7 +95,20 @@ type Fluent struct {
93
95
}
94
96
95
97
// New creates a new Logger.
96
- func New (config Config ) (f * Fluent , err error ) {
98
+ func New (config Config ) (* Fluent , error ) {
99
+ if config .Timeout == 0 {
100
+ config .Timeout = defaultTimeout
101
+ }
102
+ return newWithDialer (config , & net.Dialer {
103
+ Timeout : config .Timeout ,
104
+ })
105
+ }
106
+
107
+ type dialer interface {
108
+ Dial (string , string ) (net.Conn , error )
109
+ }
110
+
111
+ func newWithDialer (config Config , d dialer ) (f * Fluent , err error ) {
97
112
if config .FluentNetwork == "" {
98
113
config .FluentNetwork = defaultNetwork
99
114
}
@@ -106,9 +121,6 @@ func New(config Config) (f *Fluent, err error) {
106
121
if config .FluentSocketPath == "" {
107
122
config .FluentSocketPath = defaultSocketPath
108
123
}
109
- if config .Timeout == 0 {
110
- config .Timeout = defaultTimeout
111
- }
112
124
if config .WriteTimeout == 0 {
113
125
config .WriteTimeout = defaultWriteTimeout
114
126
}
@@ -128,15 +140,20 @@ func New(config Config) (f *Fluent, err error) {
128
140
fmt .Fprintf (os .Stderr , "fluent#New: AsyncConnect is now deprecated, please use Async instead" )
129
141
config .Async = config .Async || config .AsyncConnect
130
142
}
143
+
131
144
if config .Async {
132
145
f = & Fluent {
133
146
Config : config ,
147
+ dialer : d ,
134
148
pending : make (chan * msgToSend , config .BufferLimit ),
135
149
}
136
150
f .wg .Add (1 )
137
151
go f .run ()
138
152
} else {
139
- f = & Fluent {Config : config }
153
+ f = & Fluent {
154
+ Config : config ,
155
+ dialer : d ,
156
+ }
140
157
err = f .connect ()
141
158
}
142
159
return
@@ -340,12 +357,15 @@ func (f *Fluent) close(c net.Conn) {
340
357
341
358
// connect establishes a new connection using the specified transport.
342
359
func (f * Fluent ) connect () (err error ) {
343
-
344
360
switch f .Config .FluentNetwork {
345
361
case "tcp" :
346
- f .conn , err = net .DialTimeout (f .Config .FluentNetwork , f .Config .FluentHost + ":" + strconv .Itoa (f .Config .FluentPort ), f .Config .Timeout )
362
+ f .conn , err = f .dialer .Dial (
363
+ f .Config .FluentNetwork ,
364
+ f .Config .FluentHost + ":" + strconv .Itoa (f .Config .FluentPort ))
347
365
case "unix" :
348
- f .conn , err = net .DialTimeout (f .Config .FluentNetwork , f .Config .FluentSocketPath , f .Config .Timeout )
366
+ f .conn , err = f .dialer .Dial (
367
+ f .Config .FluentNetwork ,
368
+ f .Config .FluentSocketPath )
349
369
default :
350
370
err = NewErrUnknownNetwork (f .Config .FluentNetwork )
351
371
}
0 commit comments