<commit_before>package connector

import (
	"database/sql"
	"fmt"
	"github.com/billyninja/pgtools/colors"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
	"log"
	"strings"
	"sync"
	"time"
)

type WriteCfg struct {
	AccLimit     int
	FlushTimeout time.Duration
}

type Connector struct {
	DB        *sqlx.DB
	WriteCfg  *WriteCfg
	WriteAcc  []string
	LastFlush time.Time
	FlushLock sync.Mutex
}

func (cn *Connector) CheckFlushTimeout() {
	for {
		//if !cn.FlushLock && (cn.WriteCfg.FlushTimeout > 0*time.Second && time.Since(cn.LastFlush) >= cn.WriteCfg.FlushTimeout) && len(cn.WriteAcc) > 0 {
		if (cn.WriteCfg.FlushTimeout > 0*time.Second && time.Since(cn.LastFlush) >= cn.WriteCfg.FlushTimeout) && len(cn.WriteAcc) > 0 {
			err := cn.FlushNow(true)
			if err != nil {
				log.Printf("<ERRD AT TIMEOUT ENGINE>\n\n%v\n", err)
				continue
			}
		}

		time.Sleep(1 * time.Millisecond)
	}
}

func NewConnector(host, port, user, pass, db string) (*Connector, error) {
	strConn := fmt.Sprintf(
		"dbname=%s user=%s password=%s host=%s port=%s sslmode=disable",
		db,
		user,
		pass,
		host,
		port,
	)
	dbc := sqlx.MustOpen("postgres", strConn)

	wcfg := &WriteCfg{128, 5000 * time.Millisecond}
	cn := &Connector{
		WriteCfg: wcfg,
		WriteAcc: []string{},
		DB:       dbc,
	}
	go func() {
		cn.CheckFlushTimeout()
	}()

	return cn, nil
}

func (conn *Connector) Sel(q string) (*sqlx.Rows, error) {
	var rows *sqlx.Rows
	rows, err := conn.DB.Queryx(q)
	if err != nil {
		log.Println("%v", err)
		log.Println(q)
	}

	return rows, err
}

func (conn *Connector) Insert(q string, flushnow bool) (bool, bool, error) {
	persisted := false
	pos := len(conn.WriteAcc) + 1
	conn.WriteAcc = append(conn.WriteAcc, q)
	var err error
	if flushnow || (conn.WriteCfg.AccLimit > 0 && pos >= conn.WriteCfg.AccLimit) {
		err = conn.FlushNow(false)
		if err == nil {
			persisted = true
		}
	}

	return true, persisted, err
}

func (conn *Connector) DirectInsert(query string) (*sql.Rows, error) {
	rows, err := conn.DB.Query(query)
	if err != nil {
		log.Printf("ERROR: \n\n\n %s \n\n\n", query)
		return nil, err
	}

	return rows, err
}

func (conn *Connector) FlushNow(timeout bool) error {

	conn.FlushLock.Lock()
	defer conn.FlushLock.Unlock()

	trigger := "count"
	if timeout {
		trigger = "timeout"
	}

	acc := make([]string, len(conn.WriteAcc))
	copy(acc, conn.WriteAcc)
	conn.WriteAcc = []string{}

	tq := strings.Join(acc, "; ")
	t1 := time.Now()

	_, err := conn.DB.Exec(tq)
	lat := time.Since(t1)
	if err != nil {
		log.Printf("ERROR: \n\n\n %s \n\n\n", tq)

		log.Printf("Starting Drain down...\n%d queries on the queue\n\n", len(acc))
		for _, q := range acc {
			println(".")
			_, _, err = conn.Insert(q, true)
			if err != nil {
				msg := fmt.Sprintf(" Errd in Drain down: \n\n%s\n\n", err)
				log.Printf(colors.Red(msg))
			}
		}

		return err
	}
	if timeout {
		log.Printf("<%s -%s- %d - s: %d l: %s>\n",
			colors.Green("FLUSHED!"),
			colors.Yellow(trigger),
			len(acc),
			len(tq), lat)
	}

	conn.LastFlush = time.Now()
	return err
}
<commit_msg>including a EXEC shorthand<commit_after>package connector

import (
	"database/sql"
	"fmt"
	"github.com/billyninja/pgtools/colors"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
	"log"
	"strings"
	"sync"
	"time"
)

type WriteCfg struct {
	AccLimit     int
	FlushTimeout time.Duration
}

type Connector struct {
	DB        *sqlx.DB
	WriteCfg  *WriteCfg
	WriteAcc  []string
	LastFlush time.Time
	FlushLock sync.Mutex
}

func (cn *Connector) CheckFlushTimeout() {
	for {
		//if !cn.FlushLock && (cn.WriteCfg.FlushTimeout > 0*time.Second && time.Since(cn.LastFlush) >= cn.WriteCfg.FlushTimeout) && len(cn.WriteAcc) > 0 {
		if (cn.WriteCfg.FlushTimeout > 0*time.Second && time.Since(cn.LastFlush) >= cn.WriteCfg.FlushTimeout) && len(cn.WriteAcc) > 0 {
			err := cn.FlushNow(true)
			if err != nil {
				log.Printf("<ERRD AT TIMEOUT ENGINE>\n\n%v\n", err)
				continue
			}
		}

		time.Sleep(1 * time.Millisecond)
	}
}

func NewConnector(host, port, user, pass, db string) (*Connector, error) {
	strConn := fmt.Sprintf(
		"dbname=%s user=%s password=%s host=%s port=%s sslmode=disable",
		db,
		user,
		pass,
		host,
		port,
	)
	dbc := sqlx.MustOpen("postgres", strConn)

	wcfg := &WriteCfg{128, 5000 * time.Millisecond}
	cn := &Connector{
		WriteCfg: wcfg,
		WriteAcc: []string{},
		DB:       dbc,
	}
	go func() {
		cn.CheckFlushTimeout()
	}()

	return cn, nil
}

func (conn *Connector) Exec(q string) (int64, error) {
	_, err := conn.DB.Exec(q)
	if err != nil {
		log.Println("%v", err)
		log.Println(q)
	}

	return 1, err
}

func (conn *Connector) Sel(q string) (*sqlx.Rows, error) {
	var rows *sqlx.Rows
	rows, err := conn.DB.Queryx(q)
	if err != nil {
		log.Println("%v", err)
		log.Println(q)
	}

	return rows, err
}

func (conn *Connector) Insert(q string, flushnow bool) (bool, bool, error) {
	persisted := false
	pos := len(conn.WriteAcc) + 1
	conn.WriteAcc = append(conn.WriteAcc, q)
	var err error
	if flushnow || (conn.WriteCfg.AccLimit > 0 && pos >= conn.WriteCfg.AccLimit) {
		err = conn.FlushNow(false)
		if err == nil {
			persisted = true
		}
	}

	return true, persisted, err
}

func (conn *Connector) DirectInsert(query string) (*sql.Rows, error) {
	rows, err := conn.DB.Query(query)
	if err != nil {
		log.Printf("ERROR: \n\n\n %s \n\n\n", query)
		return nil, err
	}

	return rows, err
}

func (conn *Connector) FlushNow(timeout bool) error {

	conn.FlushLock.Lock()
	defer conn.FlushLock.Unlock()

	trigger := "count"
	if timeout {
		trigger = "timeout"
	}

	acc := make([]string, len(conn.WriteAcc))
	copy(acc, conn.WriteAcc)
	conn.WriteAcc = []string{}

	tq := strings.Join(acc, "; ")
	t1 := time.Now()

	_, err := conn.DB.Exec(tq)
	lat := time.Since(t1)
	if err != nil {
		log.Printf("ERROR: \n\n\n %s \n\n\n", tq)

		log.Printf("Starting Drain down...\n%d queries on the queue\n\n", len(acc))
		for _, q := range acc {
			println(".")
			_, _, err = conn.Insert(q, true)
			if err != nil {
				msg := fmt.Sprintf(" Errd in Drain down: \n\n%s\n\n", err)
				log.Printf(colors.Red(msg))
			}
		}

		return err
	}
	if timeout {
		log.Printf("<%s -%s- %d - s: %d l: %s>\n",
			colors.Green("FLUSHED!"),
			colors.Yellow(trigger),
			len(acc),
			len(tq), lat)
	}

	conn.LastFlush = time.Now()
	return err
}
<|endoftext|>


<commit_before>package main

import (
	"context"
	"fmt"
	"log"
	"time"
)

func fact(ctx context.Context, num int64) (int64, error) {
	select {
	case <-ctx.Done():
		return 0, ctx.Err()
	default:
	}
	if num < 0 {
		return 0, fmt.Errorf("invalid number %d", num)
	} else if num <= 1 {
		return 1, nil
	}
	f, err := fact(ctx, num-1)
	if err != nil {
		return 0, err
	}
	return num * f, nil
}

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
	defer cancel()
	computed, err := fact(ctx, 20)
	if err != nil {
		log.Fatalf("error (%s)", err)
		return
	}
	log.Println(computed)
}
<commit_msg>add docs<commit_after>package main

import (
	"context"
	"fmt"
	"log"
	"time"
)

// compute the factorial of num. return an error if ctx.Done() receives (i.e. a timeout or
// manual cancellation) or num < 0
func fact(ctx context.Context, num int64) (int64, error) {
	// either context was cancelled or timed out, or we continue.
	// the power of a select!
	select {
	case <-ctx.Done():
		return 0, ctx.Err()
	default:
	}

	// edge cases
	if num < 0 {
		return 0, fmt.Errorf("invalid number %d", num)
	} else if num <= 1 {
		return 1, nil
	}

	// the recursion - note that we pass the same ctx down the stack
	f, err := fact(ctx, num-1)
	if err != nil {
		return 0, err
	}
	return num * f, nil
}

func main() {
	// create the context. a few notes:
	//
	// 1. context.Background() is the "root" of the inheritance
	// 2. context.WithTimeout creates a new context, inherited from context.Background.
	//		it returns the new timeout context and a function that cancels the context manually
	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
	defer cancel()

	computed, err := fact(ctx, 20)
	if err != nil {
		log.Fatalf("error (%s)", err)
		return
	}
	log.Println(computed)
}
<|endoftext|>


<commit_before>package search

import (
	"errors"
	"fmt"
	"math"
	"math/rand"
)

var (
	memo []uint64 = []uint64{1}
)

type permutation struct {
	Seq []uint8
	pos int
}

func (p *permutation) String() string {
	return fmt.Sprint(p.pos, ": ", p.Seq)
}

// Create a permutation of 1...n from an int slice
func NewPerm(p []uint8) *permutation {
	h := hash(p, 0)
	return &permutation{p, int(h)}
}

// Create random permutation of 1...n
func RandPerm(n int) *permutation {
	p := rand.Perm(n)
	r := make([]uint8, len(p))

	for i, v := range p {
		r[i] = uint8(v + 1)
	}

	return NewPerm(r)
}

// DEPRECATED:
// Returns all permutations within a 2-exchange neighborhood
func (p *permutation) Neighborhood() []*permutation {
	n := len(p.Seq)
	perms := make([]*permutation, 0, n*(n-1)/2)

	// Find 2-exchange neighborhood
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			perm := make([]uint8, len(p.Seq))
			copy(perm, p.Seq)
			perm[j], perm[i] = p.Seq[i], p.Seq[j]
			perms = append(perms, NewPerm(perm))
		}
	}

	return perms
}

// Returns the next permutation in a 2-exchange neighborhood of p
func (p *permutation) NextNeighbor() *permutation {
	// Cycle position 1
	p.pos++
	if p.pos == int(fact(uint64(len(p.Seq)))) {
		p.pos = 0
	}

	// Unhash by position
	s := p.Unhash()

	return NewPerm(s)
}

