Skip to content

Commit

Permalink
Reuse HTTP clients
Browse files Browse the repository at this point in the history
  • Loading branch information
ishank011 committed Oct 21, 2020
1 parent b1f8529 commit 93ef37a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 48 deletions.
12 changes: 12 additions & 0 deletions cmd/reva/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/c-bata/go-prompt"
"github.com/cs3org/reva/pkg/rhttp"
)

var (
Expand All @@ -37,6 +40,8 @@ var (

gitCommit, buildDate, version, goVersion string

client *http.Client

commands = []*command{
versionCommand(),
configureCommand(),
Expand Down Expand Up @@ -94,6 +99,13 @@ func main() {
}
}

client = rhttp.GetHTTPClient(
// TODO make insecure configurable
rhttp.Insecure(true),
// TODO make timeout configurable
rhttp.Timeout(time.Duration(24*int64(time.Hour))),
)

generateMainUsage()
executor := Executor{Timeout: timeout}
completer := Completer{DisableArgPrompt: disableargprompt}
Expand Down
19 changes: 2 additions & 17 deletions cmd/reva/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"os"
"path/filepath"
"strconv"
"time"

"github.com/cs3org/reva/internal/http/services/datagateway"
"github.com/pkg/errors"
Expand Down Expand Up @@ -159,15 +158,7 @@ func uploadCommand() *command {
q.Add("xs_type", storageprovider.GRPC2PKGXS(xsType).String())
httpReq.URL.RawQuery = q.Encode()

httpClient := rhttp.GetHTTPClient(
rhttp.Context(ctx),
// TODO make insecure configurable
rhttp.Insecure(true),
// TODO make timeout configurable
rhttp.Timeout(time.Duration(24*int64(time.Hour))),
)

httpRes, err := httpClient.Do(httpReq)
httpRes, err := client.Do(httpReq)
if err != nil {
return err
}
Expand All @@ -179,13 +170,7 @@ func uploadCommand() *command {
// create the tus client.
c := tus.DefaultConfig()
c.Resume = true
c.HttpClient = rhttp.GetHTTPClient(
rhttp.Context(ctx),
// TODO make insecure configurable
rhttp.Insecure(true),
// TODO make timeout configurable
rhttp.Timeout(time.Duration(24*int64(time.Hour))),
)
c.HttpClient = client
c.Store, err = memorystore.NewMemoryStore()
if err != nil {
return err
Expand Down
20 changes: 6 additions & 14 deletions internal/grpc/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func init() {

type service struct {
provider app.Provider
client *http.Client
conf *config
}

Expand Down Expand Up @@ -78,6 +79,9 @@ func New(m map[string]interface{}, ss *grpc.Server) (rgrpc.Service, error) {
service := &service{
conf: c,
provider: provider,
client: rhttp.GetHTTPClient(
rhttp.Timeout(5 * time.Second),
),
}

return service, nil
Expand Down Expand Up @@ -119,12 +123,6 @@ func getProvider(c *config) (app.Provider, error) {
}

func (s *service) getWopiAppEndpoints(ctx context.Context) (map[string]interface{}, error) {
httpClient := rhttp.GetHTTPClient(
rhttp.Context(ctx),
// calls to WOPI are expected to take a very short time, 5s (though hardcoded) ought to be far enough
rhttp.Timeout(time.Duration(5*int64(time.Second))),
)

// TODO this query will eventually be served by Reva.
// For the time being it is a remnant of the CERNBox-specific WOPI server, which justifies the /cbox path in the URL.
wopiurl, err := url.Parse(s.conf.WopiURL)
Expand All @@ -136,7 +134,7 @@ func (s *service) getWopiAppEndpoints(ctx context.Context) (map[string]interface
if err != nil {
return nil, err
}
appsRes, err := httpClient.Do(appsReq)
appsRes, err := s.client.Do(appsReq)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -164,12 +162,6 @@ func (s *service) OpenFileInAppProvider(ctx context.Context, req *providerpb.Ope

log := appctx.GetLogger(ctx)

httpClient := rhttp.GetHTTPClient(
rhttp.Context(ctx),
// calls to WOPI are expected to take a very short time, 5s (though hardcoded) ought to be far enough
rhttp.Timeout(time.Duration(5*int64(time.Second))),
)

wopiurl, err := url.Parse(s.conf.WopiURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -202,7 +194,7 @@ func (s *service) OpenFileInAppProvider(ctx context.Context, req *providerpb.Ope

httpReq.URL.RawQuery = q.Encode()

openRes, err := httpClient.Do(httpReq)
openRes, err := s.client.Do(httpReq)

if err != nil {
res := &providerpb.OpenFileInAppProviderResponse{
Expand Down
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocdav/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (s *svc) tusUpload(ctx context.Context, dataServerURL string, transferToken
Str("dir", path.Dir(fn)).
Msg("tus.NewUpload")

upload := tus.NewUpload(body, int64(length), metadata, "")
upload := tus.NewUpload(body, length, metadata, "")

// create the uploader.
c.Store.Set(upload.Fingerprint, dataServerURL)
Expand Down
9 changes: 6 additions & 3 deletions pkg/ocm/invite/manager/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type manager struct {
config *config
sync.Mutex // concurrent access to the file
model *inviteModel
client *http.Client
}

type config struct {
Expand Down Expand Up @@ -103,6 +104,10 @@ func New(m map[string]interface{}) (invite.Manager, error) {
manager := &manager{
config: config,
model: model,
client: rhttp.GetHTTPClient(
rhttp.Timeout(5*time.Second),
rhttp.Insecure(config.InsecureConnections),
),
}

return manager, nil
Expand Down Expand Up @@ -214,15 +219,13 @@ func (m *manager) ForwardInvite(ctx context.Context, invite *invitepb.InviteToke
u.Path = path.Join(u.Path, acceptInviteEndpoint)
recipientURL := u.String()

client := rhttp.GetHTTPClient(rhttp.Insecure(m.config.InsecureConnections))

req, err := http.NewRequest("POST", recipientURL, strings.NewReader(requestBody.Encode()))
if err != nil {
return errors.Wrap(err, "json: error framing post request")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")

resp, err := client.Do(req)
resp, err := m.client.Do(req)
if err != nil {
err = errors.Wrap(err, "json: error sending post request")
return err
Expand Down
9 changes: 6 additions & 3 deletions pkg/ocm/invite/manager/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,17 @@ func New(m map[string]interface{}) (invite.Manager, error) {
Invites: sync.Map{},
AcceptedUsers: sync.Map{},
Config: c,
Client: rhttp.GetHTTPClient(
rhttp.Timeout(5*time.Second),
rhttp.Insecure(c.InsecureConnections),
),
}, nil
}

type manager struct {
Invites sync.Map
AcceptedUsers sync.Map
Client *http.Client
Config *config
}

Expand Down Expand Up @@ -114,15 +119,13 @@ func (m *manager) ForwardInvite(ctx context.Context, invite *invitepb.InviteToke
u.Path = path.Join(u.Path, acceptInviteEndpoint)
recipientURL := u.String()

client := rhttp.GetHTTPClient(rhttp.Insecure(m.Config.InsecureConnections))

req, err := http.NewRequest("POST", recipientURL, strings.NewReader(requestBody.Encode()))
if err != nil {
return errors.Wrap(err, "json: error framing post request")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")

resp, err := client.Do(req)
resp, err := m.Client.Do(req)
if err != nil {
err = errors.Wrap(err, "memory: error sending post request")
return err
Expand Down
8 changes: 5 additions & 3 deletions pkg/ocm/share/manager/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func New(m map[string]interface{}) (share.Manager, error) {
mgr := &mgr{
c: c,
model: model,
client: rhttp.GetHTTPClient(
rhttp.Timeout(5 * time.Second),
),
}

return mgr, nil
Expand Down Expand Up @@ -136,6 +139,7 @@ type mgr struct {
c *config
sync.Mutex // concurrent access to the file
model *shareModel
client *http.Client
}

func (m *shareModel) Save() error {
Expand Down Expand Up @@ -299,15 +303,13 @@ func (m *mgr) Share(ctx context.Context, md *provider.ResourceId, g *ocm.ShareGr
u.Path = path.Join(u.Path, createOCMCoreShareEndpoint)
recipientURL := u.String()

client := rhttp.GetHTTPClient(rhttp.Insecure(m.c.InsecureConnections))

req, err := http.NewRequest("POST", recipientURL, strings.NewReader(requestBody.Encode()))
if err != nil {
return nil, errors.Wrap(err, "json: error framing post request")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")

resp, err := client.Do(req)
resp, err := m.client.Do(req)
if err != nil {
err = errors.Wrap(err, "json: error sending post request")
return nil, err
Expand Down
8 changes: 5 additions & 3 deletions pkg/ocm/share/manager/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ func New(m map[string]interface{}) (share.Manager, error) {
c: c,
shares: sync.Map{},
state: state,
client: rhttp.GetHTTPClient(
rhttp.Timeout(5 * time.Second),
),
}, nil
}

type mgr struct {
c *config
shares sync.Map
state map[string]map[string]ocm.ShareState
client *http.Client
}

type config struct {
Expand Down Expand Up @@ -209,15 +213,13 @@ func (m *mgr) Share(ctx context.Context, md *provider.ResourceId, g *ocm.ShareGr
u.Path = path.Join(u.Path, createOCMCoreShareEndpoint)
recipientURL := u.String()

client := rhttp.GetHTTPClient(rhttp.Insecure(m.c.InsecureConnections))

req, err := http.NewRequest("POST", recipientURL, strings.NewReader(requestBody.Encode()))
if err != nil {
return nil, errors.Wrap(err, "json: error framing post request")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")

resp, err := client.Do(req)
resp, err := m.client.Do(req)
if err != nil {
err = errors.Wrap(err, "memory: error sending post request")
return nil, err
Expand Down
11 changes: 7 additions & 4 deletions pkg/user/manager/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type manager struct {
conf *config
redisPool *redis.Pool
oidcToken OIDCToken
client *http.Client
}

// OIDCToken stores the OIDC token used to authenticate requests to the REST API service
Expand Down Expand Up @@ -128,6 +129,10 @@ func New(m map[string]interface{}) (user.Manager, error) {
return &manager{
conf: c,
redisPool: redisPool,
client: rhttp.GetHTTPClient(
rhttp.Timeout(10*time.Second),
rhttp.Insecure(true),
),
}, nil
}

Expand Down Expand Up @@ -156,15 +161,14 @@ func (m *manager) getAPIToken(ctx context.Context) (string, time.Time, error) {
"audience": {m.conf.TargetAPI},
}

httpClient := rhttp.GetHTTPClient(rhttp.Context(ctx), rhttp.Timeout(10*time.Second), rhttp.Insecure(true))
httpReq, err := http.NewRequest("POST", m.conf.OIDCTokenEndpoint, strings.NewReader(params.Encode()))
if err != nil {
return "", time.Time{}, err
}
httpReq.SetBasicAuth(m.conf.ClientID, m.conf.ClientSecret)
httpReq.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")

httpRes, err := httpClient.Do(httpReq)
httpRes, err := m.client.Do(httpReq)
if err != nil {
return "", time.Time{}, err
}
Expand Down Expand Up @@ -192,7 +196,6 @@ func (m *manager) sendAPIRequest(ctx context.Context, url string) ([]interface{}
return nil, err
}

httpClient := rhttp.GetHTTPClient(rhttp.Context(ctx), rhttp.Timeout(10*time.Second), rhttp.Insecure(true))
httpReq, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
Expand All @@ -203,7 +206,7 @@ func (m *manager) sendAPIRequest(ctx context.Context, url string) ([]interface{}
// the token and expiration time while this request is in progress, the current token will still be valid.
httpReq.Header.Set("Authorization", "Bearer "+m.oidcToken.apiToken)

httpRes, err := httpClient.Do(httpReq)
httpRes, err := m.client.Do(httpReq)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 93ef37a

Please sign in to comment.