From f0d3c874b66282c1a9f825ba6731d8db628b3054 Mon Sep 17 00:00:00 2001 From: "Amir H. Yeganemehr" Date: Tue, 27 Jun 2023 01:22:45 +0200 Subject: [PATCH] Unix Socket listener for S3 server (#4621) Add localSocket option to s3 server --- weed/command/filer.go | 1 + weed/command/s3.go | 28 ++++++++++++++++++++++++++-- weed/command/server.go | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/weed/command/filer.go b/weed/command/filer.go index b5a44eedf..83e2abdac 100644 --- a/weed/command/filer.go +++ b/weed/command/filer.go @@ -105,6 +105,7 @@ func init() { filerS3Options.auditLogConfig = cmdFiler.Flag.String("s3.auditLogConfig", "", "path to the audit log config file") filerS3Options.allowEmptyFolder = cmdFiler.Flag.Bool("s3.allowEmptyFolder", true, "allow empty folders") filerS3Options.allowDeleteBucketNotEmpty = cmdFiler.Flag.Bool("s3.allowDeleteBucketNotEmpty", true, "allow recursive deleting all entries along with bucket") + filerS3Options.localSocket = cmdFiler.Flag.String("s3.localSocket", "", "default to /tmp/seaweedfs-s3-.sock") // start webdav on filer filerStartWebDav = cmdFiler.Flag.Bool("webdav", false, "whether to start webdav gateway") diff --git a/weed/command/s3.go b/weed/command/s3.go index 69f4ec270..f2dffd57e 100644 --- a/weed/command/s3.go +++ b/weed/command/s3.go @@ -4,12 +4,16 @@ import ( "context" "crypto/tls" "fmt" + "net" + "net/http" + "os" + "runtime" + "time" + "github.com/seaweedfs/seaweedfs/weed/s3api/s3err" "google.golang.org/grpc/credentials/tls/certprovider" "google.golang.org/grpc/credentials/tls/certprovider/pemfile" "google.golang.org/grpc/reflection" - "net/http" - "time" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" @@ -44,6 +48,7 @@ type S3Options struct { auditLogConfig *string localFilerSocket *string dataCenter *string + localSocket *string certProvider certprovider.Provider } @@ -64,6 +69,7 @@ func init() { s3StandaloneOptions.allowEmptyFolder = cmdS3.Flag.Bool("allowEmptyFolder", true, "allow empty folders") s3StandaloneOptions.allowDeleteBucketNotEmpty = cmdS3.Flag.Bool("allowDeleteBucketNotEmpty", true, "allow recursive deleting all entries along with bucket") s3StandaloneOptions.localFilerSocket = cmdS3.Flag.String("localFilerSocket", "", "local filer socket path") + s3StandaloneOptions.localSocket = cmdS3.Flag.String("localSocket", "", "default to /tmp/seaweedfs-s3-.sock") } var cmdS3 = &Command{ @@ -229,6 +235,24 @@ func (s3opt *S3Options) startS3Server() bool { *s3opt.bindIp = "localhost" } + if runtime.GOOS != "windows" { + localSocket := *s3opt.localSocket + if localSocket == "" { + localSocket = fmt.Sprintf("/tmp/seaweedfs-s3-%d.sock", *s3opt.port) + } + if err := os.Remove(localSocket); err != nil && !os.IsNotExist(err) { + glog.Fatalf("Failed to remove %s, error: %s", localSocket, err.Error()) + } + go func() { + // start on local unix socket + s3SocketListener, err := net.Listen("unix", localSocket) + if err != nil { + glog.Fatalf("Failed to listen on %s: %v", localSocket, err) + } + httpS.Serve(s3SocketListener) + }() + } + listenAddress := fmt.Sprintf("%s:%d", *s3opt.bindIp, *s3opt.port) s3ApiListener, s3ApiLocalListener, err := util.NewIpAndLocalListeners(*s3opt.bindIp, *s3opt.port, time.Duration(10)*time.Second) if err != nil { diff --git a/weed/command/server.go b/weed/command/server.go index 978cea117..249afdf10 100644 --- a/weed/command/server.go +++ b/weed/command/server.go @@ -147,6 +147,7 @@ func init() { s3Options.auditLogConfig = cmdServer.Flag.String("s3.auditLogConfig", "", "path to the audit log config file") s3Options.allowEmptyFolder = cmdServer.Flag.Bool("s3.allowEmptyFolder", true, "allow empty folders") s3Options.allowDeleteBucketNotEmpty = cmdServer.Flag.Bool("s3.allowDeleteBucketNotEmpty", true, "allow recursive deleting all entries along with bucket") + s3Options.localSocket = cmdServer.Flag.String("s3.localSocket", "", "default to /tmp/seaweedfs-s3-.sock") iamOptions.port = cmdServer.Flag.Int("iam.port", 8111, "iam server http listen port")