// After extensive research, no efficient algorithm for enumerating all permutations within
// a given Hamming distance could be found. As such, an approximation through sampling is used.
//
// IRRELEVANT: The cardinality for n=13, d=2 is 78. For d=3, it's 1,352 and for d=4, it's 15,093.
// An increase of 1 in the Hamming distance appears to approximately lead to an order of magnitude
// increase of 1 near n=13.
//
// Returns a random permutations within approximate Hamming distance d
func (p *permutation) NextHamming(d int) *permutation {
	if d < 2 {
		panic(errors.New("No permutations have a Hamming distance less than 2"))
		return nil
	}

	s := make([]uint8, len(p.Seq))
	copy(s, p.Seq)

	for d >= 2 {
		if d == 3 {
			s = Exchange3Rand(s)
			d -= 3
		} else {
			s = Exchange2Rand(s)
			d -= 2
		}
	}

	return NewPerm(s)
}

func Exchange2Rand(seq []uint8) []uint8 {
	n := len(seq)
	s := make([]uint8, n)
	copy(s, seq)
	random := rand.Perm(n)

	s[random[0]], s[random[1]] = s[random[1]], s[random[0]]
	return s
}

func Exchange3Rand(seq []uint8) []uint8 {
	n := len(seq)
	s := make([]uint8, n)
	copy(s, seq)
	random := rand.Perm(n)

	s[random[0]], s[random[1]] = s[random[1]], s[random[0]]
	s[random[1]], s[random[2]] = s[random[2]], s[random[1]]
	return s
}

// Hashes a permutation of fixed length n to a number between
// 0 and n!-1 so that a related state may be toggled in a bit
// array.
func (p *permutation) Hash() uint64 {
	return hash(p.Seq, 0)
}

// Strictly speaking, this is not a hashing function since it
// can be easily reversed. Naming conventions for variables are
// atrocious because I wrote it half asleep. I'm not even sure
// how it works, really.
func (p *permutation) Unhash() []uint8 {
	n := len(p.Seq)
	s := make([]uint8, n)

	ints := make([]uint8, n)
	for i, _ := range ints {
		ints[i] = uint8(i + 1)
	}

	hsh := float64(p.pos)
	for i := 0; i < n; i++ {
		fac := fact(uint64(n - 1 - i))
		order := int(math.Floor(hsh / float64(fac)))
		s[i] = ints[order]

		// Keep track of the remaining elements (in order)
		ints = append(ints[:order], ints[order+1:]...)

		// Subtract out contribution to hash
		factor := s[i]
		for j := 0; j < i; j++ {
			if s[j] < s[i] {
				factor--
			}
		}
		factor--

		hsh -= float64(factor) * float64(fac)
	}

	return s
}

func hash(seq []uint8, pos int) uint64 {
	n := len(seq)
	if pos >= n {
		return 0
	}

	s, order := seq[pos], seq[pos]
	for i := 0; i < pos; i++ {
		if seq[i] < s {
			order--
		}
	}

	return uint64(order-1)*fact(uint64(n-1-pos)) + hash(seq, pos+1)
}

func fact(i uint64) uint64 {
	if i >= uint64(len(memo)) {
		memo = append(memo, i*fact(i-1))
	}
	return memo[i]
}
<commit_msg>corrected behavior of NextNeighbor<commit_after>package search

import (
	"errors"
	"fmt"
	"math"
	"math/rand"
)

var (
	memo []uint64 = []uint64{1}
)

type permutation struct {
	Seq    []uint8
	hash   uint64
	length int
	i      int
	j      int
}

func (p *permutation) String() string {
	return fmt.Sprint(p.hash, ": ", p.Seq)
}

// Create a permutation of 1...n from an int slice
func NewPerm(p []uint8) *permutation {
	h := hash(p, 0)
	return &permutation{p, h, len(p), 0, 0}
}

// Create random permutation of 1...n
func RandPerm(n int) *permutation {
	p := rand.Perm(n)
	r := make([]uint8, len(p))

	for i, v := range p {
		r[i] = uint8(v + 1)
	}

	return NewPerm(r)
}

// DEPRECATED:
// Returns all permutations within a 2-exchange neighborhood
func (p *permutation) Neighborhood() []*permutation {
	n := p.length
	perms := make([]*permutation, 0, n*(n-1)/2)

	// Find 2-exchange neighborhood
	for i := 0; i < p.length; i++ {
		for j := i + 1; j < p.length; j++ {
			perm := make([]uint8, p.length)
			copy(perm, p.Seq)
			perm[j], perm[i] = p.Seq[i], p.Seq[j]
			perms = append(perms, NewPerm(perm))
		}
	}

	return perms
}

// Returns the next permutation in a 2-exchange neighborhood of p
func (p *permutation) NextNeighbor() *permutation {
	i := p.i
	j := p.j

	// Cycle position 1
	j++
	if j == p.length {
		i++
		if i == p.length {
			i = 0
		}
		j = 0
	}

	// Perform swaps
	s := make([]uint8, p.length)
	copy(s, p.Seq)
	s[j], s[i] = s[i], s[j]

	// Find new hash
	h := hash(s, 0)

	return &permutation{s, h, len(s), i, j}
}

// After extensive research, no efficient algorithm for enumerating all permutations within
// a given Hamming distance could be found. As such, an approximation through sampling is used.
//
// IRRELEVANT: The cardinality for n=13, d=2 is 78. For d=3, it's 1,352 and for d=4, it's 15,093.
// An increase of 1 in the Hamming distance appears to approximately lead to an order of magnitude
// increase of 1 near n=13.
//
// Returns a random permutations within approximate Hamming distance d
func (p *permutation) NextHamming(d int) *permutation {
	if d < 2 {
		panic(errors.New("No permutations have a Hamming distance less than 2"))
		return nil
	}

	s := make([]uint8, p.length)
	copy(s, p.Seq)

	for d >= 2 {
		if d == 3 {
			s = Exchange3Rand(s)
			d -= 3
		} else {
			s = Exchange2Rand(s)
			d -= 2
		}
	}

	return NewPerm(s)
}

func Exchange2Rand(seq []uint8) []uint8 {
	n := len(seq)
	s := make([]uint8, n)
	copy(s, seq)
	random := rand.Perm(n)

	s[random[0]], s[random[1]] = s[random[1]], s[random[0]]
	return s
}

func Exchange3Rand(seq []uint8) []uint8 {
	n := len(seq)
	s := make([]uint8, n)
	copy(s, seq)
	random := rand.Perm(n)

	s[random[0]], s[random[1]] = s[random[1]], s[random[0]]
	s[random[1]], s[random[2]] = s[random[2]], s[random[1]]
	return s
}

// Hashes a permutation of fixed length n to a number between
// 0 and n!-1 so that a related state may be toggled in a bit
// array.
func (p *permutation) Hash() uint64 {
	// return hash(p.Seq, 0)
	return p.hash
}

// Strictly speaking, this is not a hashing function since it
// can be easily reversed. Naming conventions for variables are
// atrocious because I wrote it half asleep. I'm not even sure
// how it works, really.
func (p *permutation) Unhash() []uint8 {
	s := make([]uint8, p.length)

	ints := make([]uint8, p.length)
	for i, _ := range ints {
		ints[i] = uint8(i + 1)
	}

	hsh := float64(p.hash)
	for i := 0; i < p.length; i++ {
		fac := fact(uint64(p.length - 1 - i))
		order := int(math.Floor(hsh / float64(fac)))
		s[i] = ints[order]

		// Keep track of the remaining elements (in order)
		ints = append(ints[:order], ints[order+1:]...)

		// Subtract out contribution to hash
		factor := s[i]
		for j := 0; j < i; j++ {
			if s[j] < s[i] {
				factor--
			}
		}
		factor--

		hsh -= float64(factor) * float64(fac)
	}

	return s
}

func hash(seq []uint8, pos int) uint64 {
	n := len(seq)
	if pos >= n {
		return 0
	}

	s, order := seq[pos], seq[pos]
	for i := 0; i < pos; i++ {
		if seq[i] < s {
			order--
		}
	}

	return uint64(order-1)*fact(uint64(n-1-pos)) + hash(seq, pos+1)
}

func fact(i uint64) uint64 {
	if i >= uint64(len(memo)) {
		memo = append(memo, i*fact(i-1))
	}
	return memo[i]
}
<|endoftext|>


<commit_before>package tu

import (
	"go/token"
	"sexp"
)

// Package contains information about parsed code.
type Package struct {
	Name string

	Funcs []*Func

	// Vars are sorted in order that should be used
	// during initialization.
	Vars []*Var
}

// Func is a Sexp function.
type Func struct {
	Name   string
	Body   []sexp.Form
	Params []string
}

// Var is a package-level (global) variable.
type Var struct {
	Name string
	Init sexp.Form
}

// TranslatePackage converts Go package into Sexp package.
//
// It parses and typechecks specified package,
// then converts generated objects into our format.
func TranslatePackage(pkgPath string) (*Package, error) {
	fSet := token.NewFileSet()

	parsedPkg, err := parsePackage(fSet, pkgPath)
	if err != nil {
		return nil, err
	}

	checkedPkg, err := typecheckPackage(fSet, parsedPkg)
	if err != nil {
		return nil, err
	}

	return translatePackage(checkedPkg), nil
}
<commit_msg>added 'variadic' bool member for functions<commit_after>package tu

import (
	"go/token"
	"sexp"
)

// Package contains information about parsed code.
type Package struct {
	Name string

	Funcs []*Func

	// Vars are sorted in order that should be used
	// during initialization.
	Vars []*Var
}

// Func is a Sexp function.
type Func struct {
	Name     string
	Body     []sexp.Form
	Params   []string
	Variadic bool
}

// Var is a package-level (global) variable.
type Var struct {
	Name string
	Init sexp.Form
}

// TranslatePackage converts Go package into Sexp package.
//
// It parses and typechecks specified package,
// then converts generated objects into our format.
func TranslatePackage(pkgPath string) (*Package, error) {
	fSet := token.NewFileSet()

	parsedPkg, err := parsePackage(fSet, pkgPath)
	if err != nil {
		return nil, err
	}

	checkedPkg, err := typecheckPackage(fSet, parsedPkg)
	if err != nil {
		return nil, err
	}

	return translatePackage(checkedPkg), nil
}
<|endoftext|>


<commit_before>package security

import (
	"errors"
	"fmt"
	"reflect"
)

// CreatePrincipal returns a Principal that uses 'signer' for all
// private key operations, 'store' for storing blessings bound
// to the Principal and 'roots' for the set of authoritative public
// keys on blessings recognized by this Principal.
//
// If provided 'roots' is nil then the Principal does not trust any
// public keys and all subsequent 'AddToRoots' operations fail.
//
// It returns an error if store.PublicKey does not match signer.PublicKey.
func CreatePrincipal(signer Signer, store BlessingStore, roots BlessingRoots) (Principal, error) {
	if store == nil {
		store = errStore{signer.PublicKey()}
	}
	if roots == nil {
		roots = errRoots{}
	}
	if got, want := store.PublicKey(), signer.PublicKey(); !reflect.DeepEqual(got, want) {
		return nil, fmt.Errorf("store's public key: %v does not match signer's public key: %v", got, want)
	}
	return &principal{signer: signer, store: store, roots: roots}, nil
}

