go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/filter/featureBreaker/mc.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 featureBreaker
    16  
    17  import (
    18  	"context"
    19  
    20  	mc "go.chromium.org/luci/gae/service/memcache"
    21  )
    22  
    23  type mcState struct {
    24  	*state
    25  
    26  	c context.Context
    27  	mc.RawInterface
    28  }
    29  
    30  func (m *mcState) GetMulti(keys []string, cb mc.RawItemCB) error {
    31  	if len(keys) == 0 {
    32  		return nil
    33  	}
    34  	return m.run(m.c, func() error { return m.RawInterface.GetMulti(keys, cb) })
    35  }
    36  
    37  func (m *mcState) AddMulti(items []mc.Item, cb mc.RawCB) error {
    38  	if len(items) == 0 {
    39  		return nil
    40  	}
    41  	return m.run(m.c, func() error { return m.RawInterface.AddMulti(items, cb) })
    42  }
    43  
    44  func (m *mcState) SetMulti(items []mc.Item, cb mc.RawCB) error {
    45  	if len(items) == 0 {
    46  		return nil
    47  	}
    48  	return m.run(m.c, func() error { return m.RawInterface.SetMulti(items, cb) })
    49  }
    50  
    51  func (m *mcState) DeleteMulti(keys []string, cb mc.RawCB) error {
    52  	if len(keys) == 0 {
    53  		return nil
    54  	}
    55  	return m.run(m.c, func() error { return m.RawInterface.DeleteMulti(keys, cb) })
    56  }
    57  
    58  func (m *mcState) CompareAndSwapMulti(items []mc.Item, cb mc.RawCB) error {
    59  	if len(items) == 0 {
    60  		return nil
    61  	}
    62  	return m.run(m.c, func() error { return m.RawInterface.CompareAndSwapMulti(items, cb) })
    63  }
    64  
    65  func (m *mcState) Flush() error {
    66  	return m.run(m.c, m.RawInterface.Flush)
    67  }
    68  
    69  func (m *mcState) Stats() (ret *mc.Statistics, err error) {
    70  	err = m.run(m.c, func() (err error) {
    71  		ret, err = m.RawInterface.Stats()
    72  		return
    73  	})
    74  	return
    75  }
    76  
    77  // FilterMC installs a featureBreaker mc filter in the context.
    78  func FilterMC(c context.Context, defaultError error) (context.Context, FeatureBreaker) {
    79  	state := newState(defaultError)
    80  	return mc.AddRawFilters(c, func(ic context.Context, rds mc.RawInterface) mc.RawInterface {
    81  		return &mcState{state, ic, rds}
    82  	}), state
    83  }