-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathclients.go
112 lines (97 loc) · 3.54 KB
/
clients.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
110
111
112
// Copyright © 2020 - 2024 Attestant Limited.
// 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 main
import (
"context"
"fmt"
"strings"
"sync"
eth2client "github.com/attestantio/go-eth2-client"
httpclient "github.com/attestantio/go-eth2-client/http"
multiclient "github.com/attestantio/go-eth2-client/multi"
"github.com/attestantio/vouch/services/metrics"
"github.com/attestantio/vouch/util"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
var (
knownClients = make(map[string]eth2client.Service)
knownClientsMu sync.Mutex
)
// fetchClient fetches a client service, instantiating it if required.
func fetchClient(ctx context.Context, monitor metrics.Service, address string) (eth2client.Service, error) {
if address == "" {
return nil, errors.New("no address supplied for client")
}
knownClientsMu.Lock()
client, exists := knownClients[address]
knownClientsMu.Unlock()
if !exists {
var err error
client, err = httpclient.New(ctx,
httpclient.WithLogLevel(util.LogLevel(fmt.Sprintf("eth2client.%s", address))),
httpclient.WithMonitor(monitor),
httpclient.WithTimeout(util.Timeout(fmt.Sprintf("eth2client.%s", address))),
httpclient.WithAddress(address),
httpclient.WithAllowDelayedStart(viper.GetBool("eth2client.allow-delayed-start")),
httpclient.WithExtraHeaders(map[string]string{
"User-Agent": fmt.Sprintf("Vouch/%s", ReleaseVersion),
}),
httpclient.WithReducedMemoryUsage(util.HierarchicalBool("reduced-memory-usage", fmt.Sprintf("eth2client.%s", address))),
)
if err != nil {
return nil, errors.Wrap(err, "failed to initiate consensus client")
}
knownClientsMu.Lock()
knownClients[address] = client
knownClientsMu.Unlock()
}
return client, nil
}
// fetchMulticlient fetches a multiclient service, instantiating it if required.
func fetchMultiClient(ctx context.Context, monitor metrics.Service, name string, addresses []string) (eth2client.Service, error) {
if len(addresses) == 0 {
return nil, errors.New("no addresses supplied for multiclient")
}
multiID := fmt.Sprintf("multi:%s", strings.Join(addresses, ","))
knownClientsMu.Lock()
client, exists := knownClients[multiID]
knownClientsMu.Unlock()
if !exists {
// Fetch or create the individual clients.
clients := make([]eth2client.Service, 0, len(addresses))
for _, address := range addresses {
client, err := fetchClient(ctx, monitor, address)
if err != nil {
log.Error().Err(err).Str("address", address).Msg("Cannot access client for multiclient; dropping from list")
continue
}
clients = append(clients, client)
}
var err error
client, err = multiclient.New(ctx,
multiclient.WithMonitor(monitor),
multiclient.WithLogLevel(util.LogLevel("eth2client.multi")),
multiclient.WithClients(clients),
multiclient.WithName(name),
multiclient.WithAllowDelayedStart(viper.GetBool("eth2client.allow-delayed-start")),
)
if err != nil {
return nil, errors.Wrap(err, "failed to initiate multiclient")
}
knownClientsMu.Lock()
knownClients[multiID] = client
knownClientsMu.Unlock()
}
return client, nil
}