var (
	// Every Signer.Sign operation conducted by the principal fills in a
	// "purpose" before signing to prevent "type attacks" so that a signature obtained
	// for one operation (e.g. Principal.Sign) cannot be re-purposed for another
	// operation (e.g. Principal.Bless). If the "Principal" object lives in an
	// external "agent" process, this works out well since this other process
	// can confidently audit all private key operations.
	// For this to work, ALL private key operations by the Principal must
	// use a distinct "purpose" and no two "purpose"s should share a prefix
	// or suffix.
	blessPurpose     = []byte(SignatureForBlessingCertificates)
	signPurpose      = []byte(SignatureForMessageSigning)
	dischargePurpose = []byte(SignatureForDischarge)

	errNilStore = errors.New("underlying BlessingStore object is nil")
	errNilRoots = errors.New("underlying BlessingRoots object is nil")
)

type errStore struct {
	key PublicKey
}

func (errStore) Set(Blessings, BlessingPattern) (Blessings, error) { return nil, errNilStore }
func (errStore) ForPeer(peerBlessings ...string) Blessings         { return nil }
func (errStore) SetDefault(blessings Blessings) error              { return errNilStore }
func (errStore) Default() Blessings                                { return nil }
func (errStore) DebugString() string                               { return errNilStore.Error() }
func (s errStore) PublicKey() PublicKey                            { return s.key }

type errRoots struct{}

func (errRoots) Add(PublicKey, BlessingPattern) error { return errNilRoots }
func (errRoots) Recognized(PublicKey, string) error   { return errNilRoots }
func (errRoots) DebugString() string                  { return errNilRoots.Error() }

type principal struct {
	signer Signer
	roots  BlessingRoots
	store  BlessingStore
}

func (p *principal) Bless(key PublicKey, with Blessings, extension string, caveat Caveat, additionalCaveats ...Caveat) (Blessings, error) {
	if with == nil {
		return nil, errors.New("the Blessings to bless 'with' must be non-nil")
	}
	if !reflect.DeepEqual(with.PublicKey(), p.PublicKey()) {
		return nil, fmt.Errorf("Principal with public key %v cannot extend blessing with public key %v", p.PublicKey(), with.PublicKey())
	}
	caveats := additionalCaveats
	if !isUnconstrainedUseCaveat(caveat) {
		caveats = append(caveats, caveat)
	}
	cert, err := newUnsignedCertificate(extension, key, caveats...)
	if err != nil {
		return nil, err
	}
	chains := with.certificateChains()
	newchains := make([][]Certificate, len(chains))
	for idx, chain := range chains {
		if err := cert.sign(p.signer, chain[len(chain)-1].Signature); err != nil {
			return nil, err
		}
		newchains[idx] = append(chains[idx], *cert)
	}
	return &blessingsImpl{
		chains:    newchains,
		publicKey: key,
	}, nil
}

func (p *principal) BlessSelf(name string, caveats ...Caveat) (Blessings, error) {
	cert, err := newUnsignedCertificate(name, p.PublicKey(), caveats...)
	if err != nil {
		return nil, err
	}
	if err := cert.sign(p.signer, Signature{}); err != nil {
		return nil, err
	}
	return &blessingsImpl{
		chains:    [][]Certificate{[]Certificate{*cert}},
		publicKey: p.PublicKey(),
	}, nil
}

func (p *principal) Sign(message []byte) (Signature, error) {
	return p.signer.Sign(signPurpose, message)
}

func (p *principal) MintDischarge(tp ThirdPartyCaveat, caveat Caveat, additionalCaveats ...Caveat) (Discharge, error) {
	tpcav, ok := tp.(*publicKeyThirdPartyCaveat)
	if !ok {
		return nil, fmt.Errorf("principal implementation cannot create discharges for caveats of type %T", tp)
	}
	caveats := additionalCaveats
	if !isUnconstrainedUseCaveat(caveat) {
		caveats = append(additionalCaveats, caveat)
	}
	d := &publicKeyDischarge{ThirdPartyCaveatID: tpcav.ID(), Caveats: caveats}
	if err := d.sign(p.signer); err != nil {
		return nil, fmt.Errorf("failed to sign discharge: %v", err)
	}
	return d, nil
}

func (p *principal) PublicKey() PublicKey {
	return p.signer.PublicKey()
}

func (p *principal) BlessingStore() BlessingStore {
	return p.store
}

func (p *principal) Roots() BlessingRoots {
	return p.roots
}

func (p *principal) AddToRoots(blessings Blessings) error {
	if p.roots == nil {
		return errors.New("principal does not have any BlessingRoots")
	}
	chains := blessings.certificateChains()
	glob := ChainSeparator + AllPrincipals
	for _, chain := range chains {
		root, err := UnmarshalPublicKey(chain[0].PublicKey)
		if err != nil {
			return fmt.Errorf("failed to unmarshal public key in root certificate with Extension: %q: %v", chain[0].Extension, err)
		}
		pattern := BlessingPattern(chain[0].Extension) + glob
		if err := p.roots.Add(root, pattern); err != nil {
			return fmt.Errorf("failed to Add root: %v for pattern: %v to this principal's roots: %v", root, pattern, err)
		}
	}
	return nil
}
<commit_msg>"veyron/lib/testutil/security": ForkCredentials<commit_after>package security

import (
	"errors"
	"fmt"
	"reflect"
)

// CreatePrincipal returns a Principal that uses 'signer' for all
// private key operations, 'store' for storing blessings bound
// to the Principal and 'roots' for the set of authoritative public
// keys on blessings recognized by this Principal.
//
// If provided 'roots' is nil then the Principal does not trust any
// public keys and all subsequent 'AddToRoots' operations fail.
//
// It returns an error if store.PublicKey does not match signer.PublicKey.
//
// NOTE: veyron/lib/testutil/security provides utility methods for creating
// principals for testing purposes.
func CreatePrincipal(signer Signer, store BlessingStore, roots BlessingRoots) (Principal, error) {
	if store == nil {
		store = errStore{signer.PublicKey()}
	}
	if roots == nil {
		roots = errRoots{}
	}
	if got, want := store.PublicKey(), signer.PublicKey(); !reflect.DeepEqual(got, want) {
		return nil, fmt.Errorf("store's public key: %v does not match signer's public key: %v", got, want)
	}
	return &principal{signer: signer, store: store, roots: roots}, nil
}

var (
	// Every Signer.Sign operation conducted by the principal fills in a
	// "purpose" before signing to prevent "type attacks" so that a signature obtained
	// for one operation (e.g. Principal.Sign) cannot be re-purposed for another
	// operation (e.g. Principal.Bless). If the "Principal" object lives in an
	// external "agent" process, this works out well since this other process
	// can confidently audit all private key operations.
	// For this to work, ALL private key operations by the Principal must
	// use a distinct "purpose" and no two "purpose"s should share a prefix
	// or suffix.
	blessPurpose     = []byte(SignatureForBlessingCertificates)
	signPurpose      = []byte(SignatureForMessageSigning)
	dischargePurpose = []byte(SignatureForDischarge)

	errNilStore = errors.New("underlying BlessingStore object is nil")
	errNilRoots = errors.New("underlying BlessingRoots object is nil")
)

type errStore struct {
	key PublicKey
}

func (errStore) Set(Blessings, BlessingPattern) (Blessings, error) { return nil, errNilStore }
func (errStore) ForPeer(peerBlessings ...string) Blessings         { return nil }
func (errStore) SetDefault(blessings Blessings) error              { return errNilStore }
func (errStore) Default() Blessings                                { return nil }
func (errStore) DebugString() string                               { return errNilStore.Error() }
func (s errStore) PublicKey() PublicKey                            { return s.key }

type errRoots struct{}

func (errRoots) Add(PublicKey, BlessingPattern) error { return errNilRoots }
func (errRoots) Recognized(PublicKey, string) error   { return errNilRoots }
func (errRoots) DebugString() string                  { return errNilRoots.Error() }

type principal struct {
	signer Signer
	roots  BlessingRoots
	store  BlessingStore
}

func (p *principal) Bless(key PublicKey, with Blessings, extension string, caveat Caveat, additionalCaveats ...Caveat) (Blessings, error) {
	if with == nil {
		return nil, errors.New("the Blessings to bless 'with' must be non-nil")
	}
	if !reflect.DeepEqual(with.PublicKey(), p.PublicKey()) {
		return nil, fmt.Errorf("Principal with public key %v cannot extend blessing with public key %v", p.PublicKey(), with.PublicKey())
	}
	caveats := additionalCaveats
	if !isUnconstrainedUseCaveat(caveat) {
		caveats = append(caveats, caveat)
	}
	cert, err := newUnsignedCertificate(extension, key, caveats...)
	if err != nil {
		return nil, err
	}
	chains := with.certificateChains()
	newchains := make([][]Certificate, len(chains))
	for idx, chain := range chains {
		if err := cert.sign(p.signer, chain[len(chain)-1].Signature); err != nil {
			return nil, err
		}
		newchains[idx] = append(chains[idx], *cert)
	}
	return &blessingsImpl{
		chains:    newchains,
		publicKey: key,
	}, nil
}

func (p *principal) BlessSelf(name string, caveats ...Caveat) (Blessings, error) {
	cert, err := newUnsignedCertificate(name, p.PublicKey(), caveats...)
	if err != nil {
		return nil, err
	}
	if err := cert.sign(p.signer, Signature{}); err != nil {
		return nil, err
	}
	return &blessingsImpl{
		chains:    [][]Certificate{[]Certificate{*cert}},
		publicKey: p.PublicKey(),
	}, nil
}

func (p *principal) Sign(message []byte) (Signature, error) {
	return p.signer.Sign(signPurpose, message)
}

func (p *principal) MintDischarge(tp ThirdPartyCaveat, caveat Caveat, additionalCaveats ...Caveat) (Discharge, error) {
	tpcav, ok := tp.(*publicKeyThirdPartyCaveat)
	if !ok {
		return nil, fmt.Errorf("principal implementation cannot create discharges for caveats of type %T", tp)
	}
	caveats := additionalCaveats
	if !isUnconstrainedUseCaveat(caveat) {
		caveats = append(additionalCaveats, caveat)
	}
	d := &publicKeyDischarge{ThirdPartyCaveatID: tpcav.ID(), Caveats: caveats}
	if err := d.sign(p.signer); err != nil {
		return nil, fmt.Errorf("failed to sign discharge: %v", err)
	}
	return d, nil
}

func (p *principal) PublicKey() PublicKey {
	return p.signer.PublicKey()
}

func (p *principal) BlessingStore() BlessingStore {
	return p.store
}

func (p *principal) Roots() BlessingRoots {
	return p.roots
}

