github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/config/config.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"sync"
    21  
    22  	"k8s.io/kubernetes/pkg/util"
    23  )
    24  
    25  type Merger interface {
    26  	// Invoked when a change from a source is received.  May also function as an incremental
    27  	// merger if you wish to consume changes incrementally.  Must be reentrant when more than
    28  	// one source is defined.
    29  	Merge(source string, update interface{}) error
    30  }
    31  
    32  // MergeFunc implements the Merger interface
    33  type MergeFunc func(source string, update interface{}) error
    34  
    35  func (f MergeFunc) Merge(source string, update interface{}) error {
    36  	return f(source, update)
    37  }
    38  
    39  // Mux is a class for merging configuration from multiple sources.  Changes are
    40  // pushed via channels and sent to the merge function.
    41  type Mux struct {
    42  	// Invoked when an update is sent to a source.
    43  	merger Merger
    44  
    45  	// Sources and their lock.
    46  	sourceLock sync.RWMutex
    47  	// Maps source names to channels
    48  	sources map[string]chan interface{}
    49  }
    50  
    51  // NewMux creates a new mux that can merge changes from multiple sources.
    52  func NewMux(merger Merger) *Mux {
    53  	mux := &Mux{
    54  		sources: make(map[string]chan interface{}),
    55  		merger:  merger,
    56  	}
    57  	return mux
    58  }
    59  
    60  // Channel returns a channel where a configuration source
    61  // can send updates of new configurations. Multiple calls with the same
    62  // source will return the same channel. This allows change and state based sources
    63  // to use the same channel. Different source names however will be treated as a
    64  // union.
    65  func (m *Mux) Channel(source string) chan interface{} {
    66  	if len(source) == 0 {
    67  		panic("Channel given an empty name")
    68  	}
    69  	m.sourceLock.Lock()
    70  	defer m.sourceLock.Unlock()
    71  	channel, exists := m.sources[source]
    72  	if exists {
    73  		return channel
    74  	}
    75  	newChannel := make(chan interface{})
    76  	m.sources[source] = newChannel
    77  	go util.Until(func() { m.listen(source, newChannel) }, 0, util.NeverStop)
    78  	return newChannel
    79  }
    80  
    81  func (m *Mux) listen(source string, listenChannel <-chan interface{}) {
    82  	for update := range listenChannel {
    83  		m.merger.Merge(source, update)
    84  	}
    85  }
    86  
    87  // Accessor is an interface for retrieving the current merge state.
    88  type Accessor interface {
    89  	// MergedState returns a representation of the current merge state.
    90  	// Must be reentrant when more than one source is defined.
    91  	MergedState() interface{}
    92  }
    93  
    94  // AccessorFunc implements the Accessor interface.
    95  type AccessorFunc func() interface{}
    96  
    97  func (f AccessorFunc) MergedState() interface{} {
    98  	return f()
    99  }
   100  
   101  type Listener interface {
   102  	// OnUpdate is invoked when a change is made to an object.
   103  	OnUpdate(instance interface{})
   104  }
   105  
   106  // ListenerFunc receives a representation of the change or object.
   107  type ListenerFunc func(instance interface{})
   108  
   109  func (f ListenerFunc) OnUpdate(instance interface{}) {
   110  	f(instance)
   111  }
   112  
   113  type Broadcaster struct {
   114  	// Listeners for changes and their lock.
   115  	listenerLock sync.RWMutex
   116  	listeners    []Listener
   117  }
   118  
   119  // NewBroadcaster registers a set of listeners that support the Listener interface
   120  // and notifies them all on changes.
   121  func NewBroadcaster() *Broadcaster {
   122  	return &Broadcaster{}
   123  }
   124  
   125  // Add registers listener to receive updates of changes.
   126  func (b *Broadcaster) Add(listener Listener) {
   127  	b.listenerLock.Lock()
   128  	defer b.listenerLock.Unlock()
   129  	b.listeners = append(b.listeners, listener)
   130  }
   131  
   132  // Notify notifies all listeners.
   133  func (b *Broadcaster) Notify(instance interface{}) {
   134  	b.listenerLock.RLock()
   135  	listeners := b.listeners
   136  	b.listenerLock.RUnlock()
   137  	for _, listener := range listeners {
   138  		listener.OnUpdate(instance)
   139  	}
   140  }