Skip to content

Commit

Permalink
Move remote disk StorageAPI abstraction from RPC to REST (minio#6464)
Browse files Browse the repository at this point in the history
  • Loading branch information
krishnasrinivas authored and kannappanr committed Oct 5, 2018
1 parent 670f978 commit 81bee93
Show file tree
Hide file tree
Showing 12 changed files with 945 additions and 633 deletions.
2 changes: 1 addition & 1 deletion cmd/object-api-common.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func newStorageAPI(endpoint Endpoint) (storage StorageAPI, err error) {
return newPosix(endpoint.Path)
}

return newStorageRPC(endpoint), nil
return newStorageRESTClient(endpoint), nil
}

// Cleanup a directory recursively.
Expand Down
108 changes: 108 additions & 0 deletions cmd/rest/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Minio Cloud Storage, (C) 2018 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package rest

import (
"context"
"crypto/tls"
"errors"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"

xhttp "github.com/minio/minio/cmd/http"
)

// DefaultRESTTimeout - default RPC timeout is one minute.
const DefaultRESTTimeout = 1 * time.Minute

// Client - http based RPC client.
type Client struct {
httpClient *http.Client
url *url.URL
newAuthToken func() string
}

// Call - make a REST call.
func (c *Client) Call(method string, values url.Values, body io.Reader) (reply io.ReadCloser, err error) {
req, err := http.NewRequest(http.MethodPost, c.url.String()+"/"+method+"?"+values.Encode(), body)
if err != nil {
return nil, err
}

req.Header.Set("Authorization", "Bearer "+c.newAuthToken())
req.Header.Set("X-Minio-Time", time.Now().UTC().Format(time.RFC3339))

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
// Limit the ReadAll(), just in case, because of a bug, the server responds with large data.
r := io.LimitReader(resp.Body, 1024)
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return nil, errors.New(string(b))
}
return resp.Body, nil
}

func newCustomDialContext(timeout time.Duration) func(ctx context.Context, network, addr string) (net.Conn, error) {
return func(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := &net.Dialer{
Timeout: timeout,
KeepAlive: timeout,
DualStack: true,
}

conn, err := dialer.DialContext(ctx, network, addr)
if err != nil {
return nil, err
}

return xhttp.NewTimeoutConn(conn, timeout, timeout), nil
}
}

// NewClient - returns new RPC client.
func NewClient(url *url.URL, tlsConfig *tls.Config, timeout time.Duration, newAuthToken func() string) *Client {
return &Client{
httpClient: &http.Client{
// Transport is exactly same as Go default in https://golang.org/pkg/net/http/#RoundTripper
// except custom DialContext and TLSClientConfig.
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: newCustomDialContext(timeout),
MaxIdleConnsPerHost: 4096,
MaxIdleConns: 4096,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
DisableCompression: true,
},
},
url: url,
newAuthToken: newAuthToken,
}
}
2 changes: 1 addition & 1 deletion cmd/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newCacheObjectsFn() CacheObjectLayer {
// Composed function registering routers for only distributed XL setup.
func registerDistXLRouters(router *mux.Router, endpoints EndpointList) {
// Register storage rpc router only if its a distributed setup.
registerStorageRPCRouters(router, endpoints)
registerStorageRESTHandlers(router, endpoints)

// Register distributed namespace lock.
registerDistNSLockRouter(router)
Expand Down
Loading

0 comments on commit 81bee93

Please sign in to comment.