func (p *principal) AddToRoots(blessings Blessings) error {
	if p.roots == nil {
		return errors.New("principal does not have any BlessingRoots")
	}
	chains := blessings.certificateChains()
	glob := ChainSeparator + AllPrincipals
	for _, chain := range chains {
		root, err := UnmarshalPublicKey(chain[0].PublicKey)
		if err != nil {
			return fmt.Errorf("failed to unmarshal public key in root certificate with Extension: %q: %v", chain[0].Extension, err)
		}
		pattern := BlessingPattern(chain[0].Extension) + glob
		if err := p.roots.Add(root, pattern); err != nil {
			return fmt.Errorf("failed to Add root: %v for pattern: %v to this principal's roots: %v", root, pattern, err)
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package kubeadmission

import (
	"k8s.io/apimachinery/pkg/util/sets"
	"k8s.io/apiserver/pkg/admission"
	mutatingwebhook "k8s.io/apiserver/pkg/admission/plugin/webhook/mutating"

	"github.com/openshift/origin/pkg/admission/customresourcevalidation/customresourcevalidationregistration"
	authorizationrestrictusers "github.com/openshift/origin/pkg/authorization/apiserver/admission/restrictusers"
	quotaclusterresourceoverride "github.com/openshift/origin/pkg/autoscaling/admission/clusterresourceoverride"
	quotarunonceduration "github.com/openshift/origin/pkg/autoscaling/admission/runonceduration"
	imagepolicyapiv1 "github.com/openshift/origin/pkg/image/apiserver/admission/apis/imagepolicy/v1"
	"github.com/openshift/origin/pkg/image/apiserver/admission/imagepolicy"
	"github.com/openshift/origin/pkg/network/admission/externalipranger"
	"github.com/openshift/origin/pkg/network/admission/restrictedendpoints"
	quotaclusterresourcequota "github.com/openshift/origin/pkg/quota/apiserver/admission/clusterresourcequota"
	ingressadmission "github.com/openshift/origin/pkg/route/apiserver/admission"
	projectnodeenv "github.com/openshift/origin/pkg/scheduler/admission/nodeenv"
	schedulerpodnodeconstraints "github.com/openshift/origin/pkg/scheduler/admission/podnodeconstraints"
	securityadmission "github.com/openshift/origin/pkg/security/apiserver/admission/sccadmission"
)

func RegisterOpenshiftKubeAdmissionPlugins(plugins *admission.Plugins) {
	authorizationrestrictusers.Register(plugins)
	imagepolicy.Register(plugins)
	ingressadmission.Register(plugins)
	projectnodeenv.Register(plugins)
	quotaclusterresourceoverride.Register(plugins)
	quotaclusterresourcequota.Register(plugins)
	quotarunonceduration.Register(plugins)
	schedulerpodnodeconstraints.Register(plugins)
	securityadmission.Register(plugins)
	securityadmission.RegisterSCCExecRestrictions(plugins)
	externalipranger.RegisterExternalIP(plugins)
	restrictedendpoints.RegisterRestrictedEndpoints(plugins)
}

var (

	// these are admission plugins that cannot be applied until after the kubeapiserver starts.
	// TODO if nothing comes to mind in 3.10, kill this
	SkipRunLevelZeroPlugins = sets.NewString()
	// these are admission plugins that cannot be applied until after the openshiftapiserver apiserver starts.
	SkipRunLevelOnePlugins = sets.NewString(
		"authorization.openshift.io/RestrictSubjectBindings",
		imagepolicyapiv1.PluginName, // "image.openshift.io/ImagePolicy"
		"quota.openshift.io/ClusterResourceQuota",
		"security.openshift.io/SecurityContextConstraint",
		"security.openshift.io/SCCExecRestrictions",
	)

	// AfterKubeAdmissionPlugins are the admission plugins to add after kube admission, before mutating webhooks
	openshiftAdmissionPluginsForKube = []string{
		"autoscaling.openshift.io/ClusterResourceOverride",
		"authorization.openshift.io/RestrictSubjectBindings",
		"autoscaling.openshift.io/RunOnceDuration",
		"scheduling.openshift.io/PodNodeConstraints",
		"scheduling.openshift.io/OriginPodNodeEnvironment",
		"network.openshift.io/ExternalIPRanger",
		"network.openshift.io/RestrictedEndpointsAdmission",
		imagepolicyapiv1.PluginName, // "image.openshift.io/ImagePolicy"
		"security.openshift.io/SecurityContextConstraint",
		"security.openshift.io/SCCExecRestrictions",
		"route.openshift.io/IngressAdmission",
		"quota.openshift.io/ClusterResourceQuota",
	}

	// additionalDefaultOnPlugins is a list of plugins we turn on by default that core kube does not.
	additionalDefaultOnPlugins = sets.NewString(
		"NodeRestriction",
		"OwnerReferencesPermissionEnforcement",
		"PersistentVolumeLabel",
		"PodNodeSelector",
		"PodTolerationRestriction",
		"Priority",
		imagepolicyapiv1.PluginName, // "image.openshift.io/ImagePolicy"
		"StorageObjectInUseProtection",
	)
)

func NewOrderedKubeAdmissionPlugins(kubeAdmissionOrder []string) []string {
	ret := []string{}
	for _, curr := range kubeAdmissionOrder {
		if curr == mutatingwebhook.PluginName {
			ret = append(ret, openshiftAdmissionPluginsForKube...)
			ret = append(ret, customresourcevalidationregistration.AllCustomResourceValidators...)
		}
		ret = append(ret, curr)
	}
	return ret
}

func NewDefaultOffPluginsFunc(kubeDefaultOffAdmission sets.String) func() sets.String {
	return func() sets.String {
		kubeOff := sets.NewString(kubeDefaultOffAdmission.UnsortedList()...)
		kubeOff.Delete(additionalDefaultOnPlugins.List()...)
		kubeOff.Delete(openshiftAdmissionPluginsForKube...)
		return kubeOff
	}
}
<commit_msg>kube-apiserver: delete CRD validation from default off plugin list<commit_after>package kubeadmission

import (
	"k8s.io/apimachinery/pkg/util/sets"
	"k8s.io/apiserver/pkg/admission"
	mutatingwebhook "k8s.io/apiserver/pkg/admission/plugin/webhook/mutating"

	"github.com/openshift/origin/pkg/admission/customresourcevalidation/customresourcevalidationregistration"
	authorizationrestrictusers "github.com/openshift/origin/pkg/authorization/apiserver/admission/restrictusers"
	quotaclusterresourceoverride "github.com/openshift/origin/pkg/autoscaling/admission/clusterresourceoverride"
	quotarunonceduration "github.com/openshift/origin/pkg/autoscaling/admission/runonceduration"
	imagepolicyapiv1 "github.com/openshift/origin/pkg/image/apiserver/admission/apis/imagepolicy/v1"
	"github.com/openshift/origin/pkg/image/apiserver/admission/imagepolicy"
	"github.com/openshift/origin/pkg/network/admission/externalipranger"
	"github.com/openshift/origin/pkg/network/admission/restrictedendpoints"
	quotaclusterresourcequota "github.com/openshift/origin/pkg/quota/apiserver/admission/clusterresourcequota"
	ingressadmission "github.com/openshift/origin/pkg/route/apiserver/admission"
	projectnodeenv "github.com/openshift/origin/pkg/scheduler/admission/nodeenv"
	schedulerpodnodeconstraints "github.com/openshift/origin/pkg/scheduler/admission/podnodeconstraints"
	securityadmission "github.com/openshift/origin/pkg/security/apiserver/admission/sccadmission"
)

func RegisterOpenshiftKubeAdmissionPlugins(plugins *admission.Plugins) {
	authorizationrestrictusers.Register(plugins)
	imagepolicy.Register(plugins)
	ingressadmission.Register(plugins)
	projectnodeenv.Register(plugins)
	quotaclusterresourceoverride.Register(plugins)
	quotaclusterresourcequota.Register(plugins)
	quotarunonceduration.Register(plugins)
	schedulerpodnodeconstraints.Register(plugins)
	securityadmission.Register(plugins)
	securityadmission.RegisterSCCExecRestrictions(plugins)
	externalipranger.RegisterExternalIP(plugins)
	restrictedendpoints.RegisterRestrictedEndpoints(plugins)
}

var (

	// these are admission plugins that cannot be applied until after the kubeapiserver starts.
	// TODO if nothing comes to mind in 3.10, kill this
	SkipRunLevelZeroPlugins = sets.NewString()
	// these are admission plugins that cannot be applied until after the openshiftapiserver apiserver starts.
	SkipRunLevelOnePlugins = sets.NewString(
		"authorization.openshift.io/RestrictSubjectBindings",
		imagepolicyapiv1.PluginName, // "image.openshift.io/ImagePolicy"
		"quota.openshift.io/ClusterResourceQuota",
		"security.openshift.io/SecurityContextConstraint",
		"security.openshift.io/SCCExecRestrictions",
	)

	// AfterKubeAdmissionPlugins are the admission plugins to add after kube admission, before mutating webhooks
	openshiftAdmissionPluginsForKube = []string{
		"autoscaling.openshift.io/ClusterResourceOverride",
		"authorization.openshift.io/RestrictSubjectBindings",
		"autoscaling.openshift.io/RunOnceDuration",
		"scheduling.openshift.io/PodNodeConstraints",
		"scheduling.openshift.io/OriginPodNodeEnvironment",
		"network.openshift.io/ExternalIPRanger",
		"network.openshift.io/RestrictedEndpointsAdmission",
		imagepolicyapiv1.PluginName, // "image.openshift.io/ImagePolicy"
		"security.openshift.io/SecurityContextConstraint",
		"security.openshift.io/SCCExecRestrictions",
		"route.openshift.io/IngressAdmission",
		"quota.openshift.io/ClusterResourceQuota",
	}

	// additionalDefaultOnPlugins is a list of plugins we turn on by default that core kube does not.
	additionalDefaultOnPlugins = sets.NewString(
		"NodeRestriction",
		"OwnerReferencesPermissionEnforcement",
		"PersistentVolumeLabel",
		"PodNodeSelector",
		"PodTolerationRestriction",
		"Priority",
		imagepolicyapiv1.PluginName, // "image.openshift.io/ImagePolicy"
		"StorageObjectInUseProtection",
	)
)

func NewOrderedKubeAdmissionPlugins(kubeAdmissionOrder []string) []string {
	ret := []string{}
	for _, curr := range kubeAdmissionOrder {
		if curr == mutatingwebhook.PluginName {
			ret = append(ret, openshiftAdmissionPluginsForKube...)
			ret = append(ret, customresourcevalidationregistration.AllCustomResourceValidators...)
		}
		ret = append(ret, curr)
	}
	return ret
}

func NewDefaultOffPluginsFunc(kubeDefaultOffAdmission sets.String) func() sets.String {
	return func() sets.String {
		kubeOff := sets.NewString(kubeDefaultOffAdmission.UnsortedList()...)
		kubeOff.Delete(additionalDefaultOnPlugins.List()...)
		kubeOff.Delete(openshiftAdmissionPluginsForKube...)
		kubeOff.Delete(customresourcevalidationregistration.AllCustomResourceValidators...)
		return kubeOff
	}
}
<|endoftext|>


<commit_before>package main

import (
	"net"
	"os"

	log "github.com/Sirupsen/logrus"
	"google.golang.org/grpc"

	_ "lib/logger"
	pb "lib/proto/auth"
	// sp "lib/services"
	_ "lib/statsd-pprof"
)

const (
	port = ":50006"
)

func main() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Panic(err)
		os.Exit(-1)
	}
	log.Info("listening on:", lis.Addr())

	// sp.Init("snowflake")

	// 注册服务
	s := grpc.NewServer()
	ins := &server{}
	ins.init()
	pb.RegisterAuthServiceServer(s, ins)

	// 开始服务
	s.Serve(lis)
}
<commit_msg>update auth<commit_after>package main

import (
	"net"
	"os"

	log "github.com/Sirupsen/logrus"
	"google.golang.org/grpc"

	_ "lib/logger"
	pb "lib/proto/auth"
	sp "lib/services"
	_ "lib/statsd-pprof"
)

const (
	port = ":50006"
)

func main() {
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Panic(err)
		os.Exit(-1)
	}
	log.Info("listening on:", lis.Addr())

	sp.Init("snowflake")

	// 注册服务
	s := grpc.NewServer()
	ins := &server{}
	ins.init()
	pb.RegisterAuthServiceServer(s, ins)

	// 开始服务
	s.Serve(lis)
}
<|endoftext|>


<commit_before>package otp

import (
	"crypto/sha1"
	"testing"
)

