forked from hyperledger-labs/go-perun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dialer.go
109 lines (90 loc) 路 3.17 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2020 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package simple
import (
"context"
"net"
"sync"
"time"
"github.com/pkg/errors"
pkgsync "perun.network/go-perun/pkg/sync"
"perun.network/go-perun/wallet"
"perun.network/go-perun/wire"
wirenet "perun.network/go-perun/wire/net"
)
// Dialer is a simple lookup-table based dialer that can dial known peers.
// New peer addresses can be added via Register().
type Dialer struct {
mutex sync.RWMutex // Protects peers.
peers map[wallet.AddrKey]string // Known peer addresses.
dialer net.Dialer // Used to dial connections.
network string // The socket type.
pkgsync.Closer
}
var _ wirenet.Dialer = (*Dialer)(nil)
// NewNetDialer creates a new dialer with a preset default timeout for dial
// attempts. Leaving the timeout as 0 will result in no timeouts. Standard OS
// timeouts may still apply even when no timeout is selected. The network string
// controls the type of connection that the dialer can dial.
func NewNetDialer(network string, defaultTimeout time.Duration) *Dialer {
return &Dialer{
peers: make(map[wallet.AddrKey]string),
dialer: net.Dialer{Timeout: defaultTimeout},
network: network,
}
}
// NewTCPDialer is a short-hand version of NewNetDialer for creating TCP dialers.
func NewTCPDialer(defaultTimeout time.Duration) *Dialer {
return NewNetDialer("tcp", defaultTimeout)
}
// NewUnixDialer is a short-hand version of NewNetDialer for creating Unix dialers.
func NewUnixDialer(defaultTimeout time.Duration) *Dialer {
return NewNetDialer("unix", defaultTimeout)
}
func (d *Dialer) get(key wallet.AddrKey) (string, bool) {
d.mutex.RLock()
defer d.mutex.RUnlock()
host, ok := d.peers[key]
return host, ok
}
// Dial implements Dialer.Dial().
func (d *Dialer) Dial(ctx context.Context, addr wire.Address) (wirenet.Conn, error) {
done := make(chan struct{})
defer close(done)
host, ok := d.get(wallet.Key(addr))
if !ok {
return nil, errors.New("peer not found")
}
// To combine the provided context with the Dialer's Closer as specified by
// the Dialer interface, we have to use some goroutine trickery.
wrappedCtx, cancel := context.WithCancel(ctx)
go func() {
defer cancel()
select {
case <-d.Closed():
case <-done:
}
}()
conn, err := d.dialer.DialContext(wrappedCtx, d.network, host)
if err != nil {
return nil, errors.Wrap(err, "failed to dial peer")
}
return wirenet.NewIoConn(conn), nil
}
// Register registers a network address for a peer address.
func (d *Dialer) Register(addr wire.Address, address string) {
d.mutex.Lock()
defer d.mutex.Unlock()
d.peers[wallet.Key(addr)] = address
}