Skip to content

Commit bb682bc

Browse files
committed
ipfs registry: allow timeout and retry
IPFS occasionally hangs during reading chunks and requires retrying the request. This commit adds the following options - `--read-retry-num`: times to retry query on IPFS - `--read-timeout`: timeout duration of a read request to IPFS Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent c9b5992 commit bb682bc

File tree

4 files changed

+172
-73
lines changed

4 files changed

+172
-73
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,8 @@ Usage: `nerdctl ipfs registry up [OPTIONS]`
11991199

12001200
Flags:
12011201
- :nerd_face: `--listen-registry`: Address to listen (default `localhost:5050`)
1202+
- :nerd_face: `--read-retry-num`: Times to retry query on IPFS (default 0 (no retry))
1203+
- :nerd_face: `--read-timeout`: Timeout duration of a read request to IPFS (defualt 0 (no timeout))
12021204

12031205
### :nerd_face: nerdctl ipfs registry down
12041206
Stop and remove read-only local registry backed by IPFS.
@@ -1215,6 +1217,8 @@ Usage: `nerdctl ipfs registry serve [OPTIONS]`
12151217
Flags:
12161218
- :nerd_face: `--ipfs-address`: Multiaddr of IPFS API (default is pulled from `$IPFS_PATH/api` file. If `$IPFS_PATH` env var is not present, it defaults to `~/.ipfs`).
12171219
- :nerd_face: `--listen-registry`: Address to listen (default `localhost:5050`).
1220+
- :nerd_face: `--read-retry-num`: Times to retry query on IPFS (default 0 (no retry))
1221+
- :nerd_face: `--read-timeout`: Timeout duration of a read request to IPFS (defualt 0 (no timeout))
12181222

12191223
## Global flags
12201224
- :nerd_face: :blue_square: `--address`: containerd address, optionally with "unix://" prefix

cmd/nerdctl/ipfs_registry_serve.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import (
2626
"github.com/spf13/cobra"
2727
)
2828

29-
const defaultIPFSRegistry = "localhost:5050"
29+
const (
30+
defaultIPFSRegistry = "localhost:5050"
31+
defaultIPFSReadRetryNum = 0
32+
defaultIPFSReadTimeoutDuration = 0
33+
)
3034

3135
func newIPFSRegistryServeCommand() *cobra.Command {
3236
var ipfsRegistryServeCommand = &cobra.Command{
@@ -39,6 +43,8 @@ func newIPFSRegistryServeCommand() *cobra.Command {
3943

4044
ipfsRegistryServeCommand.PersistentFlags().String("listen-registry", defaultIPFSRegistry, "address to listen")
4145
ipfsRegistryServeCommand.PersistentFlags().String("ipfs-address", "", "multiaddr of IPFS API (default is pulled from $IPFS_PATH/api file. If $IPFS_PATH env var is not present, it defaults to ~/.ipfs)")
46+
ipfsRegistryServeCommand.PersistentFlags().Int("read-retry-num", defaultIPFSReadRetryNum, "times to retry query on IPFS. Zero or lower means no retry.")
47+
ipfsRegistryServeCommand.PersistentFlags().Duration("read-timeout", defaultIPFSReadTimeoutDuration, "timeout duration of a read request to IPFS. Zero means no timeout.")
4248

4349
return ipfsRegistryServeCommand
4450
}
@@ -52,6 +58,14 @@ func ipfsRegistryServeAction(cmd *cobra.Command, args []string) error {
5258
if err != nil {
5359
return err
5460
}
61+
readTimeout, err := cmd.Flags().GetDuration("read-timeout")
62+
if err != nil {
63+
return err
64+
}
65+
readRetryNum, err := cmd.Flags().GetInt("read-retry-num")
66+
if err != nil {
67+
return err
68+
}
5569
var ipfsClient *httpapi.HttpApi
5670
if ipfsAddressStr != "" {
5771
a, err := multiaddr.NewMultiaddr(ipfsAddressStr)
@@ -68,7 +82,14 @@ func ipfsRegistryServeAction(cmd *cobra.Command, args []string) error {
6882
return err
6983
}
7084
}
85+
h, err := ipfs.NewRegistry(ipfsClient, ipfs.RegistryOptions{
86+
ReadRetryNum: readRetryNum,
87+
ReadTimeout: readTimeout,
88+
})
89+
if err != nil {
90+
return err
91+
}
7192
logrus.Infof("serving on %v", listenAddress)
72-
http.Handle("/", ipfs.NewRegistry(ipfsClient))
93+
http.Handle("/", h)
7394
return http.ListenAndServe(listenAddress, nil)
7495
}

cmd/nerdctl/ipfs_registry_up.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func newIPFSRegistryUpCommand() *cobra.Command {
4444
}
4545

4646
ipfsRegistryUpCommand.PersistentFlags().String("listen-registry", defaultIPFSRegistry, "address to listen")
47+
ipfsRegistryUpCommand.PersistentFlags().Int("read-retry-num", defaultIPFSReadRetryNum, "times to retry query on IPFS. Zero or lower means no retry.")
48+
ipfsRegistryUpCommand.PersistentFlags().Duration("read-timeout", defaultIPFSReadTimeoutDuration, "timeout duration of a read request to IPFS. Zero means no timeout.")
4749

4850
return ipfsRegistryUpCommand
4951
}
@@ -82,6 +84,14 @@ func runRegistryAsContainer(cmd *cobra.Command) error {
8284
if err != nil {
8385
return err
8486
}
87+
readTimeout, err := cmd.Flags().GetDuration("read-timeout")
88+
if err != nil {
89+
return err
90+
}
91+
readRetryNum, err := cmd.Flags().GetInt("read-retry-num")
92+
if err != nil {
93+
return err
94+
}
8595
dataStore, err := getDataStore(cmd)
8696
if err != nil {
8797
return err
@@ -108,6 +118,7 @@ func runRegistryAsContainer(cmd *cobra.Command) error {
108118
"run", "-d", "--name", ipfsRegistryContainerName, "--net=host", "--entrypoint", "/mnt/nerdctl",
109119
"--read-only", "-v", nerdctlCmd+":/mnt/nerdctl:ro", "--rootfs", registryRoot,
110120
"ipfs", "registry", "serve", "--ipfs-address", ipfsAPIAddr.String(), "--listen-registry", listenAddress,
121+
"--read-retry-num", fmt.Sprintf("%d", readRetryNum), "--read-timeout", fmt.Sprintf("%s", readTimeout),
111122
)...).CombinedOutput(); err != nil {
112123
return fmt.Errorf("failed to execute registry: %v: %v", string(out), err)
113124
}

0 commit comments

Comments
 (0)