func TestGetInterval(t *testing.T) {
	iv := GetInterval(30)
	if iv < 0 {
		t.Error("Time interval is negative.")
	}
}

func TestGetCode(t *testing.T) {
	// test cases borrowed from the excellent Python project:
	// https://github.com/tadeck/onetimepass
	code, err := GetCode("MFRGGZDFMZTWQ2LK", 1, sha1.New, 6)
	if err != nil || code != "765705" {
		t.Error("Code did not match for first interval.")
	}

	code, err = GetCode("MFRGGZDFMZTWQ2LK", 2, sha1.New, 6)
	if err != nil || code != "816065" {
		t.Error("Code did not match for second interval.")
	}
}

func TestBadSecretInGetCode(t *testing.T) {
	_, err := GetCode("abc123", 1, sha1.New, 6)
	if err == nil {
		t.Error("Decoding worked for bad base32.")
	}
}

func TestShortDigits(t *testing.T) {
	_, _ = GetCode("MFRGGZDFMZTWQ2LK", 19, sha1.New, 6)
}
<commit_msg>check len<commit_after>package otp

import (
	"crypto/sha1"
	"testing"
)

func TestGetInterval(t *testing.T) {
	iv := GetInterval(30)
	if iv < 0 {
		t.Error("Time interval is negative.")
	}
}

func TestGetCode(t *testing.T) {
	// test cases borrowed from the excellent Python project:
	// https://github.com/tadeck/onetimepass
	code, err := GetCode("MFRGGZDFMZTWQ2LK", 1, sha1.New, 6)
	if err != nil || code != "765705" {
		t.Error("Code did not match for first interval.")
	}

	code, err = GetCode("MFRGGZDFMZTWQ2LK", 2, sha1.New, 6)
	if err != nil || code != "816065" {
		t.Error("Code did not match for second interval.")
	}
}

func TestBadSecretInGetCode(t *testing.T) {
	_, err := GetCode("abc123", 1, sha1.New, 6)
	if err == nil {
		t.Error("Decoding worked for bad base32.")
	}
}

func TestShortDigits(t *testing.T) {
	code, _ := GetCode("MFRGGZDFMZTWQ2LK", 19, sha1.New, 6)
	if len(code) != 6 {
		t.Fail()
	}
}
<|endoftext|>


<commit_before>// Copyright 2017, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package stats

import (
	"context"
	"errors"
	"fmt"
	"time"

	"go.opencensus.io/tag"
)

func init() {
	defaultWorker = newWorker()
	go defaultWorker.start()
}

type worker struct {
	measures   map[string]Measure
	views      map[string]*View
	startTimes map[*View]time.Time

	timer      *time.Ticker
	c          chan command
	quit, done chan bool
}

var defaultWorker *worker

var defaultReportingDuration = 10 * time.Second

// NewMeasureFloat64 creates a new measure of type MeasureFloat64. It returns
// an error if a measure with the same name already exists.
func NewMeasureFloat64(name, description, unit string) (*MeasureFloat64, error) {
	if err := checkMeasureName(name); err != nil {
		return nil, err
	}
	m := &MeasureFloat64{
		name:        name,
		description: description,
		unit:        unit,
		views:       make(map[*View]bool),
	}

	req := &registerMeasureReq{
		m:   m,
		err: make(chan error),
	}
	defaultWorker.c <- req
	if err := <-req.err; err != nil {
		return nil, err
	}

	return m, nil
}

// NewMeasureInt64 creates a new measure of type MeasureInt64. It returns an
// error if a measure with the same name already exists.
func NewMeasureInt64(name, description, unit string) (*MeasureInt64, error) {
	if err := checkMeasureName(name); err != nil {
		return nil, err
	}
	m := &MeasureInt64{
		name:        name,
		description: description,
		unit:        unit,
		views:       make(map[*View]bool),
	}

	req := &registerMeasureReq{
		m:   m,
		err: make(chan error),
	}
	defaultWorker.c <- req
	if err := <-req.err; err != nil {
		return nil, err
	}

	return m, nil
}

// FindView returns a registered view associated with this name.
// If no registered view is found, nil is returned.
func FindView(name string) (v *View) {
	req := &getViewByNameReq{
		name: name,
		c:    make(chan *getViewByNameResp),
	}
	defaultWorker.c <- req
	resp := <-req.c
	return resp.v
}

// RegisterView registers view. It returns an error if the view is already registered.
//
// Subscription automatically registers a view.
// Most users will not register directly but register via subscription.
// Registeration can be used by libraries to claim a view name.
//
// Unregister the view once the view is not required anymore.
func RegisterView(v *View) error {
	req := &registerViewReq{
		v:   v,
		err: make(chan error),
	}
	defaultWorker.c <- req
	return <-req.err
}

// Unregister removes the previously registered view. It returns an error
// if the view wasn't registered. All data collected and not reported for the
// corresponding view will be lost. The view is automatically be unsubscribed.
func UnregisterView(v *View) error {
	req := &unregisterViewReq{
		v:   v,
		err: make(chan error),
	}
	defaultWorker.c <- req
	return <-req.err
}

// Subscribe subscribes a view. Once a view is subscribed, it reports data
// via the exporters.
// During subscription, if the view wasn't registered, it will be automatically
// registered. Once the view is no longer needed to export data,
// user should unsubscribe from the view.
func (v *View) Subscribe() error {
	req := &subscribeToViewReq{
		v:   v,
		err: make(chan error),
	}
	defaultWorker.c <- req
	return <-req.err
}

// Unsubscribe unsubscribes a previously subscribed channel.
// Data will not be exported from this view once unsubscription happens.
// If no more subscriber for v exists and the the ad hoc
// collection for this view isn't active, data stops being collected for this
// view.
func (v *View) Unsubscribe() error {
	req := &unsubscribeFromViewReq{
		v:   v,
		err: make(chan error),
	}
	defaultWorker.c <- req
	return <-req.err
}

// RetrieveData returns the current collected data for the view.
func (v *View) RetrieveData() ([]*Row, error) {
	if v == nil {
		return nil, errors.New("cannot retrieve data from nil view")
	}
	req := &retrieveDataReq{
		now: time.Now(),
		v:   v,
		c:   make(chan *retrieveDataResp),
	}
	defaultWorker.c <- req
	resp := <-req.c
	return resp.rows, resp.err
}

// Record records one or multiple measurements with the same tags at once.
// If there are any tags in the context, measurements will be tagged with them.
func Record(ctx context.Context, ms ...Measurement) {
	req := &recordReq{
		now: time.Now(),
		tm:  tag.FromContext(ctx),
		ms:  ms,
	}
	defaultWorker.c <- req
}

// SetReportingPeriod sets the interval between reporting aggregated views in
// the program. If duration is less than or
// equal to zero, it enables the default behavior.
func SetReportingPeriod(d time.Duration) {
	// TODO(acetechnologist): ensure that the duration d is more than a certain
	// value. e.g. 1s
	req := &setReportingPeriodReq{
		d: d,
		c: make(chan bool),
	}
	defaultWorker.c <- req
	<-req.c // don't return until the timer is set to the new duration.
}

func newWorker() *worker {
	return &worker{
		measures:   make(map[string]Measure),
		views:      make(map[string]*View),
		startTimes: make(map[*View]time.Time),
		timer:      time.NewTicker(defaultReportingDuration),
		c:          make(chan command),
		quit:       make(chan bool),
		done:       make(chan bool),
	}
}

func (w *worker) start() {
	for {
		select {
		case cmd := <-w.c:
			if cmd != nil {
				cmd.handleCommand(w)
			}
		case <-w.timer.C:
			w.reportUsage(time.Now())
		case <-w.quit:
			w.timer.Stop()
			close(w.c)
			w.done <- true
			return
		}
	}
}

func (w *worker) stop() {
	w.quit <- true
	<-w.done
}

func (w *worker) tryRegisterMeasure(m Measure) error {
	if x, ok := w.measures[m.Name()]; ok {
		if x != m {
			return fmt.Errorf("cannot register measure %q; another measure with the same name is already registered", m.Name())
		}
		// the measure is already registered so there is nothing to do and the
		// command is considered successful.
		return nil
	}

	w.measures[m.Name()] = m
	return nil
}

func (w *worker) tryRegisterView(v *View) error {
	if x, ok := w.views[v.Name()]; ok {
		if x != v {
			return fmt.Errorf("cannot register view %q; another view with the same name is already registered", v.Name())
		}

		// the view is already registered so there is nothing to do and the
		// command is considered successful.
		return nil
	}

	// view is not registered and needs to be registered, but first its measure
	// needs to be registered.
	if err := w.tryRegisterMeasure(v.Measure()); err != nil {
		return fmt.Errorf("cannot register view %q: %v", v.Name(), err)
	}

	w.views[v.Name()] = v

	v.Measure().addView(v)
	return nil
}

func (w *worker) reportUsage(start time.Time) {
	for _, v := range w.views {
		if !v.isSubscribed() {
			continue
		}
		rows := v.collectedRows(start)
		if isCumulative(v) {
			s, ok := w.startTimes[v]
			if !ok {
				w.startTimes[v] = start
			} else {
				start = s
			}
		}
		viewData := &ViewData{
			View:  v,
			Start: start,
			End:   time.Now(),
			Rows:  rows,
		}
		exportersMu.Lock()
		for e := range exporters {
			e.Export(viewData)
		}
		exportersMu.Unlock()
		if !isCumulative(v) {
			v.clearRows()
		}
	}
}

