github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/daemon/links.go (about)

     1  package daemon
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/docker/docker/container"
     7  )
     8  
     9  // linkIndex stores link relationships between containers, including their specified alias
    10  // The alias is the name the parent uses to reference the child
    11  type linkIndex struct {
    12  	// idx maps a parent->alias->child relationship
    13  	idx map[*container.Container]map[string]*container.Container
    14  	// childIdx maps  child->parent->aliases
    15  	childIdx map[*container.Container]map[*container.Container]map[string]struct{}
    16  	mu       sync.Mutex
    17  }
    18  
    19  func newLinkIndex() *linkIndex {
    20  	return &linkIndex{
    21  		idx:      make(map[*container.Container]map[string]*container.Container),
    22  		childIdx: make(map[*container.Container]map[*container.Container]map[string]struct{}),
    23  	}
    24  }
    25  
    26  // link adds indexes for the passed in parent/child/alias relationships
    27  func (l *linkIndex) link(parent, child *container.Container, alias string) {
    28  	l.mu.Lock()
    29  
    30  	if l.idx[parent] == nil {
    31  		l.idx[parent] = make(map[string]*container.Container)
    32  	}
    33  	l.idx[parent][alias] = child
    34  	if l.childIdx[child] == nil {
    35  		l.childIdx[child] = make(map[*container.Container]map[string]struct{})
    36  	}
    37  	if l.childIdx[child][parent] == nil {
    38  		l.childIdx[child][parent] = make(map[string]struct{})
    39  	}
    40  	l.childIdx[child][parent][alias] = struct{}{}
    41  
    42  	l.mu.Unlock()
    43  }
    44  
    45  // unlink removes the requested alias for the given parent/child
    46  func (l *linkIndex) unlink(alias string, child, parent *container.Container) {
    47  	l.mu.Lock()
    48  	delete(l.idx[parent], alias)
    49  	delete(l.childIdx[child], parent)
    50  	l.mu.Unlock()
    51  }
    52  
    53  // children maps all the aliases-> children for the passed in parent
    54  // aliases here are the aliases the parent uses to refer to the child
    55  func (l *linkIndex) children(parent *container.Container) map[string]*container.Container {
    56  	l.mu.Lock()
    57  	children := l.idx[parent]
    58  	l.mu.Unlock()
    59  	return children
    60  }
    61  
    62  // parents maps all the aliases->parent for the passed in child
    63  // aliases here are the aliases the parents use to refer to the child
    64  func (l *linkIndex) parents(child *container.Container) map[string]*container.Container {
    65  	l.mu.Lock()
    66  
    67  	parents := make(map[string]*container.Container)
    68  	for parent, aliases := range l.childIdx[child] {
    69  		for alias := range aliases {
    70  			parents[alias] = parent
    71  		}
    72  	}
    73  
    74  	l.mu.Unlock()
    75  	return parents
    76  }
    77  
    78  // delete deletes all link relationships referencing this container
    79  func (l *linkIndex) delete(container *container.Container) []string {
    80  	l.mu.Lock()
    81  
    82  	var aliases []string
    83  	for alias, child := range l.idx[container] {
    84  		aliases = append(aliases, alias)
    85  		delete(l.childIdx[child], container)
    86  	}
    87  	delete(l.idx, container)
    88  	delete(l.childIdx, container)
    89  	l.mu.Unlock()
    90  	return aliases
    91  }