go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/taskqueue/context.go (about)

     1  // Copyright 2015 The LUCI Authors.
     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  package taskqueue
    16  
    17  import (
    18  	"context"
    19  )
    20  
    21  type key int
    22  
    23  var (
    24  	taskQueueKey       key
    25  	taskQueueFilterKey key = 1
    26  )
    27  
    28  // RawFactory is the function signature for RawFactory methods compatible with
    29  // SetRawFactory.
    30  type RawFactory func(c context.Context) RawInterface
    31  
    32  // RawFilter is the function signature for a RawFilter TQ implementation. It
    33  // gets the current TQ implementation, and returns a new TQ implementation
    34  // backed by the one passed in.
    35  type RawFilter func(context.Context, RawInterface) RawInterface
    36  
    37  // rawUnfiltered gets gets the RawInterface implementation from context without
    38  // any of the filters applied.
    39  func rawUnfiltered(c context.Context) RawInterface {
    40  	if f, ok := c.Value(taskQueueKey).(RawFactory); ok && f != nil {
    41  		return f(c)
    42  	}
    43  	return nil
    44  }
    45  
    46  // rawWithFilters gets the taskqueue (transactional or not), and applies all of
    47  // the currently installed filters to it.
    48  func rawWithFilters(c context.Context, filters ...RawFilter) RawInterface {
    49  	ret := rawUnfiltered(c)
    50  	if ret == nil {
    51  		return nil
    52  	}
    53  	for _, f := range getCurFilters(c) {
    54  		ret = f(c, ret)
    55  	}
    56  	for _, f := range filters {
    57  		ret = f(c, ret)
    58  	}
    59  	return ret
    60  }
    61  
    62  // Raw gets the RawInterface implementation from context.
    63  func Raw(c context.Context) RawInterface {
    64  	return rawWithFilters(c)
    65  }
    66  
    67  // SetRawFactory sets the function to produce RawInterface instances, as returned by
    68  // the GetRaw method.
    69  func SetRawFactory(c context.Context, tqf RawFactory) context.Context {
    70  	return context.WithValue(c, taskQueueKey, tqf)
    71  }
    72  
    73  // SetRaw sets the current RawInterface object in the context. Useful for testing
    74  // with a quick mock. This is just a shorthand SetRawFactory invocation to SetRaw
    75  // a RawFactory which always returns the same object.
    76  func SetRaw(c context.Context, tq RawInterface) context.Context {
    77  	return SetRawFactory(c, func(context.Context) RawInterface { return tq })
    78  }
    79  
    80  func getCurFilters(c context.Context) []RawFilter {
    81  	curFiltsI := c.Value(taskQueueFilterKey)
    82  	if curFiltsI != nil {
    83  		return curFiltsI.([]RawFilter)
    84  	}
    85  	return nil
    86  }
    87  
    88  // AddRawFilters adds RawInterface filters to the context.
    89  func AddRawFilters(c context.Context, filts ...RawFilter) context.Context {
    90  	if len(filts) == 0 {
    91  		return c
    92  	}
    93  	cur := getCurFilters(c)
    94  	newFilts := make([]RawFilter, 0, len(cur)+len(filts))
    95  	newFilts = append(newFilts, getCurFilters(c)...)
    96  	newFilts = append(newFilts, filts...)
    97  	return context.WithValue(c, taskQueueFilterKey, newFilts)
    98  }