func isCumulative(v *View) bool {
	switch v.Window().(type) {
	case *Cumulative:
		return true
	case Cumulative:
		return true
	}
	return false
}
<commit_msg>Fix godoc for UnregisterView (#253)<commit_after>// Copyright 2017, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package stats

import (
	"context"
	"errors"
	"fmt"
	"time"

	"go.opencensus.io/tag"
)

func init() {
	defaultWorker = newWorker()
	go defaultWorker.start()
}

type worker struct {
	measures   map[string]Measure
	views      map[string]*View
	startTimes map[*View]time.Time

	timer      *time.Ticker
	c          chan command
	quit, done chan bool
}

var defaultWorker *worker

var defaultReportingDuration = 10 * time.Second

// NewMeasureFloat64 creates a new measure of type MeasureFloat64. It returns
// an error if a measure with the same name already exists.
func NewMeasureFloat64(name, description, unit string) (*MeasureFloat64, error) {
	if err := checkMeasureName(name); err != nil {
		return nil, err
	}
	m := &MeasureFloat64{
		name:        name,
		description: description,
		unit:        unit,
		views:       make(map[*View]bool),
	}

	req := &registerMeasureReq{
		m:   m,
		err: make(chan error),
	}
	defaultWorker.c <- req
	if err := <-req.err; err != nil {
		return nil, err
	}

	return m, nil
}

// NewMeasureInt64 creates a new measure of type MeasureInt64. It returns an
// error if a measure with the same name already exists.
func NewMeasureInt64(name, description, unit string) (*MeasureInt64, error) {
	if err := checkMeasureName(name); err != nil {
		return nil, err
	}
	m := &MeasureInt64{
		name:        name,
		description: description,
		unit:        unit,
		views:       make(map[*View]bool),
	}

	req := &registerMeasureReq{
		m:   m,
		err: make(chan error),
	}
	defaultWorker.c <- req
	if err := <-req.err; err != nil {
		return nil, err
	}

	return m, nil
}

// FindView returns a registered view associated with this name.
// If no registered view is found, nil is returned.
func FindView(name string) (v *View) {
	req := &getViewByNameReq{
		name: name,
		c:    make(chan *getViewByNameResp),
	}
	defaultWorker.c <- req
	resp := <-req.c
	return resp.v
}

// RegisterView registers view. It returns an error if the view is already registered.
//
// Subscription automatically registers a view.
// Most users will not register directly but register via subscription.
// Registeration can be used by libraries to claim a view name.
//
// Unregister the view once the view is not required anymore.
func RegisterView(v *View) error {
	req := &registerViewReq{
		v:   v,
		err: make(chan error),
	}
	defaultWorker.c <- req
	return <-req.err
}

// UnregisterView removes the previously registered view. It returns an error
// if the view wasn't registered. All data collected and not reported for the
// corresponding view will be lost. The view is automatically be unsubscribed.
func UnregisterView(v *View) error {
	req := &unregisterViewReq{
		v:   v,
		err: make(chan error),
	}
	defaultWorker.c <- req
	return <-req.err
}

// Subscribe subscribes a view. Once a view is subscribed, it reports data
// via the exporters.
// During subscription, if the view wasn't registered, it will be automatically
// registered. Once the view is no longer needed to export data,
// user should unsubscribe from the view.
func (v *View) Subscribe() error {
	req := &subscribeToViewReq{
		v:   v,
		err: make(chan error),
	}
	defaultWorker.c <- req
	return <-req.err
}

// Unsubscribe unsubscribes a previously subscribed channel.
// Data will not be exported from this view once unsubscription happens.
// If no more subscriber for v exists and the the ad hoc
// collection for this view isn't active, data stops being collected for this
// view.
func (v *View) Unsubscribe() error {
	req := &unsubscribeFromViewReq{
		v:   v,
		err: make(chan error),
	}
	defaultWorker.c <- req
	return <-req.err
}

// RetrieveData returns the current collected data for the view.
func (v *View) RetrieveData() ([]*Row, error) {
	if v == nil {
		return nil, errors.New("cannot retrieve data from nil view")
	}
	req := &retrieveDataReq{
		now: time.Now(),
		v:   v,
		c:   make(chan *retrieveDataResp),
	}
	defaultWorker.c <- req
	resp := <-req.c
	return resp.rows, resp.err
}

// Record records one or multiple measurements with the same tags at once.
// If there are any tags in the context, measurements will be tagged with them.
func Record(ctx context.Context, ms ...Measurement) {
	req := &recordReq{
		now: time.Now(),
		tm:  tag.FromContext(ctx),
		ms:  ms,
	}
	defaultWorker.c <- req
}

// SetReportingPeriod sets the interval between reporting aggregated views in
// the program. If duration is less than or
// equal to zero, it enables the default behavior.
func SetReportingPeriod(d time.Duration) {
	// TODO(acetechnologist): ensure that the duration d is more than a certain
	// value. e.g. 1s
	req := &setReportingPeriodReq{
		d: d,
		c: make(chan bool),
	}
	defaultWorker.c <- req
	<-req.c // don't return until the timer is set to the new duration.
}

func newWorker() *worker {
	return &worker{
		measures:   make(map[string]Measure),
		views:      make(map[string]*View),
		startTimes: make(map[*View]time.Time),
		timer:      time.NewTicker(defaultReportingDuration),
		c:          make(chan command),
		quit:       make(chan bool),
		done:       make(chan bool),
	}
}

func (w *worker) start() {
	for {
		select {
		case cmd := <-w.c:
			if cmd != nil {
				cmd.handleCommand(w)
			}
		case <-w.timer.C:
			w.reportUsage(time.Now())
		case <-w.quit:
			w.timer.Stop()
			close(w.c)
			w.done <- true
			return
		}
	}
}

func (w *worker) stop() {
	w.quit <- true
	<-w.done
}

func (w *worker) tryRegisterMeasure(m Measure) error {
	if x, ok := w.measures[m.Name()]; ok {
		if x != m {
			return fmt.Errorf("cannot register measure %q; another measure with the same name is already registered", m.Name())
		}
		// the measure is already registered so there is nothing to do and the
		// command is considered successful.
		return nil
	}

	w.measures[m.Name()] = m
	return nil
}

func (w *worker) tryRegisterView(v *View) error {
	if x, ok := w.views[v.Name()]; ok {
		if x != v {
			return fmt.Errorf("cannot register view %q; another view with the same name is already registered", v.Name())
		}

		// the view is already registered so there is nothing to do and the
		// command is considered successful.
		return nil
	}

	// view is not registered and needs to be registered, but first its measure
	// needs to be registered.
	if err := w.tryRegisterMeasure(v.Measure()); err != nil {
		return fmt.Errorf("cannot register view %q: %v", v.Name(), err)
	}

	w.views[v.Name()] = v

	v.Measure().addView(v)
	return nil
}

func (w *worker) reportUsage(start time.Time) {
	for _, v := range w.views {
		if !v.isSubscribed() {
			continue
		}
		rows := v.collectedRows(start)
		if isCumulative(v) {
			s, ok := w.startTimes[v]
			if !ok {
				w.startTimes[v] = start
			} else {
				start = s
			}
		}
		viewData := &ViewData{
			View:  v,
			Start: start,
			End:   time.Now(),
			Rows:  rows,
		}
		exportersMu.Lock()
		for e := range exporters {
			e.Export(viewData)
		}
		exportersMu.Unlock()
		if !isCumulative(v) {
			v.clearRows()
		}
	}
}

func isCumulative(v *View) bool {
	switch v.Window().(type) {
	case *Cumulative:
		return true
	case Cumulative:
		return true
	}
	return false
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	//	"io"
	//	"io/ioutil"
	"net"
	"reflect"
	"testing"

	"github.com/admiraldolphin/govhack2017/server/game"
)

func TestGame(t *testing.T) {
	s := server{state: game.New()}
	if err := s.listenAndServe("localhost:0"); err != nil {
		t.Fatalf("Couldn't start game server: %v", err)
	}
	defer s.Close()

	conn1, err := net.Dial("tcp", s.Addr().String())
	if err != nil {
		t.Fatalf("Couldn't connect player 0 to game server: %v", err)
	}
	defer conn1.Close()
	p1 := json.NewEncoder(conn1)

	conn2, err := net.Dial("tcp", s.Addr().String())
	if err != nil {
		t.Fatalf("Couldn't connect player 1 to game server: %v", err)
	}
	defer conn2.Close()
	p2 := json.NewEncoder(conn2)

	// Play a game!
	g := []struct {
		conn   net.Conn
		player *json.Encoder
		action *game.Action
		want   *game.State
	}{
		{
			conn:   conn2,
			player: p2,
			action: &game.Action{Act: game.ActStartGame},
			want: &game.State{
				State: game.StateLobby,
				Players: []game.Player{
					{}, {},
				},
			},
		},
		{
			conn:   conn1,
			player: p1,
			action: &game.Action{Act: game.ActPlayCard},
			want: &game.State{
				State: game.StateInGame,
				Players: []game.Player{
					{}, {},
				},
			},
		},
		{
			conn:   conn2,
			player: p2,
			action: &game.Action{Act: game.ActDiscard},
			want: &game.State{
				State: game.StateInGame,
				Players: []game.Player{
					{}, {},
				},
			},
		},
	}

	for i, p := range g {
		if err := p.player.Encode(p.action); err != nil {
			t.Fatalf("Message %d [%v] got error %v", i, p.action, err)
		}
		s.state.RLock()
		if got, want := s.state.State, p.want.State; got != want {
			t.Errorf("After message %d [%v]: state.State = %v, want %v", i, p.action, got, want)
		}
		if got, want := s.state.Players, p.want.Players; !reflect.DeepEqual(got, want) {
			t.Errorf("After message %d [%v]: state.Players = %v, want %v", i, p.action, got, want)
		}
		s.state.RUnlock()
	}
}
<commit_msg>Narrowing in on bug<commit_after>package main

import (
	"encoding/json"
	//	"io"
	//	"io/ioutil"
	"net"
	"reflect"
	"testing"

	"github.com/admiraldolphin/govhack2017/server/game"
)

func TestGame(t *testing.T) {
	s := server{state: game.New()}
	r := &response{}
	if err := s.listenAndServe("localhost:0"); err != nil {
		t.Fatalf("Couldn't start game server: %v", err)
	}
	defer s.Close()

	conn1, err := net.Dial("tcp", s.Addr().String())
	if err != nil {
		t.Fatalf("Couldn't connect player 0 to game server: %v", err)
	}
	defer conn1.Close()

	send1 := json.NewEncoder(conn1)
	recv1 := json.NewDecoder(conn1)

	// Should get a response immediately
	if err := recv1.Decode(r); err != nil {
		t.Fatalf("Couldn't get state immediately: %v", err)
	}
	if got, want := r.PlayerID, 0; got != want {
		t.Errorf("response.PlayerID = %d, want %d", got, want)
	}
	if got, want := r.State.State, game.StateLobby; got != want {
		t.Errorf("response.State.State = %d, want %d", got, want)
	}

	conn2, err := net.Dial("tcp", s.Addr().String())
	if err != nil {
		t.Fatalf("Couldn't connect player 1 to game server: %v", err)
	}
	defer conn2.Close()

	send2 := json.NewEncoder(conn2)
	recv2 := json.NewDecoder(conn2)

	// Should get a response immediately
	if err := recv2.Decode(r); err != nil {
		t.Fatalf("Couldn't get state immediately: %v", err)
	}
	if got, want := r.PlayerID, 1; got != want {
		t.Errorf("response.PlayerID = %d, want %d", got, want)
	}
	if got, want := r.State.State, game.StateLobby; got != want {
		t.Errorf("response.State.State = %d, want %d", got, want)
	}

	// Play a game!
	g := []struct {
		send   *json.Encoder
		recv   *json.Decoder
		action *game.Action
		want   *game.State
	}{
		{
			send:   send2,
			recv:   recv2,
			action: &game.Action{Act: game.ActStartGame},
			want: &game.State{
				State: game.StateLobby,
				Players: []game.Player{
					{}, {},
				},
			},
		},
		{
			send:   send1,
			recv:   recv1,
			action: &game.Action{Act: game.ActPlayCard},
			want: &game.State{
				State: game.StateInGame,
				Players: []game.Player{
					{}, {},
				},
			},
		},
		{
			send:   send2,
			recv:   recv2,
			action: &game.Action{Act: game.ActDiscard},
			want: &game.State{
				State: game.StateInGame,
				Players: []game.Player{
					{}, {},
				},
			},
		},
	}

	for i, p := range g {
		if err := p.send.Encode(p.action); err != nil {
			t.Fatalf("Message %d [%v] got error %v", i, p.action, err)
		}
		if err := p.recv.Decode(r); err != nil {
			t.Errorf("After message %d [%v]: got error %v", i, p.action, err)
		}

		if got, want := r.State.State, p.want.State; got != want {
			t.Errorf("After message %d [%v]: state.State = %v, want %v", i, p.action, got, want)
		}
		if got, want := r.State.Players, p.want.Players; !reflect.DeepEqual(got, want) {
			t.Errorf("After message %d [%v]: state.Players = %v, want %v", i, p.action, got, want)
		}
	}
}
<|endoftext|>


<commit_before>package v7

import (
	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
	"code.cloudfoundry.org/cli/command"
	"code.cloudfoundry.org/cli/command/flag"
	"code.cloudfoundry.org/cli/command/v7/shared"
	"code.cloudfoundry.org/cli/resources"
)

type RestartCommand struct {
	BaseCommand

	RequiredArgs        flag.AppName            `positional-args:"yes"`
	Strategy            flag.DeploymentStrategy `long:"strategy" description:"Deployment strategy, either rolling or null."`
	NoWait              bool                    `long:"no-wait" description:"Exit when the first instance of the web process is healthy"`
	usage               interface{}             `usage:"CF_NAME restart APP_NAME\n\n   This command will cause downtime unless you use '--strategy rolling'.\n\n   If the app's most recent package is unstaged, restarting the app will stage and run that package.\n   Otherwise, the app's current droplet will be run."`
	relatedCommands     interface{}             `related_commands:"restage, restart-app-instance"`
	envCFStagingTimeout interface{}             `environmentName:"CF_STAGING_TIMEOUT" environmentDescription:"Max wait time for staging, in minutes" environmentDefault:"15"`
	envCFStartupTimeout interface{}             `environmentName:"CF_STARTUP_TIMEOUT" environmentDescription:"Max wait time for app instance startup, in minutes" environmentDefault:"5"`

	Stager shared.AppStager
}

func (cmd *RestartCommand) Setup(config command.Config, ui command.UI) error {
	err := cmd.BaseCommand.Setup(config, ui)
	if err != nil {
		return err
	}
	logCacheEndpoint, _, err := cmd.Actor.GetLogCacheEndpoint()
	if err != nil {
		ui.DisplayText("here3")
		return err
	}
	logCacheClient := command.NewLogCacheClient(logCacheEndpoint, config, ui)
	cmd.Stager = shared.NewAppStager(cmd.Actor, cmd.UI, cmd.Config, logCacheClient)

	return nil
}

func (cmd RestartCommand) Execute(args []string) error {
	err := cmd.SharedActor.CheckTarget(true, true)
	if err != nil {
		return err
	}

	user, err := cmd.Config.CurrentUser()
	if err != nil {
		return err
	}

	app, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID)
	cmd.UI.DisplayWarnings(warnings)
	if err != nil {
		return err
	}

	packageGUID, warnings, err := cmd.Actor.GetUnstagedNewestPackageGUID(app.GUID)
	cmd.UI.DisplayWarnings(warnings)
	if err != nil {
		return err
	}

	if packageGUID != "" || cmd.Strategy.Name == constant.DeploymentStrategyRolling {
		cmd.UI.DisplayTextWithFlavor("Restarting app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
			"AppName":   cmd.RequiredArgs.AppName,
			"OrgName":   cmd.Config.TargetedOrganization().Name,
			"SpaceName": cmd.Config.TargetedSpace().Name,
			"Username":  user.Name,
		})
		cmd.UI.DisplayNewline()
	}

	if packageGUID != "" {
		err = cmd.Stager.StageAndStart(app, cmd.Config.TargetedSpace(), cmd.Config.TargetedOrganization(), packageGUID, cmd.Strategy.Name, cmd.NoWait, constant.ApplicationRestarting)
		if err != nil {
			return err
		}
	} else {
		err = cmd.Stager.StartApp(app, resources.Droplet{}, cmd.Strategy.Name, cmd.NoWait, cmd.Config.TargetedSpace(), cmd.Config.TargetedOrganization(), constant.ApplicationRestarting)
		if err != nil {
			return err
		}
	}

	return nil
}
<commit_msg>Remove residual debugging log<commit_after>package v7

