func()

in internal/sourcemap/metadata_fetcher.go [215:275]


func (s *MetadataESFetcher) update(ctx context.Context, sourcemaps map[identifier]string) {
	span := apm.TransactionFromContext(ctx).StartSpan("MetadataESFetcher.update", "", nil)
	defer span.End()

	s.mu.Lock()
	defer s.mu.Unlock()

	var invalidation []identifier

	for id, contentHash := range s.set {
		if updatedHash, ok := sourcemaps[id]; ok {
			if contentHash == updatedHash {
				// already in the cache, remove from the updates.
				delete(sourcemaps, id)
			} else {
				// content hash changed, invalidate the sourcemap cache
				s.logger.Debugf("Hash changed: %s -> %s: invalidating %v", contentHash, updatedHash, id)
				invalidation = append(invalidation, id)
			}
		} else {
			// the sourcemap no longer exists in ES.
			// invalidate the sourcemap cache.
			invalidation = append(invalidation, id)

			// the sourcemap no longer exists in ES.
			// remove from metadata cache
			delete(s.set, id)

			// remove aliases
			for _, k := range getAliases(id.name, id.version, id.path) {
				delete(s.alias, k)
			}
		}
	}

	if len(invalidation) != 0 {
		select {
		case s.invalidationChan <- invalidation:
		case <-ctx.Done():
			s.logger.Debug("timed out while invalidating soucemaps")
		}
	}

	// add new sourcemaps to the metadata cache.
	for id, contentHash := range sourcemaps {
		s.set[id] = contentHash
		s.logger.Debugf("Added metadata id %v", id)
		// store aliases with a pointer to the original id.
		// The id is then passed over to the backend fetcher
		// to minimize the size of the lru cache and
		// and increase cache hits.
		for _, k := range getAliases(id.name, id.version, id.path) {
			s.logger.Debugf("Added metadata alias %v -> %v", k, id)
			s.alias[k] = &id
		}
	}

	s.initErr = nil

	s.logger.Debugf("Metadata cache now has %d entries.", len(s.set))
}