github.com/opentofu/opentofu@v1.7.1/internal/providers/schema_cache.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package providers
     7  
     8  import (
     9  	"sync"
    10  
    11  	"github.com/opentofu/opentofu/internal/addrs"
    12  )
    13  
    14  // SchemaCache is a global cache of Schemas.
    15  // This will be accessed by both core and the provider clients to ensure that
    16  // large schemas are stored in a single location.
    17  var SchemaCache = &schemaCache{
    18  	m: make(map[addrs.Provider]ProviderSchema),
    19  }
    20  
    21  // Global cache for provider schemas
    22  // Cache the entire response to ensure we capture any new fields, like
    23  // ServerCapabilities. This also serves to capture errors so that multiple
    24  // concurrent calls resulting in an error can be handled in the same manner.
    25  type schemaCache struct {
    26  	mu sync.Mutex
    27  	m  map[addrs.Provider]ProviderSchema
    28  }
    29  
    30  func (c *schemaCache) Set(p addrs.Provider, s ProviderSchema) {
    31  	c.mu.Lock()
    32  	defer c.mu.Unlock()
    33  
    34  	c.m[p] = s
    35  }
    36  
    37  func (c *schemaCache) Get(p addrs.Provider) (ProviderSchema, bool) {
    38  	c.mu.Lock()
    39  	defer c.mu.Unlock()
    40  
    41  	s, ok := c.m[p]
    42  	return s, ok
    43  }