import (
	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
	"code.cloudfoundry.org/cli/command"
	"code.cloudfoundry.org/cli/command/flag"
	"code.cloudfoundry.org/cli/command/v7/shared"
	"code.cloudfoundry.org/cli/resources"
)

type RestartCommand struct {
	BaseCommand

	RequiredArgs        flag.AppName            `positional-args:"yes"`
	Strategy            flag.DeploymentStrategy `long:"strategy" description:"Deployment strategy, either rolling or null."`
	NoWait              bool                    `long:"no-wait" description:"Exit when the first instance of the web process is healthy"`
	usage               interface{}             `usage:"CF_NAME restart APP_NAME\n\n   This command will cause downtime unless you use '--strategy rolling'.\n\n   If the app's most recent package is unstaged, restarting the app will stage and run that package.\n   Otherwise, the app's current droplet will be run."`
	relatedCommands     interface{}             `related_commands:"restage, restart-app-instance"`
	envCFStagingTimeout interface{}             `environmentName:"CF_STAGING_TIMEOUT" environmentDescription:"Max wait time for staging, in minutes" environmentDefault:"15"`
	envCFStartupTimeout interface{}             `environmentName:"CF_STARTUP_TIMEOUT" environmentDescription:"Max wait time for app instance startup, in minutes" environmentDefault:"5"`

	Stager shared.AppStager
}

func (cmd *RestartCommand) Setup(config command.Config, ui command.UI) error {
	err := cmd.BaseCommand.Setup(config, ui)
	if err != nil {
		return err
	}
	logCacheEndpoint, _, err := cmd.Actor.GetLogCacheEndpoint()
	if err != nil {
		return err
	}
	logCacheClient := command.NewLogCacheClient(logCacheEndpoint, config, ui)
	cmd.Stager = shared.NewAppStager(cmd.Actor, cmd.UI, cmd.Config, logCacheClient)

	return nil
}

func (cmd RestartCommand) Execute(args []string) error {
	err := cmd.SharedActor.CheckTarget(true, true)
	if err != nil {
		return err
	}

	user, err := cmd.Config.CurrentUser()
	if err != nil {
		return err
	}

	app, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(cmd.RequiredArgs.AppName, cmd.Config.TargetedSpace().GUID)
	cmd.UI.DisplayWarnings(warnings)
	if err != nil {
		return err
	}

	packageGUID, warnings, err := cmd.Actor.GetUnstagedNewestPackageGUID(app.GUID)
	cmd.UI.DisplayWarnings(warnings)
	if err != nil {
		return err
	}

	if packageGUID != "" || cmd.Strategy.Name == constant.DeploymentStrategyRolling {
		cmd.UI.DisplayTextWithFlavor("Restarting app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
			"AppName":   cmd.RequiredArgs.AppName,
			"OrgName":   cmd.Config.TargetedOrganization().Name,
			"SpaceName": cmd.Config.TargetedSpace().Name,
			"Username":  user.Name,
		})
		cmd.UI.DisplayNewline()
	}

	if packageGUID != "" {
		err = cmd.Stager.StageAndStart(app, cmd.Config.TargetedSpace(), cmd.Config.TargetedOrganization(), packageGUID, cmd.Strategy.Name, cmd.NoWait, constant.ApplicationRestarting)
		if err != nil {
			return err
		}
	} else {
		err = cmd.Stager.StartApp(app, resources.Droplet{}, cmd.Strategy.Name, cmd.NoWait, cmd.Config.TargetedSpace(), cmd.Config.TargetedOrganization(), constant.ApplicationRestarting)
		if err != nil {
			return err
		}
	}

	return nil
}
<|endoftext|>


<commit_before>// Copyright 2013 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package docker

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/globocom/config"
	"github.com/globocom/tsuru/db"
	"github.com/globocom/tsuru/exec"
	"github.com/globocom/tsuru/log"
	"github.com/globocom/tsuru/provision"
	"github.com/globocom/tsuru/router"
	_ "github.com/globocom/tsuru/router/hipache"
	_ "github.com/globocom/tsuru/router/nginx"
	_ "github.com/globocom/tsuru/router/testing"
	"io"
	"labix.org/v2/mgo"
	"labix.org/v2/mgo/bson"
	"net"
	"strings"
	"sync"
)

func init() {
	provision.Register("docker", &DockerProvisioner{})
}

var execut exec.Executor

func executor() exec.Executor {
	if execut == nil {
		execut = exec.OsExecutor{}
	}
	return execut
}

func getRouter() (router.Router, error) {
	r, err := config.GetString("docker:router")
	if err != nil {
		return nil, err
	}
	return router.Get(r)
}

type DockerProvisioner struct{}

// Provision creates a container and install its dependencies
func (p *DockerProvisioner) Provision(app provision.App) error {
	return nil
}

func (p *DockerProvisioner) Restart(app provision.App) error {
	containers, err := getContainers(app.GetName())
	if err != nil {
		log.Printf("Got error while getting app containers: %s", err)
		return err
	}
	for _, c := range containers {
		err = c.stop()
		if err != nil {
			log.Printf("Error while stopping container %s", c.Id)
			return err
		}
		err = c.start()
		if err != nil {
			log.Printf("Error while starting container %s", c.Id)
			return err
		}
	}
	return nil
}

func (p *DockerProvisioner) Deploy(app provision.App, w io.Writer) error {
	_, err := newContainer(app)
	return err
}

func (p *DockerProvisioner) Destroy(app provision.App) error {
	units := app.ProvisionUnits()
	for _, u := range units {
		go func(u provision.AppUnit) {
			c := container{Id: u.GetName()}
			log.Printf("removing container %s", u.GetInstanceId())
			if err := c.remove(); err != nil {
				log.Print("Could not remove container. Aborting...")
				log.Print(err)
				return
			}
			log.Printf("removing container %s from the database", u.GetName())
			if err := collection().Remove(bson.M{"_id": c.Id}); err != nil {
				log.Printf("Could not remove container from database. Error %s", err.Error())
			}
			log.Print("Units successfuly removed.")
		}(u)
	}
	img := &image{Name: app.GetName()}
	log.Printf("removing image %s from the database", app.GetName())
	if err := img.remove(); err != nil {
		return err
	}
	return nil
}

func (*DockerProvisioner) Addr(app provision.App) (string, error) {
	units := app.ProvisionUnits()
	return units[0].GetIp(), nil
}

func (*DockerProvisioner) AddUnits(app provision.App, units uint) ([]provision.Unit, error) {
	return []provision.Unit{}, nil
}

func (*DockerProvisioner) RemoveUnit(app provision.App, unitName string) error {
	return nil
}

func (*DockerProvisioner) InstallDeps(app provision.App, w io.Writer) error {
	return nil
}

func (*DockerProvisioner) ExecuteCommand(stdout, stderr io.Writer, app provision.App, cmd string, args ...string) error {
	containers, err := getContainers(app.GetName())
	if err != nil {
		return err
	}
	if len(containers) == 0 {
		return errors.New("No containers for this app")
	}
	for _, c := range containers {
		err = c.ssh(stdout, stderr, cmd, args...)
		if err != nil {
			return err
		}
	}
	return nil
}

func (p *DockerProvisioner) CollectStatus() ([]provision.Unit, error) {
	docker, err := config.GetString("docker:binary")
	if err != nil {
		return nil, err
	}
	out, err := runCmd(docker, "ps", "-q")
	if err != nil {
		return nil, err
	}
	var linesGroup sync.WaitGroup
	lines := strings.Split(strings.TrimSpace(out), "\n")
	units := make(chan provision.Unit, len(lines))
	result := buildResult(len(lines), units)
	errs := make(chan error, 1)
	for _, line := range lines {
		linesGroup.Add(1)
		go collectUnit(line, units, errs, &linesGroup)
	}
	linesGroup.Wait()
	close(errs)
	close(units)
	if err, ok := <-errs; ok {
		return nil, err
	}
	return <-result, nil
}

func collectUnit(id string, units chan<- provision.Unit, errs chan<- error, wg *sync.WaitGroup) {
	defer wg.Done()
	docker, _ := config.GetString("docker:binary")
	container, err := getContainer(id)
	if err != nil {
		log.Printf("Container %q not in the database. Skipping...", id)
		return
	}
	out, err := runCmd(docker, "inspect", id)
	if err != nil {
		errs <- err
		return
	}
	var c map[string]interface{}
	err = json.Unmarshal([]byte(out), &c)
	if err != nil {
		errs <- err
		return
	}
	unit := provision.Unit{
		Name:    container.Id,
		AppName: container.AppName,
		Type:    container.Type,
	}
	unit.Ip = c["NetworkSettings"].(map[string]interface{})["IpAddress"].(string)
	addr := fmt.Sprintf("%s:%s", unit.Ip, container.Port)
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		unit.Status = provision.StatusInstalling
	} else {
		conn.Close()
		unit.Status = provision.StatusStarted
	}
	units <- unit
}

