github.com/cs3org/reva/v2@v2.27.7/internal/http/services/owncloud/ocs/cache.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package ocs
    20  
    21  import (
    22  	"context"
    23  	"net/http"
    24  	"net/http/httptest"
    25  
    26  	"github.com/cs3org/reva/v2/pkg/appctx"
    27  	ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
    28  	"google.golang.org/grpc/metadata"
    29  )
    30  
    31  func (s *svc) cacheWarmup(w http.ResponseWriter, r *http.Request) {
    32  	if s.warmupCacheTracker != nil {
    33  		u, ok1 := ctxpkg.ContextGetUser(r.Context())
    34  		tkn, ok2 := ctxpkg.ContextGetToken(r.Context())
    35  		if !ok1 || !ok2 {
    36  			return
    37  		}
    38  
    39  		log := appctx.GetLogger(r.Context())
    40  
    41  		// We make a copy of the context because the original one comes with its cancel channel,
    42  		// so once the initial request is finished, this ctx gets cancelled as well.
    43  		// And in most of the cases, the warmup takes a longer amount of time to complete than the original request.
    44  		// TODO: Check if we can come up with a better solution, eg, https://stackoverflow.com/a/54132324
    45  		ctx := context.Background()
    46  		ctx = appctx.WithLogger(ctx, log)
    47  		ctx = ctxpkg.ContextSetUser(ctx, u)
    48  		ctx = ctxpkg.ContextSetToken(ctx, tkn)
    49  		ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, tkn)
    50  
    51  		req, _ := http.NewRequest("GET", "", nil)
    52  		req = req.WithContext(ctx)
    53  		req.URL = r.URL
    54  
    55  		id := u.Id.OpaqueId
    56  		if _, err := s.warmupCacheTracker.Get(id); err != nil {
    57  			p := httptest.NewRecorder()
    58  			_ = s.warmupCacheTracker.Set(id, true)
    59  
    60  			log.Info().Msgf("cache warmup getting created shares for user %s", id)
    61  			req.URL.Path = "/v1.php/apps/files_sharing/api/v1/shares"
    62  			s.router.ServeHTTP(p, req)
    63  
    64  			log.Info().Msgf("cache warmup getting received shares for user %s", id)
    65  			req.URL.Path = "/v1.php/apps/files_sharing/api/v1/shares"
    66  			q := req.URL.Query()
    67  			q.Set("shared_with_me", "true")
    68  			q.Set("state", "all")
    69  			req.URL.RawQuery = q.Encode()
    70  			s.router.ServeHTTP(p, req)
    71  		}
    72  	}
    73  }