github.com/maobaolong/goofys@v0.24.1-0.20200717030821-b50ef2d29ddf/api/common/config.go (about)

     1  // Copyright 2015 - 2019 Ka-Hing Cheung
     2  // Copyright 2015 - 2017 Google Inc. All Rights Reserved.
     3  // Copyright 2019 Databricks
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package common
    18  
    19  import (
    20  	"mime"
    21  	"net"
    22  	"net/http"
    23  	"os"
    24  	"strings"
    25  	"time"
    26  )
    27  
    28  type FlagStorage struct {
    29  	// File system
    30  	MountOptions      map[string]string
    31  	MountPoint        string
    32  	MountPointArg     string
    33  	MountPointCreated string
    34  
    35  	Cache    []string
    36  	DirMode  os.FileMode
    37  	FileMode os.FileMode
    38  	Uid      uint32
    39  	Gid      uint32
    40  
    41  	// Common Backend Config
    42  	UseContentType bool
    43  	Endpoint       string
    44  
    45  	Backend interface{}
    46  
    47  	// Tuning
    48  	Cheap        bool
    49  	ExplicitDir  bool
    50  	StatCacheTTL time.Duration
    51  	TypeCacheTTL time.Duration
    52  	HTTPTimeout  time.Duration
    53  
    54  	// Debugging
    55  	DebugFuse  bool
    56  	DebugS3    bool
    57  	Foreground bool
    58  
    59  	// Readahead
    60  	MaxReadAhead   uint32
    61  	ReadAheadChunk uint32
    62  }
    63  
    64  func (flags *FlagStorage) GetMimeType(fileName string) (retMime *string) {
    65  	if flags.UseContentType {
    66  		dotPosition := strings.LastIndex(fileName, ".")
    67  		if dotPosition == -1 {
    68  			return nil
    69  		}
    70  		mimeType := mime.TypeByExtension(fileName[dotPosition:])
    71  		if mimeType == "" {
    72  			return nil
    73  		}
    74  		semicolonPosition := strings.LastIndex(mimeType, ";")
    75  		if semicolonPosition == -1 {
    76  			return &mimeType
    77  		}
    78  		s := mimeType[:semicolonPosition]
    79  		retMime = &s
    80  	}
    81  
    82  	return
    83  }
    84  
    85  func (flags *FlagStorage) Cleanup() {
    86  	if flags.MountPointCreated != "" && flags.MountPointCreated != flags.MountPointArg {
    87  		err := os.Remove(flags.MountPointCreated)
    88  		if err != nil {
    89  			log.Errorf("rmdir %v = %v", flags.MountPointCreated, err)
    90  		}
    91  	}
    92  }
    93  
    94  var defaultHTTPTransport = http.Transport{
    95  	Proxy: http.ProxyFromEnvironment,
    96  	DialContext: (&net.Dialer{
    97  		Timeout:   30 * time.Second,
    98  		KeepAlive: 30 * time.Second,
    99  		DualStack: true,
   100  	}).DialContext,
   101  	MaxIdleConns:          1000,
   102  	MaxIdleConnsPerHost:   1000,
   103  	IdleConnTimeout:       90 * time.Second,
   104  	TLSHandshakeTimeout:   10 * time.Second,
   105  	ExpectContinueTimeout: 10 * time.Second,
   106  }
   107  
   108  func GetHTTPTransport() *http.Transport {
   109  	return &defaultHTTPTransport
   110  }