func buildResult(maxSize int, units chan provision.Unit) <-chan []provision.Unit {
	ch := make(chan []provision.Unit, 1)
	go func() {
		result := make([]provision.Unit, 0, maxSize)
		for unit := range units {
			result = append(result, unit)
		}
		ch <- result
	}()
	return ch
}

func getPort(portMapping map[string]interface{}) string {
	var port string
	for k := range portMapping {
		port = k
		break
	}
	return port
}

func collection() *mgo.Collection {
	name, err := config.GetString("docker:collection")
	if err != nil {
		log.Fatalf("FATAL: %s.", err)
	}
	conn, err := db.Conn()
	if err != nil {
		log.Printf("Failed to connect to the database: %s", err)
	}
	return conn.Collection(name)
}

func imagesCollection() *mgo.Collection {
	nameIndex := mgo.Index{Key: []string{"name"}, Unique: true}
	conn, err := db.Conn()
	if err != nil {
		log.Printf("Failed to connect to the database: %s", err)
	}
	c := conn.Collection("docker_image")
	c.EnsureIndex(nameIndex)
	return c
}
<commit_msg>provision/docker/provisioner: removing duplicated logic to remove container from db<commit_after>// Copyright 2013 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package docker

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/globocom/config"
	"github.com/globocom/tsuru/db"
	"github.com/globocom/tsuru/exec"
	"github.com/globocom/tsuru/log"
	"github.com/globocom/tsuru/provision"
	"github.com/globocom/tsuru/router"
	_ "github.com/globocom/tsuru/router/hipache"
	_ "github.com/globocom/tsuru/router/nginx"
	_ "github.com/globocom/tsuru/router/testing"
	"io"
	"labix.org/v2/mgo"
	"net"
	"strings"
	"sync"
)

func init() {
	provision.Register("docker", &DockerProvisioner{})
}

var execut exec.Executor

func executor() exec.Executor {
	if execut == nil {
		execut = exec.OsExecutor{}
	}
	return execut
}

func getRouter() (router.Router, error) {
	r, err := config.GetString("docker:router")
	if err != nil {
		return nil, err
	}
	return router.Get(r)
}

type DockerProvisioner struct{}

// Provision creates a container and install its dependencies
func (p *DockerProvisioner) Provision(app provision.App) error {
	return nil
}

func (p *DockerProvisioner) Restart(app provision.App) error {
	containers, err := getContainers(app.GetName())
	if err != nil {
		log.Printf("Got error while getting app containers: %s", err)
		return err
	}
	for _, c := range containers {
		err = c.stop()
		if err != nil {
			log.Printf("Error while stopping container %s", c.Id)
			return err
		}
		err = c.start()
		if err != nil {
			log.Printf("Error while starting container %s", c.Id)
			return err
		}
	}
	return nil
}

func (p *DockerProvisioner) Deploy(app provision.App, w io.Writer) error {
	_, err := newContainer(app)
	return err
}

func (p *DockerProvisioner) Destroy(app provision.App) error {
	units := app.ProvisionUnits()
	for _, u := range units {
		go func(u provision.AppUnit) {
			c := container{Id: u.GetName()}
			if err := c.remove(); err != nil {
				return
			}
		}(u)
	}
	return nil
}

func (*DockerProvisioner) Addr(app provision.App) (string, error) {
	units := app.ProvisionUnits()
	return units[0].GetIp(), nil
}

func (*DockerProvisioner) AddUnits(app provision.App, units uint) ([]provision.Unit, error) {
	return []provision.Unit{}, nil
}

func (*DockerProvisioner) RemoveUnit(app provision.App, unitName string) error {
	return nil
}

func (*DockerProvisioner) InstallDeps(app provision.App, w io.Writer) error {
	return nil
}

func (*DockerProvisioner) ExecuteCommand(stdout, stderr io.Writer, app provision.App, cmd string, args ...string) error {
	containers, err := getContainers(app.GetName())
	if err != nil {
		return err
	}
	if len(containers) == 0 {
		return errors.New("No containers for this app")
	}
	for _, c := range containers {
		err = c.ssh(stdout, stderr, cmd, args...)
		if err != nil {
			return err
		}
	}
	return nil
}

func (p *DockerProvisioner) CollectStatus() ([]provision.Unit, error) {
	docker, err := config.GetString("docker:binary")
	if err != nil {
		return nil, err
	}
	out, err := runCmd(docker, "ps", "-q")
	if err != nil {
		return nil, err
	}
	var linesGroup sync.WaitGroup
	lines := strings.Split(strings.TrimSpace(out), "\n")
	units := make(chan provision.Unit, len(lines))
	result := buildResult(len(lines), units)
	errs := make(chan error, 1)
	for _, line := range lines {
		linesGroup.Add(1)
		go collectUnit(line, units, errs, &linesGroup)
	}
	linesGroup.Wait()
	close(errs)
	close(units)
	if err, ok := <-errs; ok {
		return nil, err
	}
	return <-result, nil
}

func collectUnit(id string, units chan<- provision.Unit, errs chan<- error, wg *sync.WaitGroup) {
	defer wg.Done()
	docker, _ := config.GetString("docker:binary")
	container, err := getContainer(id)
	if err != nil {
		log.Printf("Container %q not in the database. Skipping...", id)
		return
	}
	out, err := runCmd(docker, "inspect", id)
	if err != nil {
		errs <- err
		return
	}
	var c map[string]interface{}
	err = json.Unmarshal([]byte(out), &c)
	if err != nil {
		errs <- err
		return
	}
	unit := provision.Unit{
		Name:    container.Id,
		AppName: container.AppName,
		Type:    container.Type,
	}
	unit.Ip = c["NetworkSettings"].(map[string]interface{})["IpAddress"].(string)
	addr := fmt.Sprintf("%s:%s", unit.Ip, container.Port)
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		unit.Status = provision.StatusInstalling
	} else {
		conn.Close()
		unit.Status = provision.StatusStarted
	}
	units <- unit
}

func buildResult(maxSize int, units chan provision.Unit) <-chan []provision.Unit {
	ch := make(chan []provision.Unit, 1)
	go func() {
		result := make([]provision.Unit, 0, maxSize)
		for unit := range units {
			result = append(result, unit)
		}
		ch <- result
	}()
	return ch
}

func getPort(portMapping map[string]interface{}) string {
	var port string
	for k := range portMapping {
		port = k
		break
	}
	return port
}

func collection() *mgo.Collection {
	name, err := config.GetString("docker:collection")
	if err != nil {
		log.Fatalf("FATAL: %s.", err)
	}
	conn, err := db.Conn()
	if err != nil {
		log.Printf("Failed to connect to the database: %s", err)
	}
	return conn.Collection(name)
}

func imagesCollection() *mgo.Collection {
	nameIndex := mgo.Index{Key: []string{"name"}, Unique: true}
	conn, err := db.Conn()
	if err != nil {
		log.Printf("Failed to connect to the database: %s", err)
	}
	c := conn.Collection("docker_image")
	c.EnsureIndex(nameIndex)
	return c
}
<|endoftext|>


<commit_before>package genetics

import (
	"github.com/yaricom/goNEAT/neat/network"
	"fmt"
)

// The Multiple-Input Multiple-Output (MIMO) control Gene allows to create modular genomes, in which several groups of genes
// connected through single MIMO Gene and corresponding control function is applied to all inputs in order to produce
// outputs. This allows to build modular hierarchical genomes which can be considered as sum of constituent components
// and evolved as a whole and as a concrete parts simultaneously.
type MIMOControlGene struct {
	// The current innovation number for this gene
	InnovationNum int64
	// Used to see how much mutation has changed the link
	MutationNum   float64
	// If true the gene is enabled
	IsEnabled     bool

	// The control node with control/activation function
	ControlNode   *network.NNode
}

// Creates new MIMO gene
func NewMIMOGene(control_node *network.NNode, innov_num int64, mut_num float64, enabled bool) *MIMOControlGene {
	gene := &MIMOControlGene{
		ControlNode:control_node,
		InnovationNum:innov_num,
		MutationNum:mut_num,
		IsEnabled:enabled,
	}
	return gene
}

// The copy constructor taking parameters from provided control gene for given control node
func NewMIMOGeneCopy(g *MIMOControlGene, control_node *network.NNode) *MIMOControlGene {
	cg := NewMIMOGene(control_node, g.InnovationNum, g.MutationNum, g.IsEnabled)
	return cg
}

// The stringer
func (g *MIMOControlGene) String() string {
	enabl_str := ""
	if !g.IsEnabled {
		enabl_str = " -DISABLED-"
	}
	return fmt.Sprintf("[MIMO Gene INNOV (%4d, % .3f) %s control node: %s]",
		g.InnovationNum, g.MutationNum, enabl_str, g.ControlNode.String())
}<commit_msg>Implemented IO nodes intersection check<commit_after>package genetics

import (
	"github.com/yaricom/goNEAT/neat/network"
	"fmt"
)

// The Multiple-Input Multiple-Output (MIMO) control Gene allows to create modular genomes, in which several groups of genes
// connected through single MIMO Gene and corresponding control function is applied to all inputs in order to produce
// outputs. This allows to build modular hierarchical genomes which can be considered as sum of constituent components
// and evolved as a whole and as a concrete parts simultaneously.
type MIMOControlGene struct {
	// The current innovation number for this gene
	InnovationNum int64
	// Used to see how much mutation has changed the link
	MutationNum   float64
	// If true the gene is enabled
	IsEnabled     bool

	// The control node with control/activation function
	ControlNode   *network.NNode

	// The list of associated IO nodes for fast traversal
	ioNodes       []*network.NNode
}

// Creates new MIMO gene
func NewMIMOGene(control_node *network.NNode, innov_num int64, mut_num float64, enabled bool) *MIMOControlGene {
	gene := &MIMOControlGene{
		ControlNode:control_node,
		InnovationNum:innov_num,
		MutationNum:mut_num,
		IsEnabled:enabled,
	}
	// collect IO nodes list
	gene.ioNodes = make([]*network.NNode, 0)
	for _, l := range control_node.Incoming {
		gene.ioNodes = append(gene.ioNodes, l.InNode)
	}
	for _, l := range control_node.Outgoing {
		gene.ioNodes = append(gene.ioNodes, l.OutNode)
	}

	return gene
}

// The copy constructor taking parameters from provided control gene for given control node
func NewMIMOGeneCopy(g *MIMOControlGene, control_node *network.NNode) *MIMOControlGene {
	cg := NewMIMOGene(control_node, g.InnovationNum, g.MutationNum, g.IsEnabled)
	return cg
}

// Tests whether this gene has intersection with provided map of nodes, i.e. any of it's IO nodes included into list
func (g *MIMOControlGene) hasIntersection(nodes map[int]*network.NNode) bool {
	for _, nd := range g.ioNodes {
		if _, Ok := nodes[nd.Id]; Ok {
			// found
			return true
		}
	}
	return false
}

// The stringer
func (g *MIMOControlGene) String() string {
	enabl_str := ""
	if !g.IsEnabled {
		enabl_str = " -DISABLED-"
	}
	return fmt.Sprintf("[MIMO Gene INNOV (%4d, % .3f) %s control node: %s]",
		g.InnovationNum, g.MutationNum, enabl_str, g.ControlNode.String())
}<|endoftext|>