Repository: dragonflyoss/Dragonfly2
Pull request #1521 (2022-08-03 10:23:55)

feat: add daemon-socket-path
Signed-off-by: Gaius <gaius.qi@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description
- Add daemon socket path.
<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#1521](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (931c577) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/9b0fd1aef420fa0be0da4c5304c496700e6871d5?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (9b0fd1a) will **decrease** coverage by `0.14%`.
> The diff coverage is `75.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521/graphs/tree.svg?width=650&height=150&src=pr&token=PUICJYSAT7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@            Coverage Diff             @@
##             main    #1521      +/-   ##
==========================================
- Coverage   43.57%   43.42%   -0.15%     
==========================================
  Files         116      116              
  Lines       10720    10722       +2     
==========================================
- Hits         4671     4656      -15     
- Misses       5565     5585      +20     
+ Partials      484      481       -3     
```

| Flag | Coverage Δ | |
|---|---|---|
| Object-compatibility-e2etests | `?` | |
| e2etests | `?` | |
| unittests | `43.42% <75.00%> (-0.15%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [client/config/dfget.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2NvbmZpZy9kZmdldC5nbw==) | `29.76% <ø> (ø)` | |
| [client/daemon/daemon.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9kYWVtb24uZ28=) | `5.14% <ø> (+0.02%)` | :arrow_up: |
| [pkg/dfpath/dfpath.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL2RmcGF0aC9kZnBhdGguZ28=) | `68.08% <75.00%> (+0.64%)` | :arrow_up: |
| [client/daemon/peer/peertask\_piecetask\_poller.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3BpZWNldGFza19wb2xsZXIuZ28=) | `30.43% <0.00%> (-16.67%)` | :arrow_down: |
| [client/daemon/peer/piece\_manager.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BpZWNlX21hbmFnZXIuZ28=) | `53.01% <0.00%> (-0.54%)` | :arrow_down: |
| [client/daemon/peer/peertask\_conductor.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/1521/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX2NvbmR1Y3Rvci5nbw==) | `57.68% <0.00%> (+0.78%)` | :arrow_up: |


Patch:
diff --git a/client/config/dfget.go b/client/config/dfget.go
index 88b4cf26..a027b2f1 100644
--- a/client/config/dfget.go
+++ b/client/config/dfget.go
@@ -70,6 +70,9 @@ type ClientOption struct {
 	// CallSystem system name that executes dfget.
 	CallSystem string `yaml:"callSystem,omitempty" mapstructure:"callSystem,omitempty"`
 
+	// DaemonSock is daemon download socket path.
+	DaemonSock string `yaml:"daemonSock,omitempty" mapstructure:"daemon-sock,omitempty"`
+
 	// Pattern download pattern, must be 'p2p' or 'seed-peer' or 'source',
 	// default:`p2p`.
 	Pattern string `yaml:"pattern,omitempty" mapstructure:"pattern,omitempty"`
diff --git a/client/daemon/daemon.go b/client/daemon/daemon.go
index e75675e2..9bb787a8 100644
--- a/client/daemon/daemon.go
+++ b/client/daemon/daemon.go
@@ -369,13 +369,10 @@ func (cd *clientDaemon) Serve() error {
 	if cd.Option.Download.DownloadGRPC.UnixListen == nil {
 		return errors.New("download grpc unix listen option is empty")
 	}
-	if cd.Option.Download.DownloadGRPC.UnixListen.Socket == "" {
-		cd.Option.Download.DownloadGRPC.UnixListen.Socket = cd.dfpath.DaemonSockPath()
-	}
 	_ = os.Remove(cd.Option.Download.DownloadGRPC.UnixListen.Socket)
 	downloadListener, err := rpc.Listen(dfnet.NetAddr{
 		Type: dfnet.UNIX,
-		Addr: cd.Option.Download.DownloadGRPC.UnixListen.Socket,
+		Addr: cd.dfpath.DaemonSockPath(),
 	})
 	if err != nil {
 		logger.Errorf("failed to listen for download grpc service: %v", err)
diff --git a/cmd/dfget/cmd/daemon.go b/cmd/dfget/cmd/daemon.go
index 0d248e4e..85f8bcf5 100644
--- a/cmd/dfget/cmd/daemon.go
+++ b/cmd/dfget/cmd/daemon.go
@@ -108,6 +108,10 @@ func initDaemonDfpath(cfg *config.DaemonOption) (dfpath.Dfpath, error) {
 		options = append(options, dfpath.WithLogDir(cfg.LogDir))
 	}
 
+	if cfg.Download.DownloadGRPC.UnixListen.Socket != "" {
+		options = append(options, dfpath.WithDownloadUnixSocketPath(cfg.Download.DownloadGRPC.UnixListen.Socket))
+	}
+
 	cacheDir := dfpath.DefaultCacheDir
 	if cfg.CacheDir != "" {
 		cacheDir = cfg.CacheDir
diff --git a/cmd/dfget/cmd/root.go b/cmd/dfget/cmd/root.go
index d86e8dd0..777e175f 100644
--- a/cmd/dfget/cmd/root.go
+++ b/cmd/dfget/cmd/root.go
@@ -164,6 +164,8 @@ func init() {
 
 	flagSet.String("callsystem", dfgetConfig.CallSystem, "The caller name which is mainly used for statistics and access control")
 
+	flagSet.String("daemon-sock", dfgetConfig.DaemonSock, "Download socket path of daemon. In linux, default value is /var/run/dfdaemon.sock, in macos(just for testing), default value is /tmp/dfdaemon.sock")
+
 	flagSet.String("workhome", dfgetConfig.WorkHome, "Dfget working directory")
 
 	flagSet.String("logdir", dfgetConfig.LogDir, "Dfget log directory")
@@ -205,6 +207,10 @@ func initDfgetDfpath(cfg *config.ClientOption) (dfpath.Dfpath, error) {
 		options = append(options, dfpath.WithLogDir(cfg.LogDir))
 	}
 
+	if cfg.DaemonSock != "" {
+		options = append(options, dfpath.WithDownloadUnixSocketPath(cfg.DaemonSock))
+	}
+
 	return dfpath.New(options...)
 }
 
diff --git a/pkg/dfpath/dfpath.go b/pkg/dfpath/dfpath.go
index f3052ea1..10596817 100644
--- a/pkg/dfpath/dfpath.go
+++ b/pkg/dfpath/dfpath.go
@@ -21,6 +21,7 @@ package dfpath
 import (
 	"io/fs"
 	"os"
+	"path"
 	"path/filepath"
 	"sync"
 
@@ -96,13 +97,21 @@ func WithPluginDir(dir string) Option {
 	}
 }
 
+// WithDownloadUnixSocketPath set unix socket path.
+func WithDownloadUnixSocketPath(path string) Option {
+	return func(d *dfpath) {
+		d.daemonSockPath = path
+	}
+}
+
 // New returns a new dfpath interface.
 func New(options ...Option) (Dfpath, error) {
 	cache.Do(func() {
 		d := &dfpath{
-			workHome:  DefaultWorkHome,
-			logDir:    DefaultLogDir,
-			pluginDir: DefaultPluginDir,
+			workHome:       DefaultWorkHome,
+			logDir:         DefaultLogDir,
+			pluginDir:      DefaultPluginDir,
+			daemonSockPath: DefaultDownloadUnixSocketPath,
 		}
 
 		for _, opt := range options {
@@ -110,7 +119,6 @@ func New(options ...Option) (Dfpath, error) {
 		}
 
 		// Initialize dfdaemon path.
-		d.daemonSockPath = filepath.Join(d.workHome, "daemon.sock")
 		d.daemonLockPath = filepath.Join(d.workHome, "daemon.lock")
 		d.dfgetLockPath = filepath.Join(d.workHome, "dfget.lock")
 
@@ -129,6 +137,11 @@ func New(options ...Option) (Dfpath, error) {
 			cache.err = multierror.Append(cache.err, err)
 		}
 
+		// Create unix socket directory.
+		if err := os.MkdirAll(path.Dir(d.daemonSockPath), fs.FileMode(0755)); err != nil {
+			cache.err = multierror.Append(cache.err, err)
+		}
+
 		// Create cache directory.
 		if d.cacheDir != "" {
 			if err := os.MkdirAll(d.cacheDir, fs.FileMode(0755)); err != nil {
diff --git a/pkg/dfpath/dfpath_darwin.go b/pkg/dfpath/dfpath_darwin.go
index 1e52f44f..9992f1ff 100644
--- a/pkg/dfpath/dfpath_darwin.go
+++ b/pkg/dfpath/dfpath_darwin.go
@@ -31,3 +31,4 @@ var DefaultConfigDir = filepath.Join(DefaultWorkHome, "config")
 var DefaultLogDir = filepath.Join(DefaultWorkHome, "logs")
 var DefaultDataDir = filepath.Join(DefaultWorkHome, "data")
 var DefaultPluginDir = filepath.Join(DefaultWorkHome, "plugins")
+var DefaultDownloadUnixSocketPath = filepath.Join(DefaultWorkHome, "dfdaemon.sock")
diff --git a/pkg/dfpath/dfpath_linux.go b/pkg/dfpath/dfpath_linux.go
index 62350489..321e6ee2 100644
--- a/pkg/dfpath/dfpath_linux.go
+++ b/pkg/dfpath/dfpath_linux.go
@@ -25,3 +25,4 @@ var DefaultConfigDir = "/etc/dragonfly"
 var DefaultLogDir = "/var/log/dragonfly"
 var DefaultDataDir = "/var/lib/dragonfly"
 var DefaultPluginDir = "/usr/local/dragonfly/plugins"
+var DefaultDownloadUnixSocketPath = "/var/run/dfdaemon.sock"


Test patch:
diff --git a/pkg/dfpath/dfpath_test.go b/pkg/dfpath/dfpath_test.go
index c5ac694f..f0c5ba94 100644
--- a/pkg/dfpath/dfpath_test.go
+++ b/pkg/dfpath/dfpath_test.go
@@ -52,6 +52,7 @@ func TestNew(t *testing.T) {
 				assert.Equal(d.LogDir(), DefaultLogDir)
 				assert.Equal(d.DataDir(), "")
 				assert.Equal(d.PluginDir(), DefaultPluginDir)
+				assert.Equal(d.DaemonSockPath(), DefaultDownloadUnixSocketPath)
 			},
 		},
 		{
@@ -84,6 +85,7 @@ func TestNew(t *testing.T) {
 				assert.Equal(d.LogDir(), DefaultLogDir)
 				assert.Equal(d.DataDir(), "")
 				assert.Equal(d.PluginDir(), DefaultPluginDir)
+				assert.Equal(d.DaemonSockPath(), DefaultDownloadUnixSocketPath)
 			},
 		},
 		{
@@ -100,6 +102,7 @@ func TestNew(t *testing.T) {
 				assert.Equal(d.LogDir(), "foo")
 				assert.Equal(d.DataDir(), "")
 				assert.Equal(d.PluginDir(), DefaultPluginDir)
+				assert.Equal(d.DaemonSockPath(), DefaultDownloadUnixSocketPath)
 			},
 		},
 		{
@@ -116,6 +119,24 @@ func TestNew(t *testing.T) {
 				assert.Equal(d.LogDir(), DefaultLogDir)
 				assert.Equal(d.DataDir(), "")
 				assert.Equal(d.PluginDir(), "foo")
+				assert.Equal(d.DaemonSockPath(), DefaultDownloadUnixSocketPath)
+			},
+		},
+		{
+			name:    "new dfpath by daemonSockPath",
+			options: []Option{WithDownloadUnixSocketPath("foo")},
+			expect: func(t *testing.T, options []Option) {
+				assert := assert.New(t)
+				cache.Once = sync.Once{}
+				cache.err = &multierror.Error{}
+				d, err := New(options...)
+				assert.NoError(err)
+				assert.Equal(d.WorkHome(), DefaultWorkHome)
+				assert.Equal(d.CacheDir(), "")
+				assert.Equal(d.LogDir(), DefaultLogDir)
+				assert.Equal(d.DataDir(), "")
+				assert.Equal(d.PluginDir(), DefaultPluginDir)
+				assert.Equal(d.DaemonSockPath(), "foo")
 			},
 		},
 	}