go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/datastore/transaction.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 datastore 16 17 import ( 18 "context" 19 ) 20 21 // Transaction is a generic interface used to describe a Datastore transaction. 22 // 23 // The nil Transaction represents no transaction context. 24 // 25 // TODO: Add some functionality here. Ideas include: 26 // - Active() bool: is the transaction currently active? 27 // - AffectedGroups() []*ds.Key: list the groups that have been referenced in 28 // this Transaction so far. 29 type Transaction any 30 31 // WithoutTransaction returns a Context that isn't bound to a transaction. 32 // This may be called even when outside of a transaction, in which case the 33 // input Context is a valid return value. 34 // 35 // This can be useful to perform non-transactional tasks given only a Context 36 // that is bound to a transaction. 37 func WithoutTransaction(c context.Context) context.Context { 38 raw := Raw(c) 39 if t := raw.CurrentTransaction(); t == nil { 40 // If we're not in a transaction, return the input Contxt. 41 return c 42 } 43 return raw.WithoutTransaction() 44 } 45 46 // CurrentTransaction returns a reference to the current Transaction, or nil 47 // if the Context does not have a current Transaction. 48 func CurrentTransaction(c context.Context) Transaction { 49 return Raw(c).CurrentTransaction() 50 }