go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/encryptedcookies/internal/stores.go (about)

     1  // Copyright 2021 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 internal
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"sync"
    21  
    22  	"go.chromium.org/luci/server/encryptedcookies/session"
    23  	"go.chromium.org/luci/server/module"
    24  )
    25  
    26  var (
    27  	implsM sync.Mutex
    28  	impls  []StoreImpl
    29  )
    30  
    31  // StoreImpl represents a factory for producing session.Store.
    32  type StoreImpl struct {
    33  	ID      string
    34  	Factory func(ctx context.Context, namespace string) (session.Store, error)
    35  	Deps    []module.Dependency
    36  }
    37  
    38  // RegisterStoreImpl registers an available store implementation.
    39  //
    40  // Called during init() time by packages that implements stores.
    41  func RegisterStoreImpl(impl StoreImpl) {
    42  	implsM.Lock()
    43  	defer implsM.Unlock()
    44  	for _, im := range impls {
    45  		if im.ID == impl.ID {
    46  			panic(fmt.Sprintf("session store implementation %q has already been registered", impl.ID))
    47  		}
    48  	}
    49  	impls = append(impls, impl)
    50  }
    51  
    52  // StoreImpls returns registered store implementations.
    53  func StoreImpls() []StoreImpl {
    54  	implsM.Lock()
    55  	defer implsM.Unlock()
    56  	return append([]StoreImpl(nil), impls...)
    57  }