This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
162 lines (136 loc) · 5.24 KB
/
server.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"net/url"
"github.com/filecoin-saturn/cassiopeia/httpserver"
"github.com/filecoin-project/lassie/pkg/aggregateeventrecorder"
"github.com/filecoin-project/lassie/pkg/indexerlookup"
"github.com/filecoin-project/lassie/pkg/lassie"
"github.com/filecoin-project/lassie/pkg/net/host"
"github.com/filecoin-project/lassie/pkg/retriever"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/config"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/urfave/cli/v2"
)
func serveAction(cctx *cli.Context) error {
// lassie config
libp2pLowWater := cctx.Int("libp2p-conns-lowwater")
libp2pHighWater := cctx.Int("libp2p-conns-highwater")
concurrentSPRetrievals := cctx.Uint("concurrent-sp-retrievals")
lassieOpts := []lassie.LassieOption{}
if concurrentSPRetrievals > 0 {
lassieOpts = append(lassieOpts, lassie.WithConcurrentSPRetrievals(concurrentSPRetrievals))
}
libp2pOpts := []config.Option{}
if libp2pHighWater != 0 || libp2pLowWater != 0 {
connManager, err := connmgr.NewConnManager(libp2pLowWater, libp2pHighWater)
if err != nil {
return cli.Exit(err, 1)
}
libp2pOpts = append(libp2pOpts, libp2p.ConnectionManager(connManager))
}
lassieCfg, err := buildLassieConfigFromCLIContext(cctx, lassieOpts, libp2pOpts)
if err != nil {
return cli.Exit(err, 1)
}
// http server config
address := cctx.String("address")
port := cctx.Uint("port")
tempDir := cctx.String("tempdir")
maxBlocks := cctx.Uint64("maxblocks")
accessToken := cctx.String("access-token")
httpServerCfg := httpserver.HttpServerConfig{
Address: address,
Port: port,
TempDir: tempDir,
MaxBlocksPerRequest: maxBlocks,
AccessToken: accessToken,
}
// event recorder config
eventRecorderURL := cctx.String("event-recorder-url")
authToken := cctx.String("event-recorder-auth")
instanceID := cctx.String("event-recorder-instance-id")
eventRecorderCfg := &aggregateeventrecorder.EventRecorderConfig{
InstanceID: instanceID,
EndpointURL: eventRecorderURL,
EndpointAuthorization: authToken,
}
lassie, err := lassie.NewLassieWithConfig(cctx.Context, lassieCfg)
if err != nil {
return cli.Exit(err, 1)
}
// create and subscribe an event recorder API if an endpoint URL is set
if eventRecorderCfg.EndpointURL != "" {
eventRecorder := aggregateeventrecorder.NewAggregateEventRecorder(cctx.Context, *eventRecorderCfg)
lassie.RegisterSubscriber(eventRecorder.RetrievalEventSubscriber())
}
httpServer, err := httpserver.NewHttpServer(cctx.Context, lassie, httpServerCfg)
if err != nil {
logger.Errorw("failed to create http server", "err", err)
return cli.Exit(err, 1)
}
serverErrChan := make(chan error, 1)
go func() {
fmt.Printf("Lassie daemon listening on address %s\n", httpServer.Addr())
fmt.Println("Hit CTRL-C to stop the daemon")
serverErrChan <- httpServer.Start()
}()
select {
case <-cctx.Context.Done(): // command was cancelled
case err = <-serverErrChan: // error from server
logger.Errorw("failed to start http server", "err", err)
}
fmt.Println("Shutting down Lassie daemon")
if err = httpServer.Close(); err != nil {
logger.Errorw("failed to close http server", "err", err)
return cli.Exit(err, 1)
}
fmt.Println("Lassie daemon stopped")
return nil
}
func buildLassieConfigFromCLIContext(cctx *cli.Context, lassieOpts []lassie.LassieOption, libp2pOpts []config.Option) (*lassie.LassieConfig, error) {
providerTimeout := cctx.Duration("provider-timeout")
globalTimeout := cctx.Duration("global-timeout")
bitswapConcurrency := cctx.Int("bitswap-concurrency")
lassieOpts = append(lassieOpts, lassie.WithProviderTimeout(providerTimeout))
if globalTimeout > 0 {
lassieOpts = append(lassieOpts, lassie.WithGlobalTimeout(globalTimeout))
}
if len(protocols) > 0 {
lassieOpts = append(lassieOpts, lassie.WithProtocols(protocols))
}
host, err := host.InitHost(cctx.Context, libp2pOpts)
if err != nil {
return nil, err
}
lassieOpts = append(lassieOpts, lassie.WithHost(host))
if len(fetchProviderAddrInfos) > 0 {
finderOpt := lassie.WithFinder(retriever.NewDirectCandidateFinder(host, fetchProviderAddrInfos))
if cctx.IsSet("ipni-endpoint") {
logger.Warn("Ignoring ipni-endpoint flag since direct provider is specified")
}
lassieOpts = append(lassieOpts, finderOpt)
} else if cctx.IsSet("ipni-endpoint") {
endpoint := cctx.String("ipni-endpoint")
endpointUrl, err := url.ParseRequestURI(endpoint)
if err != nil {
logger.Errorw("Failed to parse IPNI endpoint as URL", "err", err)
return nil, fmt.Errorf("cannot parse given IPNI endpoint %s as valid URL: %w", endpoint, err)
}
finder, err := indexerlookup.NewCandidateFinder(indexerlookup.WithHttpEndpoint(endpointUrl))
if err != nil {
logger.Errorw("Failed to instantiate IPNI candidate finder", "err", err)
return nil, err
}
lassieOpts = append(lassieOpts, lassie.WithFinder(finder))
logger.Debug("Using explicit IPNI endpoint to find candidates", "endpoint", endpoint)
}
if len(providerBlockList) > 0 {
lassieOpts = append(lassieOpts, lassie.WithProviderBlockList(providerBlockList))
}
if bitswapConcurrency > 0 {
lassieOpts = append(lassieOpts, lassie.WithBitswapConcurrency(bitswapConcurrency))
}
return lassie.NewLassieConfig(lassieOpts...), nil
}