seamless / backends.go

package main

import (

// List of backends, support (thread safe) adding, removing and getting next in list (circular)
type Backends struct {
	backends []string
	current  int
	lock     sync.Mutex

// Set sets the current list of backends
func (bs *Backends) Set(backends []string) {
	defer bs.lock.Unlock()

	bs.backends = backends
	bs.current = 0

// Next returns the next back in circular fashion
func (bs *Backends) Next() (string, error) {
	defer bs.lock.Unlock()

	if len(bs.backends) == 0 {
		return "", fmt.Errorf("empty backends")

	// We advance first to make sure we're in bounds
	bs.current = (bs.current + 1) % len(bs.backends)
	backend := bs.backends[bs.current]
	return backend, nil

// Add adds a new backend
func (bs *Backends) Add(backend string) {
	defer bs.lock.Unlock()

	bs.backends = append(bs.backends, backend)

// Remove removes all occurrences of backend from list of backends, returns the number of items removed
func (bs *Backends) Remove(backend string) int {
	defer bs.lock.Unlock()

	i, count := 0, 0
	for i < len(bs.backends) {
		if bs.backends[i] == backend {
			bs.backends = append(bs.backends[:i], bs.backends[i+1:]...)
		} else {

	return count

// String is string representation of backends
func (bs *Backends) String() string {
	defer bs.lock.Unlock()

	return strings.Join(bs.backends, ",")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.