...

Source file src/golang.conradwood.net/go-easyops/common/common.go

Documentation: golang.conradwood.net/go-easyops/common

     1  /*
     2  commonly used by other go-easyops packages. May provide useful information on the state of go-easyops.
     3  */
     4  package common
     5  
     6  // this packge MUST NOT add command line variables.
     7  // it is used by server & client to share common state
     8  // no side effects please!
     9  
    10  import (
    11  	"sync"
    12  )
    13  
    14  var (
    15  	myservicenames []string
    16  	clients        []*Service
    17  	blocked        []*BlockedService
    18  	addlock        sync.Mutex
    19  )
    20  
    21  type Service struct {
    22  	Name string
    23  }
    24  
    25  type BlockedService struct {
    26  	Name    string
    27  	Counter int
    28  }
    29  
    30  // add a service name to the list of services being used
    31  func AddServiceName(s string) {
    32  	addlock.Lock()
    33  	defer addlock.Unlock()
    34  	for _, c := range clients {
    35  		if c.Name == s {
    36  			return
    37  		}
    38  	}
    39  	clients = append(clients, &Service{Name: s})
    40  }
    41  
    42  // get all services used
    43  func GetConnectionNames() []*Service {
    44  	return clients
    45  }
    46  
    47  // get services that are used, but are currently blocked because no targets are available
    48  func GetBlockedConnectionNames() []*BlockedService {
    49  	addlock.Lock()
    50  	defer addlock.Unlock()
    51  	var res []*BlockedService
    52  	for _, x := range blocked {
    53  		if x.Counter > 0 {
    54  			res = append(res, x)
    55  		}
    56  	}
    57  	return res
    58  }
    59  
    60  // mark a service as blocked by name
    61  func AddBlockedServiceName(s string) {
    62  	addlock.Lock()
    63  	defer addlock.Unlock()
    64  	for _, x := range blocked {
    65  		if x.Name == s {
    66  			x.Counter++
    67  			return
    68  		}
    69  	}
    70  	blocked = append(blocked, &BlockedService{Name: s, Counter: 1})
    71  }
    72  
    73  // unmark a service as blocked by name
    74  func RemoveBlockedServiceName(s string) {
    75  	addlock.Lock()
    76  	defer addlock.Unlock()
    77  	for _, x := range blocked {
    78  		if x.Name == s {
    79  			x.Counter = 0
    80  			return
    81  		}
    82  	}
    83  }
    84  
    85  // mark a service as exported.
    86  func AddExportedServiceName(name string) {
    87  	addlock.Lock()
    88  	defer addlock.Unlock()
    89  	for _, m := range myservicenames {
    90  		if m == name {
    91  			return
    92  		}
    93  	}
    94  	myservicenames = append(myservicenames, name)
    95  }
    96  

View as plain text