-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialer.go
44 lines (35 loc) · 1.26 KB
/
dialer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"net"
"time"
"word-of-wisdom-go/internal/services"
"go.uber.org/dig"
)
type SessionDialer interface {
// DialSession establishes new connection and returns session and close function
DialSession(network, address string) (*services.SessionIO, func() error, error)
}
type sessionDialerFunc func(network, address string) (*services.SessionIO, func() error, error)
func (f sessionDialerFunc) DialSession(network, address string) (*services.SessionIO, func() error, error) {
return f(network, address)
}
var _ SessionDialer = sessionDialerFunc(nil)
type SessionDialerDeps struct {
dig.In
// config
IOTimeout time.Duration `name:"config.client.ioTimeout"`
}
func newSessionDialer(deps SessionDialerDeps) SessionDialer {
return sessionDialerFunc(func(network, address string) (*services.SessionIO, func() error, error) {
conn, err := net.Dial(network, address)
if err != nil {
return nil, nil, fmt.Errorf("error connecting to server: %w", err)
}
if err = conn.SetDeadline(time.Now().Add(deps.IOTimeout)); err != nil { // coverage-ignore // hard to simulate this
return nil, nil, fmt.Errorf("failed to set deadline: %w", err)
}
session := services.NewSessionIO(conn.LocalAddr().String(), conn)
return session, conn.Close, nil
})
}