<commit_before>// Package gotestcover provides multiple packages support for Go test cover.
package main

import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"sync"
)

var (
	// go build
	flagA    bool
	flagX    bool
	flagRace bool
	flagTags string

	// go test
	flagV            bool
	flagCount        int
	flagCPU          string
	flagParallel     string
	flagRun          string
	flagShort        bool
	flagTimeout      string
	flagCoverMode    string
	flagCoverProfile string

	// custom
	flagParallelPackages = runtime.GOMAXPROCS(0)

	// GAE/Go
	flagGoogleAppEngine bool
)

func main() {
	err := run()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func run() error {
	err := parseFlags()
	if err != nil {
		return err
	}
	pkgArgs, flagArgs := parseArgs()
	pkgs, err := resolvePackages(pkgArgs)
	if err != nil {
		return err
	}
	cov, failed := runAllPackageTests(pkgs, flagArgs, func(out string) {
		fmt.Print(out)
	})
	err = writeCoverProfile(cov)
	if err != nil {
		return err
	}
	if failed {
		return fmt.Errorf("test failed")
	}
	return nil
}

func parseFlags() error {
	flag.BoolVar(&flagA, "a", flagA, "see 'go build' help")
	flag.BoolVar(&flagX, "x", flagX, "see 'go build' help")
	flag.BoolVar(&flagRace, "race", flagRace, "see 'go build' help")
	flag.StringVar(&flagTags, "tags", flagTags, "see 'go build' help")

	flag.BoolVar(&flagV, "v", flagV, "see 'go test' help")
	flag.IntVar(&flagCount, "count", flagCount, "see 'go test' help")
	flag.StringVar(&flagCPU, "cpu", flagCPU, "see 'go test' help")
	flag.StringVar(&flagParallel, "parallel", flagParallel, "see 'go test' help")
	flag.StringVar(&flagRun, "run", flagRun, "see 'go test' help")
	flag.BoolVar(&flagShort, "short", flagShort, "see 'go test' help")
	flag.StringVar(&flagTimeout, "timeout", flagTimeout, "see 'go test' help")
	flag.StringVar(&flagCoverMode, "covermode", flagCoverMode, "see 'go test' help")
	flag.StringVar(&flagCoverProfile, "coverprofile", flagCoverProfile, "see 'go test' help")

	flag.IntVar(&flagParallelPackages, "parallelpackages", flagParallelPackages, "Number of package test run in parallel")

	flag.BoolVar(&flagGoogleAppEngine, "gae", flagGoogleAppEngine, "Bool of Command exec in GAE/Go")

	flag.Parse()
	if flagCoverProfile == "" {
		return fmt.Errorf("flag coverprofile must be set")
	}
	if flagParallelPackages < 1 {
		return fmt.Errorf("flag parallelpackages must be greater than or equal to 1")
	}
	return nil
}

func parseArgs() (pkgArgs, flagArgs []string) {
	args := flag.Args()
	for i, a := range args {
		if strings.HasPrefix(a, "-") {
			return args[:i], args[i:]
		}
	}
	return args, nil
}

func resolvePackages(pkgArgs []string) ([]string, error) {
	cmdArgs := []string{"list"}
	cmdArgs = append(cmdArgs, pkgArgs...)
	cmdOut, err := runGoCommand(cmdArgs...)
	if err != nil {
		return nil, err
	}
	var pkgs []string
	sc := bufio.NewScanner(bytes.NewReader(cmdOut))
	for sc.Scan() {
		pkgs = append(pkgs, sc.Text())
	}
	return pkgs, nil
}

func runAllPackageTests(pkgs []string, flgs []string, pf func(string)) ([]byte, bool) {
	pkgch := make(chan string)
	type res struct {
		out string
		cov []byte
		err error
	}
	resch := make(chan res)
	wg := new(sync.WaitGroup)
	wg.Add(flagParallelPackages)
	go func() {
		for _, pkg := range pkgs {
			pkgch <- pkg
		}
		close(pkgch)
		wg.Wait()
		close(resch)
	}()
	for i := 0; i < flagParallelPackages; i++ {
		go func() {
			for p := range pkgch {
				out, cov, err := runPackageTests(p, flgs)
				resch <- res{
					out: out,
					cov: cov,
					err: err,
				}
			}
			wg.Done()
		}()
	}
	failed := false
	var cov []byte
	for r := range resch {
		if r.err == nil {
			pf(r.out)
			cov = append(cov, r.cov...)
		} else {
			pf(r.err.Error())
			failed = true
		}
	}
	return cov, failed
}

func runPackageTests(pkg string, flgs []string) (out string, cov []byte, err error) {
	coverFile, err := ioutil.TempFile("", "gotestcover-")
	if err != nil {
		return "", nil, err
	}
	defer os.Remove(coverFile.Name())
	defer coverFile.Close()
	var args []string
	args = append(args, "test")

	if flagA {
		args = append(args, "-a")
	}
	if flagX {
		args = append(args, "-x")
	}
	if flagRace {
		args = append(args, "-race")
	}
	if flagTags != "" {
		args = append(args, "-tags", flagTags)
	}

	if flagV {
		args = append(args, "-v")
	}
	if flagCount != 0 {
		args = append(args, "-count", fmt.Sprint(flagCount))
	}
	if flagCPU != "" {
		args = append(args, "-cpu", flagCPU)
	}
	if flagParallel != "" {
		args = append(args, "-parallel", flagParallel)
	}
	if flagRun != "" {
		args = append(args, "-run", flagRun)
	}
	if flagShort {
		args = append(args, "-short")
	}
	if flagTimeout != "" {
		args = append(args, "-timeout", flagTimeout)
	}
	args = append(args, "-cover")
	if flagCoverMode != "" {
		args = append(args, "-covermode", flagCoverMode)
	}
	args = append(args, "-coverprofile", coverFile.Name())

	args = append(args, pkg)

	args = append(args, flgs...)

	cmdOut, err := runGoCommand(args...)
	if err != nil {
		return "", nil, err
	}
	cov, err = ioutil.ReadAll(coverFile)
	if err != nil {
		return "", nil, err
	}
	cov = removeFirstLine(cov)
	return string(cmdOut), cov, nil
}

func writeCoverProfile(cov []byte) error {
	if len(cov) == 0 {
		return nil
	}
	buf := new(bytes.Buffer)
	mode := flagCoverMode
	if mode == "" {
		if flagRace {
			mode = "atomic"
		} else {
			mode = "set"
		}
	}
	fmt.Fprintf(buf, "mode: %s\n", mode)
	buf.Write(cov)
	return ioutil.WriteFile(flagCoverProfile, buf.Bytes(), os.FileMode(0644))
}

func runGoCommand(args ...string) ([]byte, error) {
	goCmd := "go"
	if flagGoogleAppEngine {
		goCmd = "goapp"
	}
	cmd := exec.Command(goCmd, args...)
	out, err := cmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("command %s: %s\n%s", cmd.Args, err, out)
	}
	return out, nil
}

func removeFirstLine(b []byte) []byte {
	out := new(bytes.Buffer)
	sc := bufio.NewScanner(bytes.NewReader(b))
	firstLine := true
	for sc.Scan() {
		if firstLine {
			firstLine = false
			continue
		}
		fmt.Fprintf(out, "%s\n", sc.Bytes())
	}
	return out.Bytes()
}
<commit_msg>add -coverpkg support<commit_after>// Package gotestcover provides multiple packages support for Go test cover.
package main

import (
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"sync"
)

var (
	// go build
	flagA    bool
	flagX    bool
	flagRace bool
	flagTags string

	// go test
	flagV            bool
	flagCount        int
	flagCPU          string
	flagParallel     string
	flagRun          string
	flagShort        bool
	flagTimeout      string
	flagCoverMode    string
	flagCoverPkg     string
	flagCoverProfile string

	// custom
	flagParallelPackages = runtime.GOMAXPROCS(0)

	// GAE/Go
	flagGoogleAppEngine bool
)

func main() {
	err := run()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func run() error {
	err := parseFlags()
	if err != nil {
		return err
	}
	pkgArgs, flagArgs := parseArgs()
	pkgs, err := resolvePackages(pkgArgs)
	if err != nil {
		return err
	}
	cov, failed := runAllPackageTests(pkgs, flagArgs, func(out string) {
		fmt.Print(out)
	})
	merge := exec.Command("sh", "-c", "sort -k 3 -n -r | sort -s -k 1,2 -u")
	merge.Stdin = bytes.NewReader(cov)
	cov, err = merge.Output()
	if err != nil {
		return err
	}
	err = writeCoverProfile(cov)
	if err != nil {
		return err
	}
	if failed {
		return fmt.Errorf("test failed")
	}
	return nil
}

func parseFlags() error {
	flag.BoolVar(&flagA, "a", flagA, "see 'go build' help")
	flag.BoolVar(&flagX, "x", flagX, "see 'go build' help")
	flag.BoolVar(&flagRace, "race", flagRace, "see 'go build' help")
	flag.StringVar(&flagTags, "tags", flagTags, "see 'go build' help")

	flag.BoolVar(&flagV, "v", flagV, "see 'go test' help")
	flag.IntVar(&flagCount, "count", flagCount, "see 'go test' help")
	flag.StringVar(&flagCPU, "cpu", flagCPU, "see 'go test' help")
	flag.StringVar(&flagParallel, "parallel", flagParallel, "see 'go test' help")
	flag.StringVar(&flagRun, "run", flagRun, "see 'go test' help")
	flag.BoolVar(&flagShort, "short", flagShort, "see 'go test' help")
	flag.StringVar(&flagTimeout, "timeout", flagTimeout, "see 'go test' help")
	flag.StringVar(&flagCoverMode, "covermode", flagCoverMode, "see 'go test' help")
	flag.StringVar(&flagCoverPkg, "coverpkg", flagCoverPkg, "see 'go test' help")
	flag.StringVar(&flagCoverProfile, "coverprofile", flagCoverProfile, "see 'go test' help")

	flag.IntVar(&flagParallelPackages, "parallelpackages", flagParallelPackages, "Number of package test run in parallel")

	flag.BoolVar(&flagGoogleAppEngine, "gae", flagGoogleAppEngine, "Bool of Command exec in GAE/Go")

	flag.Parse()
	if flagCoverProfile == "" {
		return fmt.Errorf("flag coverprofile must be set")
	}
	if flagParallelPackages < 1 {
		return fmt.Errorf("flag parallelpackages must be greater than or equal to 1")
	}
	return nil
}

func parseArgs() (pkgArgs, flagArgs []string) {
	args := flag.Args()
	for i, a := range args {
		if strings.HasPrefix(a, "-") {
			return args[:i], args[i:]
		}
	}
	return args, nil
}

func resolvePackages(pkgArgs []string) ([]string, error) {
	cmdArgs := []string{"list"}
	cmdArgs = append(cmdArgs, pkgArgs...)
	cmdOut, err := runGoCommand(cmdArgs...)
	if err != nil {
		return nil, err
	}
	var pkgs []string
	sc := bufio.NewScanner(bytes.NewReader(cmdOut))
	for sc.Scan() {
		pkgs = append(pkgs, sc.Text())
	}
	return pkgs, nil
}

func runAllPackageTests(pkgs []string, flgs []string, pf func(string)) ([]byte, bool) {
	pkgch := make(chan string)
	type res struct {
		out string
		cov []byte
		err error
	}
	resch := make(chan res)
	wg := new(sync.WaitGroup)
	wg.Add(flagParallelPackages)
	go func() {
		for _, pkg := range pkgs {
			pkgch <- pkg
		}
		close(pkgch)
		wg.Wait()
		close(resch)
	}()
	for i := 0; i < flagParallelPackages; i++ {
		go func() {
			for p := range pkgch {
				out, cov, err := runPackageTests(p, flgs)
				resch <- res{
					out: out,
					cov: cov,
					err: err,
				}
			}
			wg.Done()
		}()
	}
	failed := false
	var cov []byte
	for r := range resch {
		if r.err == nil {
			pf(r.out)
			cov = append(cov, r.cov...)
		} else {
			pf(r.err.Error())
			failed = true
		}
	}
	return cov, failed
}

func runPackageTests(pkg string, flgs []string) (out string, cov []byte, err error) {
	coverFile, err := ioutil.TempFile("", "gotestcover-")
	if err != nil {
		return "", nil, err
	}
	defer os.Remove(coverFile.Name())
	defer coverFile.Close()
	var args []string
	args = append(args, "test")

	if flagA {
		args = append(args, "-a")
	}
	if flagX {
		args = append(args, "-x")
	}
	if flagRace {
		args = append(args, "-race")
	}
	if flagTags != "" {
		args = append(args, "-tags", flagTags)
	}

	if flagV {
		args = append(args, "-v")
	}
	if flagCount != 0 {
		args = append(args, "-count", fmt.Sprint(flagCount))
	}
	if flagCPU != "" {
		args = append(args, "-cpu", flagCPU)
	}
	if flagParallel != "" {
		args = append(args, "-parallel", flagParallel)
	}
	if flagRun != "" {
		args = append(args, "-run", flagRun)
	}
	if flagShort {
		args = append(args, "-short")
	}
	if flagTimeout != "" {
		args = append(args, "-timeout", flagTimeout)
	}
	args = append(args, "-cover")
	if flagCoverMode != "" {
		args = append(args, "-covermode", flagCoverMode)
	}
	if flagCoverPkg != "" {
		args = append(args, "-coverpkg", flagCoverPkg)
	}
	args = append(args, "-coverprofile", coverFile.Name())

	args = append(args, pkg)

	args = append(args, flgs...)

	cmdOut, err := runGoCommand(args...)
	if err != nil {
		return "", nil, err
	}
	cov, err = ioutil.ReadAll(coverFile)
	if err != nil {
		return "", nil, err
	}
	cov = removeFirstLine(cov)
	return string(cmdOut), cov, nil
}

func writeCoverProfile(cov []byte) error {
	if len(cov) == 0 {
		return nil
	}
	buf := new(bytes.Buffer)
	mode := flagCoverMode
	if mode == "" {
		if flagRace {
			mode = "atomic"
		} else {
			mode = "set"
		}
	}
	fmt.Fprintf(buf, "mode: %s\n", mode)
	buf.Write(cov)
	return ioutil.WriteFile(flagCoverProfile, buf.Bytes(), os.FileMode(0644))
}

func runGoCommand(args ...string) ([]byte, error) {
	goCmd := "go"
	if flagGoogleAppEngine {
		goCmd = "goapp"
	}
	cmd := exec.Command(goCmd, args...)
	out, err := cmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("command %s: %s\n%s", cmd.Args, err, out)
	}
	return out, nil
}

func removeFirstLine(b []byte) []byte {
	out := new(bytes.Buffer)
	sc := bufio.NewScanner(bytes.NewReader(b))
	firstLine := true
	for sc.Scan() {
		if firstLine {
			firstLine = false
			continue
		}
		fmt.Fprintf(out, "%s\n", sc.Bytes())
	}
	return out.Bytes()
}
<|endoftext|>


<commit_before>package etcd

import (
	"encoding/json"
	"fmt"
	"github.com/coreos/etcd/store"
	"io/ioutil"
	"net/http"
	"path"
	"strings"
)

type Options map[string]interface{}

var (
	// Making a map to make it easier to test existence
	validGetOptions = map[string]bool{
		"recursive":  true,
		"consistent": true,
		"sorted":     true,
		"wait":       true,
		"wait_index": true,
	}
)

// Get the value of the given key
func (c *Client) Get(key string) ([]*store.Response, error) {
	return c.GetWithOptions(key, nil)
}

// The same with Get, but allows passing options
func (c *Client) GetWithOptions(key string, options Options) ([]*store.Response, error) {
	logger.Debugf("get %s [%s]", key, c.cluster.Leader)

	p := path.Join("keys", key)
	if options != nil {
		str, err := optionsToString(options)
		if err != nil {
			return nil, err
		}
		p += str
	}

	resp, err := c.sendRequest("GET", p, "")

	if err != nil {
		return nil, err
	}

	b, err := ioutil.ReadAll(resp.Body)

	resp.Body.Close()

	if err != nil {
		return nil, err
	}

	if resp.StatusCode != http.StatusOK {

		return nil, handleError(b)
	}

	return convertGetResponse(b)
}

// GetTo gets the value of the key from a given machine address.
// If the given machine is not available it returns an error.
// Mainly use for testing purpose
func (c *Client) GetFrom(key string, addr string) ([]*store.Response, error) {
	return c.GetFromWithOptions(key, addr, nil)
}

// The same with GetFrom, but allows passing options
func (c *Client) GetFromWithOptions(key string, addr string, options Options) ([]*store.Response, error) {
	httpPath := c.createHttpPath(addr, path.Join(version, "keys", key))

	if options != nil {
		str, err := optionsToString(options)
		if err != nil {
			return nil, err
		}
		httpPath += str
	}

	resp, err := c.httpClient.Get(httpPath)

	if err != nil {
		return nil, err
	}

	b, err := ioutil.ReadAll(resp.Body)

	resp.Body.Close()

	if err != nil {
		return nil, err
	}

	if resp.StatusCode != http.StatusOK {
		return nil, handleError(b)
	}

	return convertGetResponse(b)
}

// Convert byte stream to response.
func convertGetResponse(b []byte) ([]*store.Response, error) {

	var results []*store.Response
	var result *store.Response

	err := json.Unmarshal(b, &result)

	if err != nil {
		err = json.Unmarshal(b, &results)

		if err != nil {
			return nil, err
		}

	} else {
		results = make([]*store.Response, 1)
		results[0] = result
	}
	return results, nil
}

// Convert options to a string of HTML parameters
func optionsToString(options Options) (string, error) {
	p := "?"
	optionArr := make([]string, 0)
	for opKey, opVal := range options {
		if validGetOptions[opKey] {
			optionArr = append(optionArr, fmt.Sprintf("%v=%v", opKey, opVal))
		} else {
			return "", fmt.Errorf("Invalid option: %v", opKey)
		}
	}
	p += strings.Join(optionArr, "&")
	return p, nil
}
<commit_msg>Use url.Values to format HTML parameters<commit_after>package etcd

import (
	"encoding/json"
	"fmt"
	"github.com/coreos/etcd/store"
	"io/ioutil"
	"net/http"
	"net/url"
	"path"
)

type Options map[string]interface{}

var (
	// Making a map to make it easier to test existence
	validGetOptions = map[string]bool{
		"recursive":  true,
		"consistent": true,
		"sorted":     true,
		"wait":       true,
		"wait_index": true,
	}
)

// Get the value of the given key
func (c *Client) Get(key string) ([]*store.Response, error) {
	return c.GetWithOptions(key, nil)
}

// The same with Get, but allows passing options
func (c *Client) GetWithOptions(key string, options Options) ([]*store.Response, error) {
	logger.Debugf("get %s [%s]", key, c.cluster.Leader)

	p := path.Join("keys", key)
	if options != nil {
		str, err := optionsToString(options)
		if err != nil {
			return nil, err
		}
		p += str
	}

	resp, err := c.sendRequest("GET", p, "")

	if err != nil {
		return nil, err
	}

	b, err := ioutil.ReadAll(resp.Body)

	resp.Body.Close()

	if err != nil {
		return nil, err
	}

	if resp.StatusCode != http.StatusOK {

		return nil, handleError(b)
	}

	return convertGetResponse(b)
}

// GetTo gets the value of the key from a given machine address.
// If the given machine is not available it returns an error.
// Mainly use for testing purpose
func (c *Client) GetFrom(key string, addr string) ([]*store.Response, error) {
	return c.GetFromWithOptions(key, addr, nil)
}

// The same with GetFrom, but allows passing options
func (c *Client) GetFromWithOptions(key string, addr string, options Options) ([]*store.Response, error) {
	httpPath := c.createHttpPath(addr, path.Join(version, "keys", key))

	if options != nil {
		str, err := optionsToString(options)
		if err != nil {
			return nil, err
		}
		httpPath += str
	}

	resp, err := c.httpClient.Get(httpPath)

	if err != nil {
		return nil, err
	}

	b, err := ioutil.ReadAll(resp.Body)

	resp.Body.Close()

	if err != nil {
		return nil, err
	}

	if resp.StatusCode != http.StatusOK {
		return nil, handleError(b)
	}

	return convertGetResponse(b)
}

// Convert byte stream to response.
func convertGetResponse(b []byte) ([]*store.Response, error) {

	var results []*store.Response
	var result *store.Response

	err := json.Unmarshal(b, &result)

	if err != nil {
		err = json.Unmarshal(b, &results)

		if err != nil {
			return nil, err
		}

	} else {
		results = make([]*store.Response, 1)
		results[0] = result
	}
	return results, nil
}

// Convert options to a string of HTML parameters
func optionsToString(options Options) (string, error) {
	p := "?"
	v := url.Values{}
	for opKey, opVal := range options {
		if validGetOptions[opKey] {
			v.Set(opKey, fmt.Sprintf("%v", opVal))
		} else {
			return "", fmt.Errorf("Invalid option: %v", opKey)
		}
	}
	p += v.Encode()
	return p, nil
}
<|endoftext|>


<commit_before>package hub

import (
	"errors"
	"net"
	"time"

	"github.com/v2ray/v2ray-core/common/log"
	"github.com/v2ray/v2ray-core/transport"
	"github.com/v2ray/v2ray-core/transport/hub/kcpv"
	"github.com/xtaci/kcp-go"
)

type KCPVlistener struct {
	lst  *kcp.Listener
	conf *kcpv.Config
}

func (kvl *KCPVlistener) Accept() (*KCPVconn, error) {
	conn, err := kvl.lst.Accept()
	if err != nil {
		return nil, err
	}

	kcv := &KCPVconn{hc: conn}
	kcv.conf = kvl.conf
	err = kcv.ApplyConf()
	if err != nil {
		return nil, err
	}
	return kcv, nil
}
func (kvl *KCPVlistener) Close() error {
	return kvl.lst.Close()
}
func (kvl *KCPVlistener) Addr() net.Addr {
	return kvl.lst.Addr()
}

type KCPVconn struct {
	hc         *kcp.UDPSession
	conf       *kcpv.Config
	conntokeep time.Time
}

func (kcpvc *KCPVconn) Read(b []byte) (int, error) {
	ifb := time.Now().Add(time.Duration(kcpvc.conf.AdvancedConfigs.ReadTimeout) * time.Second)
	if ifb.After(kcpvc.conntokeep) {
		kcpvc.conntokeep = ifb
	}
	kcpvc.hc.SetDeadline(kcpvc.conntokeep)
	return kcpvc.hc.Read(b)
}

func (kcpvc *KCPVconn) Write(b []byte) (int, error) {
	ifb := time.Now().Add(time.Duration(kcpvc.conf.AdvancedConfigs.WriteTimeout) * time.Second)
	if ifb.After(kcpvc.conntokeep) {
		kcpvc.conntokeep = ifb
	}
	kcpvc.hc.SetDeadline(kcpvc.conntokeep)
	return kcpvc.hc.Write(b)
}
func (kcpvc *KCPVconn) ApplyConf() error {
	nodelay, interval, resend, nc := 0, 40, 0, 0
	if kcpvc.conf.Mode != "manual" {
		switch kcpvc.conf.Mode {
		case "normal":
			nodelay, interval, resend, nc = 0, 30, 2, 1
		case "fast":
			nodelay, interval, resend, nc = 0, 20, 2, 1
		case "fast2":
			nodelay, interval, resend, nc = 1, 20, 2, 1
		case "fast3":
			nodelay, interval, resend, nc = 1, 10, 2, 1
		}
	} else {
		log.Error("kcp: Failed to Apply configure: Manual mode is not supported.(yet!)")
		return errors.New("kcp: Manual Not Implemented")
	}

	kcpvc.hc.SetNoDelay(nodelay, interval, resend, nc)
	kcpvc.hc.SetWindowSize(kcpvc.conf.AdvancedConfigs.Sndwnd, kcpvc.conf.AdvancedConfigs.Rcvwnd)
	kcpvc.hc.SetMtu(kcpvc.conf.AdvancedConfigs.Mtu)
	kcpvc.hc.SetACKNoDelay(kcpvc.conf.AdvancedConfigs.Acknodelay)
	kcpvc.hc.SetDSCP(kcpvc.conf.AdvancedConfigs.Dscp)
	return nil
}

func (kcpvc *KCPVconn) Close() error {

	return kcpvc.hc.Close()
}

func (kcpvc *KCPVconn) LocalAddr() net.Addr {
	return kcpvc.hc.LocalAddr()
}

func (kcpvc *KCPVconn) RemoteAddr() net.Addr {
	return kcpvc.hc.RemoteAddr()
}

func (kcpvc *KCPVconn) SetDeadline(t time.Time) error {
	return kcpvc.hc.SetDeadline(t)
}

func (kcpvc *KCPVconn) SetReadDeadline(t time.Time) error {
	return kcpvc.hc.SetReadDeadline(t)
}

func (kcpvc *KCPVconn) SetWriteDeadline(t time.Time) error {
	return kcpvc.hc.SetWriteDeadline(t)
}

func DialKCP(dest v2net.Destination) (*KCPVconn, error) {
	kcpconf := transport.KcpConfig
	cpip, _ := kcpv.GetChipher(kcpconf.Key)
	kcv, err := kcp.DialWithOptions(kcpconf.AdvancedConfigs.Fec, dest.NetAddr(), cpip)
	if err != nil {
		return nil, err
	}
	kcvn := &KCPVconn{hc: kcv}
	kcvn.conf = kcpconf
	err = kcvn.ApplyConf()
	if err != nil {
		return nil, err
	}
	return kcvn, nil
}
<commit_msg>KCP:func ListenKCP finished<commit_after>package hub

import (
	"errors"
	"net"
	"time"

	"github.com/v2ray/v2ray-core/common/log"
	v2net "github.com/v2ray/v2ray-core/common/net"
	"github.com/v2ray/v2ray-core/transport"
	"github.com/v2ray/v2ray-core/transport/hub/kcpv"
	"github.com/xtaci/kcp-go"
)

type KCPVlistener struct {
	lst  *kcp.Listener
	conf *kcpv.Config
}

func (kvl *KCPVlistener) Accept() (*KCPVconn, error) {
	conn, err := kvl.lst.Accept()
	if err != nil {
		return nil, err
	}

	kcv := &KCPVconn{hc: conn}
	kcv.conf = kvl.conf
	err = kcv.ApplyConf()
	if err != nil {
		return nil, err
	}
	return kcv, nil
}
func (kvl *KCPVlistener) Close() error {
	return kvl.lst.Close()
}
func (kvl *KCPVlistener) Addr() net.Addr {
	return kvl.lst.Addr()
}

type KCPVconn struct {
	hc         *kcp.UDPSession
	conf       *kcpv.Config
	conntokeep time.Time
}

func (kcpvc *KCPVconn) Read(b []byte) (int, error) {
	ifb := time.Now().Add(time.Duration(kcpvc.conf.AdvancedConfigs.ReadTimeout) * time.Second)
	if ifb.After(kcpvc.conntokeep) {
		kcpvc.conntokeep = ifb
	}
	kcpvc.hc.SetDeadline(kcpvc.conntokeep)
	return kcpvc.hc.Read(b)
}

func (kcpvc *KCPVconn) Write(b []byte) (int, error) {
	ifb := time.Now().Add(time.Duration(kcpvc.conf.AdvancedConfigs.WriteTimeout) * time.Second)
	if ifb.After(kcpvc.conntokeep) {
		kcpvc.conntokeep = ifb
	}
	kcpvc.hc.SetDeadline(kcpvc.conntokeep)
	return kcpvc.hc.Write(b)
}
func (kcpvc *KCPVconn) ApplyConf() error {
	nodelay, interval, resend, nc := 0, 40, 0, 0
	if kcpvc.conf.Mode != "manual" {
		switch kcpvc.conf.Mode {
		case "normal":
			nodelay, interval, resend, nc = 0, 30, 2, 1
		case "fast":
			nodelay, interval, resend, nc = 0, 20, 2, 1
		case "fast2":
			nodelay, interval, resend, nc = 1, 20, 2, 1
		case "fast3":
			nodelay, interval, resend, nc = 1, 10, 2, 1
		}
	} else {
		log.Error("kcp: Failed to Apply configure: Manual mode is not supported.(yet!)")
		return errors.New("kcp: Manual Not Implemented")
	}

	kcpvc.hc.SetNoDelay(nodelay, interval, resend, nc)
	kcpvc.hc.SetWindowSize(kcpvc.conf.AdvancedConfigs.Sndwnd, kcpvc.conf.AdvancedConfigs.Rcvwnd)
	kcpvc.hc.SetMtu(kcpvc.conf.AdvancedConfigs.Mtu)
	kcpvc.hc.SetACKNoDelay(kcpvc.conf.AdvancedConfigs.Acknodelay)
	kcpvc.hc.SetDSCP(kcpvc.conf.AdvancedConfigs.Dscp)
	return nil
}

func (kcpvc *KCPVconn) Close() error {

	return kcpvc.hc.Close()
}

func (kcpvc *KCPVconn) LocalAddr() net.Addr {
	return kcpvc.hc.LocalAddr()
}

func (kcpvc *KCPVconn) RemoteAddr() net.Addr {
	return kcpvc.hc.RemoteAddr()
}

func (kcpvc *KCPVconn) SetDeadline(t time.Time) error {
	return kcpvc.hc.SetDeadline(t)
}

func (kcpvc *KCPVconn) SetReadDeadline(t time.Time) error {
	return kcpvc.hc.SetReadDeadline(t)
}

func (kcpvc *KCPVconn) SetWriteDeadline(t time.Time) error {
	return kcpvc.hc.SetWriteDeadline(t)
}

func DialKCP(dest v2net.Destination) (*KCPVconn, error) {
	kcpconf := transport.KcpConfig
	cpip, _ := kcpv.GetChipher(kcpconf.Key)
	kcv, err := kcp.DialWithOptions(kcpconf.AdvancedConfigs.Fec, dest.NetAddr(), cpip)
	if err != nil {
		return nil, err
	}
	kcvn := &KCPVconn{hc: kcv}
	kcvn.conf = kcpconf
	err = kcvn.ApplyConf()
	if err != nil {
		return nil, err
	}
	return kcvn, nil
}

func ListenKCP(address v2net.Address, port v2net.Port) (*KCPVlistener, error) {
	kcpconf := transport.KcpConfig
	cpip, _ := kcpv.GetChipher(kcpconf.Key)
	laddr := address.String() + ":" + port.String()
	kcl, err := kcp.ListenWithOptions(kcpconf.AdvancedConfigs.Fec, laddr, cpip)
	kcvl := &KCPVlistener{lst: kcl, conf: kcpconf}
	return kcvl, err
}
<|endoftext|>


<commit_before>package hrd

import (
	"github.com/101loops/hrd/internal/types"

	ae "appengine"
)

// Query represents a datastore query.
type Query struct {
	inner *types.Query
	ctx   ae.Context
	kind  *Kind
	opts  *types.Opts
}

// newQuery creates a new Query for the passed kind.
// The kind's options are used as default options.
func newQuery(ctx ae.Context, kind *Kind) (ret *Query) {
	return &Query{
		inner: types.NewQuery(kind.name),
		ctx:   ctx,
		kind:  kind,
		opts:  types.DefaultOpts(),
	}
}

func (qry *Query) clone() *Query {
	ret := *qry
	ret.opts = qry.opts.Clone()
	ret.inner = qry.inner.Clone()
	//	if len(qry.log) > 0 {
	//		ret.log = make([]string, len(qry.log))
	//		copy(ret.log, qry.log)
	//	}
	return &ret
}

// NoGlobalCache prevents reading/writing entities from/to memcache.
func (qry *Query) NoGlobalCache() (ret *Query) {
	ret = qry.clone()
	ret.opts.NoGlobalCache = true
	return
}

// Limit returns a derivative Query that has a limit on the number
// of results returned. A negative value means unlimited.
func (qry *Query) Limit(limit int) (ret *Query) {
	ret = qry.clone()
	if limit > 0 {
		//ret.log("LIMIT %v", limit)
	} else {
		limit = -1
		//ret.log("NO LIMIT")
	}
	ret.inner.Limit = limit
	return
}

// NoLimit returns a derivative Query that has no limit on the number
// of results returned.
func (qry *Query) NoLimit() (ret *Query) {
	return qry.Limit(-1)
}

// Ancestor returns a derivative Query with an ancestor filter.
// The ancestor should not be nil.
func (qry *Query) Ancestor(k *Key) (ret *Query) {
	ret = qry.clone()
	//ret.log("ANCESTOR '%v'", k.String())
	ret.inner.Ancestor = k.inner
	return
}

// Project returns a derivative Query that yields only the passed fields.
// It cannot be used in a keys-only query.
func (qry *Query) Project(fields ...string) (ret *Query) {
	ret = qry.clone()
	//ret.log("PROJECT '%v'", strings.Join(fields, "', '"))
	ret.inner.Projection = append([]string(nil), fields...)
	ret.inner.TypeOf = types.ProjectQuery
	return
}

// EventualConsistency returns a derivative query that returns eventually
// consistent results. It only has an effect on ancestor queries.
func (qry *Query) EventualConsistency() (ret *Query) {
	ret = qry.clone()
	//ret.log("EVENTUAL CONSISTENCY")
	ret.inner.Eventual = true
	return
}

// Start returns a derivative Query with the passed start point.
func (qry *Query) Start(c string) (ret *Query) {
	ret = qry.clone()
	//ret.log("START CURSOR")
	ret.inner.Start = c
	return
}

// End returns a derivative Query with the passed end point.
func (qry *Query) End(c string) (ret *Query) {
	ret = qry.clone()
	//ret.log("END CURSOR")
	ret.inner.End = c
	return
}

// Offset returns a derivative Query that has an offset of how many keys
// to skip over before returning results. A negative value is invalid.
func (qry *Query) Offset(off int) (ret *Query) {
	ret = qry.clone()
	//ret.log("OFFSET %v", off)
	ret.inner.Offset = off
	return
}

// OrderAsc returns a derivative Query with a field-based sort order, ascending.
// Orders are applied in the order they are added.
func (qry *Query) OrderAsc(s string) (ret *Query) {
	ret = qry.clone()
	//ret.log("ORDER ASC %v", s)
	ret.inner.Order = append(ret.inner.Order, types.Order{FieldName: s, Descending: false})
	return
}

// OrderDesc returns a derivative Query with a field-based sort order, descending.
// Orders are applied in the order they are added.
func (qry *Query) OrderDesc(s string) (ret *Query) {
	ret = qry.clone()
	//ret.log("ORDER DESC %v", s)
	ret.inner.Order = append(ret.inner.Order, types.Order{FieldName: s, Descending: true})
	return
}

// Distinct returns a derivative query that yields de-duplicated entities with
// respect to the set of projected fields. It is only used for projection
// queries.
func (qry *Query) Distinct() (ret *Query) {
	ret = qry.clone()
	ret.inner.Distinct = true
	return ret
}

// Filter returns a derivative Query with a field-based filter.
// The filterStr argument must be a field name followed by optional space,
// followed by an operator, one of ">", "<", ">=", "<=", or "=".
// Fields are compared against the provided value using the operator.
// Multiple filters are AND'ed together.
func (qry *Query) Filter(q string, val interface{}) (ret *Query) {
	ret = qry.clone()
	ret.inner.Filter = append(ret.inner.Filter, types.Filter{Filter: q, Value: val})
	//ret.log("FILTER '%v %v'", q, val)
	return
}

// GetCount returns the number of results for the query.
func (qry *Query) GetCount() (int, error) {
	//qry.log("COUNT")
	//qry.ctx.Infof(qry.getLog())

	return dsCount(qry.ctx, qry.inner)
}

// GetKeys executes the query as keys-only: No entities are retrieved, just their keys.
func (qry *Query) GetKeys() ([]*Key, string, error) {
	keysQry := qry.clone()
	//keysQry.log("KEYS-ONLY")
	keysQry.inner.TypeOf = types.KeysOnlyQuery

	it := keysQry.Run()

	keys, err := it.GetAll(nil)
	if err != nil {
		return nil, "", err
	}

	cursor, err := it.Cursor()
	return keys, cursor, err
}

// GetAll runs the query and writes the entities to the passed destination.
//
// Note that, if not manually disabled, queries for more than 1 item use
// a "hybrid query". This means that first a keys-only query is executed
// and then the keys are used to lookup the local and global cache as well
// as the datastore eventually. For a warm cache this usually is
// faster and cheaper than the regular query.
func (qry *Query) GetAll(dsts interface{}) ([]*Key, string, error) {
	useHybridQry := qry.inner.Limit != 1 && qry.inner.TypeOf == types.FullQuery && !qry.opts.NoGlobalCache
	if useHybridQry {
		return qry.getAllByHybrid(dsts)
	}

	it := qry.Run()
	keys, err := it.GetAll(dsts)
	if err != nil {
		return nil, "", err
	}
	cursor, err := it.Cursor()
	return keys, cursor, err
}

func (qry *Query) getAllByHybrid(dsts interface{}) ([]*Key, string, error) {
	keys, cursor, err := qry.GetKeys()
	if err == nil && len(keys) > 0 {
		keys, err = newLoader(qry.ctx, qry.kind).Keys(keys).GetAll(dsts)
	}
	return keys, cursor, err
}

// GetFirst executes the query and writes the result's first entity
// to the passed destination.
func (qry *Query) GetFirst(dst interface{}) (*Key, error) {
	return qry.Run().GetOne(dst)
}

// Run executes the query and returns an Iterator.
func (qry *Query) Run() *Iterator {
	//qry.ctx.Infof(qry.getLog())
	return newIterator(qry)
}

//func (qry *Query) log(s string, values ...interface{}) {
//	qry.log = append(qry.log, fmt.Sprintf(s, values...))
//}
//
//func (qry *Query) getLog() string {
//	return fmt.Sprintf("running query \"%v\"", strings.Join(qry.log, " | "))
//}
<commit_msg>formatting<commit_after>package hrd

import (
	"github.com/101loops/hrd/internal/types"

	ae "appengine"
)

// Query represents a datastore query.
type Query struct {
	inner *types.Query
	ctx   ae.Context
	kind  *Kind
	opts  *types.Opts
}

// newQuery creates a new Query for the passed kind.
// The kind's options are used as default options.
func newQuery(ctx ae.Context, kind *Kind) (ret *Query) {
	return &Query{
		inner: types.NewQuery(kind.name),
		ctx:   ctx,
		kind:  kind,
		opts:  types.DefaultOpts(),
	}
}

func (qry *Query) clone() *Query {
	ret := *qry
	ret.opts = qry.opts.Clone()
	ret.inner = qry.inner.Clone()
	//	if len(qry.log) > 0 {
	//		ret.log = make([]string, len(qry.log))
	//		copy(ret.log, qry.log)
	//	}
	return &ret
}

// NoGlobalCache prevents reading/writing entities from/to memcache.
func (qry *Query) NoGlobalCache() (ret *Query) {
	ret = qry.clone()
	ret.opts.NoGlobalCache = true
	return
}

// Limit returns a derivative Query that has a limit on the number
// of results returned. A negative value means unlimited.
func (qry *Query) Limit(limit int) (ret *Query) {
	ret = qry.clone()
	if limit > 0 {
		//ret.log("LIMIT %v", limit)
	} else {
		limit = -1
		//ret.log("NO LIMIT")
	}
	ret.inner.Limit = limit
	return
}

// NoLimit returns a derivative Query that has no limit on the number
// of results returned.
func (qry *Query) NoLimit() (ret *Query) {
	return qry.Limit(-1)
}

// Ancestor returns a derivative Query with an ancestor filter.
// The ancestor should not be nil.
func (qry *Query) Ancestor(k *Key) (ret *Query) {
	ret = qry.clone()
	//ret.log("ANCESTOR '%v'", k.String())
	ret.inner.Ancestor = k.inner
	return
}

// Project returns a derivative Query that yields only the passed fields.
// It cannot be used in a keys-only query.
func (qry *Query) Project(fields ...string) (ret *Query) {
	ret = qry.clone()
	//ret.log("PROJECT '%v'", strings.Join(fields, "', '"))
	ret.inner.Projection = append([]string(nil), fields...)
	ret.inner.TypeOf = types.ProjectQuery
	return
}

// EventualConsistency returns a derivative query that returns eventually
// consistent results. It only has an effect on ancestor queries.
func (qry *Query) EventualConsistency() (ret *Query) {
	ret = qry.clone()
	//ret.log("EVENTUAL CONSISTENCY")
	ret.inner.Eventual = true
	return
}

// Start returns a derivative Query with the passed start point.
func (qry *Query) Start(c string) (ret *Query) {
	ret = qry.clone()
	//ret.log("START CURSOR")
	ret.inner.Start = c
	return
}

// End returns a derivative Query with the passed end point.
func (qry *Query) End(c string) (ret *Query) {
	ret = qry.clone()
	//ret.log("END CURSOR")
	ret.inner.End = c
	return
}

// Offset returns a derivative Query that has an offset of how many keys
// to skip over before returning results. A negative value is invalid.
func (qry *Query) Offset(off int) (ret *Query) {
	ret = qry.clone()
	//ret.log("OFFSET %v", off)
	ret.inner.Offset = off
	return
}

// OrderAsc returns a derivative Query with a field-based sort order, ascending.
// Orders are applied in the order they are added.
func (qry *Query) OrderAsc(s string) (ret *Query) {
	ret = qry.clone()
	//ret.log("ORDER ASC %v", s)
	ret.inner.Order = append(ret.inner.Order, types.Order{FieldName: s, Descending: false})
	return
}

// OrderDesc returns a derivative Query with a field-based sort order, descending.
// Orders are applied in the order they are added.
func (qry *Query) OrderDesc(s string) (ret *Query) {
	ret = qry.clone()
	//ret.log("ORDER DESC %v", s)
	ret.inner.Order = append(ret.inner.Order, types.Order{FieldName: s, Descending: true})
	return
}

// Distinct returns a derivative query that yields de-duplicated entities with
// respect to the set of projected fields. It is only used for projection
// queries.
func (qry *Query) Distinct() (ret *Query) {
	ret = qry.clone()
	ret.inner.Distinct = true
	return ret
}

// Filter returns a derivative Query with a field-based filter.
// The filterStr argument must be a field name followed by optional space,
// followed by an operator, one of ">", "<", ">=", "<=", or "=".
// Fields are compared against the provided value using the operator.
// Multiple filters are AND'ed together.
func (qry *Query) Filter(q string, val interface{}) (ret *Query) {
	ret = qry.clone()
	ret.inner.Filter = append(ret.inner.Filter, types.Filter{Filter: q, Value: val})
	//ret.log("FILTER '%v %v'", q, val)
	return
}

// GetCount returns the number of results for the query.
func (qry *Query) GetCount() (int, error) {
	//qry.log("COUNT")
	//qry.ctx.Infof(qry.getLog())

	return dsCount(qry.ctx, qry.inner)
}

// GetKeys executes the query as keys-only: No entities are retrieved, just their keys.
func (qry *Query) GetKeys() ([]*Key, string, error) {
	keysQry := qry.clone()
	//keysQry.log("KEYS-ONLY")
	keysQry.inner.TypeOf = types.KeysOnlyQuery

	it := keysQry.Run()
	keys, err := it.GetAll(nil)
	if err != nil {
		return nil, "", err
	}
	cursor, err := it.Cursor()
	return keys, cursor, err
}

// GetAll runs the query and writes the entities to the passed destination.
//
// Note that, if not manually disabled, queries for more than 1 item use
// a "hybrid query". This means that first a keys-only query is executed
// and then the keys are used to lookup the local and global cache as well
// as the datastore eventually. For a warm cache this usually is
// faster and cheaper than the regular query.
func (qry *Query) GetAll(dsts interface{}) ([]*Key, string, error) {
	useHybridQry := qry.inner.Limit != 1 && qry.inner.TypeOf == types.FullQuery && !qry.opts.NoGlobalCache
	if useHybridQry {
		return qry.getAllByHybrid(dsts)
	}

	it := qry.Run()
	keys, err := it.GetAll(dsts)
	if err != nil {
		return nil, "", err
	}
	cursor, err := it.Cursor()
	return keys, cursor, err
}

func (qry *Query) getAllByHybrid(dsts interface{}) ([]*Key, string, error) {
	keys, cursor, err := qry.GetKeys()
	if err == nil && len(keys) > 0 {
		keys, err = newLoader(qry.ctx, qry.kind).Keys(keys).GetAll(dsts)
	}
	return keys, cursor, err
}

// GetFirst executes the query and writes the result's first entity
// to the passed destination.
func (qry *Query) GetFirst(dst interface{}) (*Key, error) {
	return qry.Run().GetOne(dst)
}

// Run executes the query and returns an Iterator.
func (qry *Query) Run() *Iterator {
	//qry.ctx.Infof(qry.getLog())
	return newIterator(qry)
}

//func (qry *Query) log(s string, values ...interface{}) {
//	qry.log = append(qry.log, fmt.Sprintf(s, values...))
//}
//
//func (qry *Query) getLog() string {
//	return fmt.Sprintf("running query \"%v\"", strings.Join(qry.log, " | "))
//}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"strings"
)

var cmdQuery = &Command{
	Run:   runSoql,
	Usage: "query <soql statement> [output format]",
	Short: "Execute a SOQL statement",
	Long: `
Execute a SOQL statement

Examples:

  force query select Id, Name, Account.Name From Contact

  force query select Id, Name, Account.Name From Contact --format:csv

`,
}

func runSoql(cmd *Command, args []string) {
	force, _ := ActiveForce()
	if len(args) < 1 {
		cmd.printUsage()
	} else {
		format := "console"
		formatArg := args[len(args)-1]

		if strings.Contains(formatArg, "format:") {
			args = args[:len(args)-1]
			format = strings.SplitN(formatArg, ":", 2)[1]
		}

		soql := strings.Join(args, " ")
		records, err := force.Query(fmt.Sprintf("%s", soql))
		if err != nil {
			ErrorAndExit(err.Error())
		} else {
			if format == "console" {
				DisplayForceRecords(records)
			} else {
				DisplayForceRecordsf(records.Records, format)
			}
		}
	}
}
<commit_msg>fixed soql bug<commit_after>package main

import (
	"fmt"
	"strings"
)

var cmdQuery = &Command{
	Run:   runQuery,
	Usage: "query <soql statement> [output format]",
	Short: "Execute a SOQL statement",
	Long: `
Execute a SOQL statement

Examples:

  force query select Id, Name, Account.Name From Contact

  force query select Id, Name, Account.Name From Contact --format:csv

`,
}

func runQuery(cmd *Command, args []string) {
	force, _ := ActiveForce()
	if len(args) < 1 {
		cmd.printUsage()
	} else {
		format := "console"
		formatArg := args[len(args)-1]

		if strings.Contains(formatArg, "format:") {
			args = args[:len(args)-1]
			format = strings.SplitN(formatArg, ":", 2)[1]
		}

		soql := strings.Join(args, " ")
		records, err := force.Query(fmt.Sprintf("%s", soql))
		if err != nil {
			ErrorAndExit(err.Error())
		} else {
			if format == "console" {
				DisplayForceRecords(records)
			} else {
				DisplayForceRecordsf(records.Records, format)
			}
		}
	}
}
<|endoftext|>


<commit_before>// Copyright 2015 Google Inc. All rights reserved.
//
// 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 martianurl

import (
	"encoding/json"
	"net/http"
	"net/url"

	"github.com/google/martian"
	"github.com/google/martian/parse"
	"github.com/google/martian/verify"
)

var noop = martian.Noop("url.Filter")

func init() {
	parse.Register("url.Filter", filterFromJSON)
}

// Filter runs modifiers iff the request URL matches all of the segments in url.
type Filter struct {
	reqmod martian.RequestModifier
	resmod martian.ResponseModifier
	url    *url.URL
}

type filterJSON struct {
	Scheme   string               `json:"scheme"`
	Host     string               `json:"host"`
	Path     string               `json:"path"`
	Query    string               `json:"query"`
	Modifier json.RawMessage      `json:"modifier"`
	Scope    []parse.ModifierType `json:"scope"`
}

// NewFilter constructs a filter that applies the modifer when the
// request URL matches all of the provided URL segments.
func NewFilter(u *url.URL) *Filter {
	return &Filter{
		url:    u,
		reqmod: noop,
		resmod: noop,
	}
}

// SetRequestModifier sets the request modifier.
func (f *Filter) SetRequestModifier(reqmod martian.RequestModifier) {
	if reqmod == nil {
		reqmod = noop
	}

	f.reqmod = reqmod
}

// SetResponseModifier sets the response modifier.
func (f *Filter) SetResponseModifier(resmod martian.ResponseModifier) {
	if resmod == nil {
		resmod = noop
	}

	f.resmod = resmod
}

// ModifyRequest runs the modifier if the URL matches all provided matchers.
func (f *Filter) ModifyRequest(req *http.Request) error {
	if f.matches(req.URL) {
		return f.reqmod.ModifyRequest(req)
	}

	return nil
}

// ModifyResponse runs the modifier if the request URL matches urlMatcher.
func (f *Filter) ModifyResponse(res *http.Response) error {
	if f.matches(res.Request.URL) {
		return f.resmod.ModifyResponse(res)
	}

	return nil
}

// filterFromJSON takes a JSON message as a byte slice and returns a
// parse.Result that contains a URLFilter and a bitmask that represents the
// type of modifier.
//
// Example JSON configuration message:
// {
//   "scheme": "https",
//   "host": "example.com",
//   "path": "/foo/bar",
//   "rawQuery": "q=value",
//   "scope": ["request", "response"],
//   "modifier": { ... }
// }
func filterFromJSON(b []byte) (*parse.Result, error) {
	msg := &filterJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	filter := NewFilter(&url.URL{
		Scheme:   msg.Scheme,
		Host:     msg.Host,
		Path:     msg.Path,
		RawQuery: msg.Query,
	})

	r, err := parse.FromJSON(msg.Modifier)
	if err != nil {
		return nil, err
	}

	reqmod := r.RequestModifier()
	if err != nil {
		return nil, err
	}
	if reqmod != nil {
		filter.SetRequestModifier(reqmod)
	}

	resmod := r.ResponseModifier()
	if resmod != nil {
		filter.SetResponseModifier(resmod)
	}

	return parse.NewResult(filter, msg.Scope)
}

// matches forces all non-empty URL segments to match or it returns false.
func (f *Filter) matches(u *url.URL) bool {
	switch {
	case f.url.Scheme != "" && f.url.Scheme != u.Scheme:
		return false
	case f.url.Host != "" && !MatchHost(u.Host, f.url.Host):
		return false
	case f.url.Path != "" && f.url.Path != u.Path:
		return false
	case f.url.RawQuery != "" && f.url.RawQuery != u.RawQuery:
		return false
	case f.url.Fragment != "" && f.url.Fragment != u.Fragment:
		return false
	}

	return true
}

// VerifyRequests returns an error containing all the verification errors
// returned by request verifiers.
func (f *Filter) VerifyRequests() error {
	if reqv, ok := f.reqmod.(verify.RequestVerifier); ok {
		return reqv.VerifyRequests()
	}

	return nil
}

// VerifyResponses returns an error containing all the verification errors
// returned by response verifiers.
func (f *Filter) VerifyResponses() error {
	if resv, ok := f.resmod.(verify.ResponseVerifier); ok {
		return resv.VerifyResponses()
	}

	return nil
}

// ResetRequestVerifications resets the state of the contained request verifiers.
func (f *Filter) ResetRequestVerifications() {
	if reqv, ok := f.reqmod.(verify.RequestVerifier); ok {
		reqv.ResetRequestVerifications()
	}
}

// ResetResponseVerifications resets the state of the contained response verifiers.
func (f *Filter) ResetResponseVerifications() {
	if resv, ok := f.resmod.(verify.ResponseVerifier); ok {
		resv.ResetResponseVerifications()
	}
}
<commit_msg>Fix typo in example: RawQuery > query (#111)<commit_after>// Copyright 2015 Google Inc. All rights reserved.
//
// 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 martianurl

import (
	"encoding/json"
	"net/http"
	"net/url"

	"github.com/google/martian"
	"github.com/google/martian/parse"
	"github.com/google/martian/verify"
)

var noop = martian.Noop("url.Filter")

func init() {
	parse.Register("url.Filter", filterFromJSON)
}

// Filter runs modifiers iff the request URL matches all of the segments in url.
type Filter struct {
	reqmod martian.RequestModifier
	resmod martian.ResponseModifier
	url    *url.URL
}

type filterJSON struct {
	Scheme   string               `json:"scheme"`
	Host     string               `json:"host"`
	Path     string               `json:"path"`
	Query    string               `json:"query"`
	Modifier json.RawMessage      `json:"modifier"`
	Scope    []parse.ModifierType `json:"scope"`
}

// NewFilter constructs a filter that applies the modifer when the
// request URL matches all of the provided URL segments.
func NewFilter(u *url.URL) *Filter {
	return &Filter{
		url:    u,
		reqmod: noop,
		resmod: noop,
	}
}

// SetRequestModifier sets the request modifier.
func (f *Filter) SetRequestModifier(reqmod martian.RequestModifier) {
	if reqmod == nil {
		reqmod = noop
	}

	f.reqmod = reqmod
}

// SetResponseModifier sets the response modifier.
func (f *Filter) SetResponseModifier(resmod martian.ResponseModifier) {
	if resmod == nil {
		resmod = noop
	}

	f.resmod = resmod
}

// ModifyRequest runs the modifier if the URL matches all provided matchers.
func (f *Filter) ModifyRequest(req *http.Request) error {
	if f.matches(req.URL) {
		return f.reqmod.ModifyRequest(req)
	}

	return nil
}

// ModifyResponse runs the modifier if the request URL matches urlMatcher.
func (f *Filter) ModifyResponse(res *http.Response) error {
	if f.matches(res.Request.URL) {
		return f.resmod.ModifyResponse(res)
	}

	return nil
}

// filterFromJSON takes a JSON message as a byte slice and returns a
// parse.Result that contains a URLFilter and a bitmask that represents the
// type of modifier.
//
// Example JSON configuration message:
// {
//   "scheme": "https",
//   "host": "example.com",
//   "path": "/foo/bar",
//   "query": "q=value",
//   "scope": ["request", "response"],
//   "modifier": { ... }
// }
func filterFromJSON(b []byte) (*parse.Result, error) {
	msg := &filterJSON{}
	if err := json.Unmarshal(b, msg); err != nil {
		return nil, err
	}

	filter := NewFilter(&url.URL{
		Scheme:   msg.Scheme,
		Host:     msg.Host,
		Path:     msg.Path,
		RawQuery: msg.Query,
	})

	r, err := parse.FromJSON(msg.Modifier)
	if err != nil {
		return nil, err
	}

	reqmod := r.RequestModifier()
	if err != nil {
		return nil, err
	}
	if reqmod != nil {
		filter.SetRequestModifier(reqmod)
	}

	resmod := r.ResponseModifier()
	if resmod != nil {
		filter.SetResponseModifier(resmod)
	}

	return parse.NewResult(filter, msg.Scope)
}

// matches forces all non-empty URL segments to match or it returns false.
func (f *Filter) matches(u *url.URL) bool {
	switch {
	case f.url.Scheme != "" && f.url.Scheme != u.Scheme:
		return false
	case f.url.Host != "" && !MatchHost(u.Host, f.url.Host):
		return false
	case f.url.Path != "" && f.url.Path != u.Path:
		return false
	case f.url.RawQuery != "" && f.url.RawQuery != u.RawQuery:
		return false
	case f.url.Fragment != "" && f.url.Fragment != u.Fragment:
		return false
	}

	return true
}

// VerifyRequests returns an error containing all the verification errors
// returned by request verifiers.
func (f *Filter) VerifyRequests() error {
	if reqv, ok := f.reqmod.(verify.RequestVerifier); ok {
		return reqv.VerifyRequests()
	}

	return nil
}

// VerifyResponses returns an error containing all the verification errors
// returned by response verifiers.
func (f *Filter) VerifyResponses() error {
	if resv, ok := f.resmod.(verify.ResponseVerifier); ok {
		return resv.VerifyResponses()
	}

	return nil
}

// ResetRequestVerifications resets the state of the contained request verifiers.
func (f *Filter) ResetRequestVerifications() {
	if reqv, ok := f.reqmod.(verify.RequestVerifier); ok {
		reqv.ResetRequestVerifications()
	}
}

// ResetResponseVerifications resets the state of the contained response verifiers.
func (f *Filter) ResetResponseVerifications() {
	if resv, ok := f.resmod.(verify.ResponseVerifier); ok {
		resv.ResetResponseVerifications()
	}
}
<|endoftext|>


<commit_before>package channels

const minQueueLen = 8

// A fast, ring-buffer queue based on the version suggested by Dariusz Górecki.
// Using this instead of a simple slice+append provides substantial memory and time
// benefits, and fewer GC pauses.
type queue struct {
	buf []interface{}
	head, tail, count int
}

func newQueue() *queue {
	return &queue{buf: make([]interface{}, minQueueLen)}
}

func (q *queue) length() int {
	return q.count
}

func (q *queue) resize() {
	newBuf := make([]interface{}, q.count*2)

	stop := len(q.buf)
	if (q.tail > q.head) {
		stop = q.tail
	}
	copy(newBuf, q.buf[q.head:stop])
	copy(newBuf[stop-q.head:], q.buf[:q.head])

	q.head = 0
	q.tail = q.count
	q.buf = newBuf
}

func (q *queue) enqueue(elem interface{}) {
	if q.count == len(q.buf) {
		q.resize()
	}

	q.buf[q.tail] = elem
	q.tail = (q.tail+1) % len(q.buf)
	q.count++
}

func (q *queue) peek() interface{} {
	return q.buf[q.head]
}

func (q *queue) dequeue() {
	q.head = (q.head+1) % len(q.buf)
	q.count--
	if len(q.buf) > minQueueLen && q.count*3 < len(q.buf) {
		q.resize()
	}
}
<commit_msg>Be slightly less agressive in shrinking the queue.<commit_after>package channels

const minQueueLen = 8

// A fast, ring-buffer queue based on the version suggested by Dariusz Górecki.
// Using this instead of a simple slice+append provides substantial memory and time
// benefits, and fewer GC pauses.
type queue struct {
	buf []interface{}
	head, tail, count int
}

func newQueue() *queue {
	return &queue{buf: make([]interface{}, minQueueLen)}
}

func (q *queue) length() int {
	return q.count
}

func (q *queue) resize() {
	newBuf := make([]interface{}, q.count*2)

	stop := len(q.buf)
	if (q.tail > q.head) {
		stop = q.tail
	}
	copy(newBuf, q.buf[q.head:stop])
	copy(newBuf[stop-q.head:], q.buf[:q.head])

	q.head = 0
	q.tail = q.count
	q.buf = newBuf
}

func (q *queue) enqueue(elem interface{}) {
	if q.count == len(q.buf) {
		q.resize()
	}

	q.buf[q.tail] = elem
	q.tail = (q.tail+1) % len(q.buf)
	q.count++
}

func (q *queue) peek() interface{} {
	return q.buf[q.head]
}

func (q *queue) dequeue() {
	q.head = (q.head+1) % len(q.buf)
	q.count--
	if len(q.buf) > minQueueLen && q.count*4 < len(q.buf) {
		q.resize()
	}
}
<|endoftext|>


<commit_before>package crawl

// Job - Crawl job interface.
type Job interface {
	// Request - Returns crawl job.
	Request() *Request

	// Done - Sets job as done.
	Done()
}

// Queue - Requests queue.
type Queue interface {
	// Get - Gets request from Queue channel.
	// Returns io.EOF if queue is done/closed.
	Get() (job Job, err error)

	// Schedule - Schedules a Request.
	// Returns io.ErrClosedPipe if queue is closed.
	Schedule(job Job) error

	// Close - Closes the queue.
	Close() error
}
<commit_msg>Queue cleaner (docs)<commit_after>package crawl

// Job - Crawl job interface.
type Job interface {
	// Request - Returns crawl job.
	Request() *Request

	// Done - Sets job as done.
	Done()
}

// Queue - Requests queue.
type Queue interface {
	// Get - Gets request from Queue channel.
	// Returns io.EOF if queue is done/closed.
	Get() (Job, error)

	// Schedule - Schedules a Request.
	// Returns io.ErrClosedPipe if queue is closed.
	Schedule(Job) error

	// Close - Closes the queue.
	Close() error
}
<|endoftext|>


<commit_before>package flow

import (
	"fmt"
	"reflect"
)

// getInPort returns the inport with given name as reflect.Value channel.
func (n *Graph) getInPort(name string) (reflect.Value, error) {
	pName, ok := n.inPorts[name]
	if !ok {
		return reflect.ValueOf(nil), fmt.Errorf("Inport not found: '%s'", name)
	}
	return pName.channel, nil
}

// getOutPort returns the outport with given name as reflect.Value channel.
func (n *Graph) getOutPort(name string) (reflect.Value, error) {
	pName, ok := n.outPorts[name]
	if !ok {
		return reflect.ValueOf(nil), fmt.Errorf("Outport not found: '%s'", name)
	}
	return pName.channel, nil
}

// MapInPort adds an inport to the net and maps it to a contained proc's port.
func (n *Graph) MapInPort(name, procName, procPort string) error {
	var channel reflect.Value
	var err error
	if p, procFound := n.procs[procName]; procFound {
		if g, isNet := p.(*Graph); isNet {
			// Is a subnet
			channel, err = g.getInPort(procPort)
		} else {
			// Is a proc
			channel, err = n.getProcPort(procName, procPort, reflect.RecvDir)
		}
	} else {
		return fmt.Errorf("Could not map inport: process '%s' not found", procName)
	}
	if err == nil {
		n.inPorts[name] = port{proc: procName, port: procPort, channel: channel}
	}
	return err
}

// // AnnotateInPort sets optional run-time annotation for the port utilized by
// // runtimes and FBP protocol clients.
// func (n *Graph) AnnotateInPort(name string, info PortInfo) bool {
// 	port, exists := n.inPorts[name]
// 	if !exists {
// 		return false
// 	}
// 	port.info = info
// 	return true
// }

// // UnmapInPort removes an existing inport mapping
// func (n *Graph) UnmapInPort(name string) bool {
// 	if _, exists := n.inPorts[name]; !exists {
// 		return false
// 	}
// 	delete(n.inPorts, name)
// 	return true
// }

// MapOutPort adds an outport to the net and maps it to a contained proc's port.
func (n *Graph) MapOutPort(name, procName, procPort string) error {
	var channel reflect.Value
	var err error
	if p, procFound := n.procs[procName]; procFound {
		if g, isNet := p.(*Graph); isNet {
			// Is a subnet
			channel, err = g.getOutPort(procPort)
		} else {
			// Is a proc
			channel, err = n.getProcPort(procName, procPort, reflect.SendDir)
		}
	} else {
		return fmt.Errorf("Could not map outport: process '%s' not found", procName)
	}
	if err == nil {
		n.outPorts[name] = port{proc: procName, port: procPort, channel: channel}
	}
	return err
}

// // AnnotateOutPort sets optional run-time annotation for the port utilized by
// // runtimes and FBP protocol clients.
// func (n *Graph) AnnotateOutPort(name string, info PortInfo) bool {
// 	port, exists := n.outPorts[name]
// 	if !exists {
// 		return false
// 	}
// 	port.info = info
// 	return true
// }

// // UnmapOutPort removes an existing outport mapping
// func (n *Graph) UnmapOutPort(name string) bool {
// 	if _, exists := n.outPorts[name]; !exists {
// 		return false
// 	}
// 	delete(n.outPorts, name)
// 	return true
// }

// // SetInPort assigns a channel to a network's inport to talk to the outer world.
// // It returns true on success or false if the inport cannot be set.
// func (n *Graph) SetInPort(name string, channel interface{}) bool {
// 	res := false
// 	// Get the component's inport associated
// 	p := n.getInPort(name)
// 	// Try to set it
// 	if p.CanSet() {
// 		p.Set(reflect.ValueOf(channel))
// 		res = true
// 	}
// 	// Save it in inPorts to be used with IIPs if needed
// 	if p, ok := n.inPorts[name]; ok {
// 		p.channel = reflect.ValueOf(channel)
// 		n.inPorts[name] = p
// 	}
// 	return res
// }

// // RenameInPort changes graph's inport name
// func (n *Graph) RenameInPort(oldName, newName string) bool {
// 	if _, exists := n.inPorts[oldName]; !exists {
// 		return false
// 	}
// 	n.inPorts[newName] = n.inPorts[oldName]
// 	delete(n.inPorts, oldName)
// 	return true
// }

// // UnsetInPort removes an external inport from the graph
// func (n *Graph) UnsetInPort(name string) bool {
// 	port, exists := n.inPorts[name]
// 	if !exists {
// 		return false
// 	}
// 	if proc, ok := n.procs[port.proc]; ok {
// 		unsetProcPort(proc, port.port, false)
// 	}
// 	delete(n.inPorts, name)
// 	return true
// }

// // SetOutPort assigns a channel to a network's outport to talk to the outer world.
// // It returns true on success or false if the outport cannot be set.
// func (n *Graph) SetOutPort(name string, channel interface{}) bool {
// 	res := false
// 	// Get the component's outport associated
// 	p := n.getOutPort(name)
// 	// Try to set it
// 	if p.CanSet() {
// 		p.Set(reflect.ValueOf(channel))
// 		res = true
// 	}
// 	// Save it in outPorts to be used later
// 	if p, ok := n.outPorts[name]; ok {
// 		p.channel = reflect.ValueOf(channel)
// 		n.outPorts[name] = p
// 	}
// 	return res
// }

// // RenameOutPort changes graph's outport name
// func (n *Graph) RenameOutPort(oldName, newName string) bool {
// 	if _, exists := n.outPorts[oldName]; !exists {
// 		return false
// 	}
// 	n.outPorts[newName] = n.outPorts[oldName]
// 	delete(n.outPorts, oldName)
// 	return true
// }

// // UnsetOutPort removes an external outport from the graph
// func (n *Graph) UnsetOutPort(name string) bool {
// 	port, exists := n.outPorts[name]
// 	if !exists {
// 		return false
// 	}
// 	if proc, ok := n.procs[port.proc]; ok {
// 		unsetProcPort(proc, port.proc, true)
// 	}
// 	delete(n.outPorts, name)
// 	return true
// }
<commit_msg>Update port setting code<commit_after>package flow

import (
	"fmt"
	"reflect"
)

// getInPort returns the inport with given name as reflect.Value channel.
func (n *Graph) getInPort(name string) (reflect.Value, error) {
	pName, ok := n.inPorts[name]
	if !ok {
		return reflect.ValueOf(nil), fmt.Errorf("Inport not found: '%s'", name)
	}
	return pName.channel, nil
}

// getOutPort returns the outport with given name as reflect.Value channel.
func (n *Graph) getOutPort(name string) (reflect.Value, error) {
	pName, ok := n.outPorts[name]
	if !ok {
		return reflect.ValueOf(nil), fmt.Errorf("Outport not found: '%s'", name)
	}
	return pName.channel, nil
}

// MapInPort adds an inport to the net and maps it to a contained proc's port.
func (n *Graph) MapInPort(name, procName, procPort string) error {
	var channel reflect.Value
	var err error
	if p, procFound := n.procs[procName]; procFound {
		if g, isNet := p.(*Graph); isNet {
			// Is a subnet
			channel, err = g.getInPort(procPort)
		} else {
			// Is a proc
			channel, err = n.getProcPort(procName, procPort, reflect.RecvDir)
		}
	} else {
		return fmt.Errorf("Could not map inport: process '%s' not found", procName)
	}
	if err == nil {
		n.inPorts[name] = port{proc: procName, port: procPort, channel: channel}
	}
	return err
}

// // AnnotateInPort sets optional run-time annotation for the port utilized by
// // runtimes and FBP protocol clients.
// func (n *Graph) AnnotateInPort(name string, info PortInfo) bool {
// 	port, exists := n.inPorts[name]
// 	if !exists {
// 		return false
// 	}
// 	port.info = info
// 	return true
// }

// // UnmapInPort removes an existing inport mapping
// func (n *Graph) UnmapInPort(name string) bool {
// 	if _, exists := n.inPorts[name]; !exists {
// 		return false
// 	}
// 	delete(n.inPorts, name)
// 	return true
// }

// MapOutPort adds an outport to the net and maps it to a contained proc's port.
func (n *Graph) MapOutPort(name, procName, procPort string) error {
	var channel reflect.Value
	var err error
	if p, procFound := n.procs[procName]; procFound {
		if g, isNet := p.(*Graph); isNet {
			// Is a subnet
			channel, err = g.getOutPort(procPort)
		} else {
			// Is a proc
			channel, err = n.getProcPort(procName, procPort, reflect.SendDir)
		}
	} else {
		return fmt.Errorf("Could not map outport: process '%s' not found", procName)
	}
	if err == nil {
		n.outPorts[name] = port{proc: procName, port: procPort, channel: channel}
	}
	return err
}

// // AnnotateOutPort sets optional run-time annotation for the port utilized by
// // runtimes and FBP protocol clients.
// func (n *Graph) AnnotateOutPort(name string, info PortInfo) bool {
// 	port, exists := n.outPorts[name]
// 	if !exists {
// 		return false
// 	}
// 	port.info = info
// 	return true
// }

// // UnmapOutPort removes an existing outport mapping
// func (n *Graph) UnmapOutPort(name string) bool {
// 	if _, exists := n.outPorts[name]; !exists {
// 		return false
// 	}
// 	delete(n.outPorts, name)
// 	return true
// }

// SetInPort assigns a channel to a network's inport to talk to the outer world.
func (n *Graph) SetInPort(name string, channel interface{}) error {
	// Get the component's inport associated
	p, err := n.getInPort(name)
	if err != nil {
		return err
	}
	// Try to set it
	if p.CanSet() {
		p.Set(reflect.ValueOf(channel))
	} else {
		return fmt.Errorf("Cannot set graph inport: '%s'", name)
	}
	// Save it in inPorts to be used with IIPs if needed
	if p, ok := n.inPorts[name]; ok {
		p.channel = reflect.ValueOf(channel)
		n.inPorts[name] = p
	}
	return nil
}

// // RenameInPort changes graph's inport name
// func (n *Graph) RenameInPort(oldName, newName string) bool {
// 	if _, exists := n.inPorts[oldName]; !exists {
// 		return false
// 	}
// 	n.inPorts[newName] = n.inPorts[oldName]
// 	delete(n.inPorts, oldName)
// 	return true
// }

// // UnsetInPort removes an external inport from the graph
// func (n *Graph) UnsetInPort(name string) bool {
// 	port, exists := n.inPorts[name]
// 	if !exists {
// 		return false
// 	}
// 	if proc, ok := n.procs[port.proc]; ok {
// 		unsetProcPort(proc, port.port, false)
// 	}
// 	delete(n.inPorts, name)
// 	return true
// }

// SetOutPort assigns a channel to a network's outport to talk to the outer world.
// It returns true on success or false if the outport cannot be set.
func (n *Graph) SetOutPort(name string, channel interface{}) error {
	// Get the component's outport associated
	p, err := n.getOutPort(name)
	if err != nil {
		return err
	}
	// Try to set it
	if p.CanSet() {
		p.Set(reflect.ValueOf(channel))
	} else {
		return fmt.Errorf("Cannot set graph outport: '%s'", name)
	}
	// Save it in outPorts to be used later
	if p, ok := n.outPorts[name]; ok {
		p.channel = reflect.ValueOf(channel)
		n.outPorts[name] = p
	}
	return nil
}

// // RenameOutPort changes graph's outport name
// func (n *Graph) RenameOutPort(oldName, newName string) bool {
// 	if _, exists := n.outPorts[oldName]; !exists {
// 		return false
// 	}
// 	n.outPorts[newName] = n.outPorts[oldName]
// 	delete(n.outPorts, oldName)
// 	return true
// }

// // UnsetOutPort removes an external outport from the graph
// func (n *Graph) UnsetOutPort(name string) bool {
// 	port, exists := n.outPorts[name]
// 	if !exists {
// 		return false
// 	}
// 	if proc, ok := n.procs[port.proc]; ok {
// 		unsetProcPort(proc, port.proc, true)
// 	}
// 	delete(n.outPorts, name)
// 	return true
// }
<|endoftext|>


<commit_before>package template

const Etcd3AttachDepService = `
[Unit]
Description=Attach etcd dependencies
Requires=network.target
After=network.target

[Service]
# image is from https://github.com/giantswarm/aws-attach-etcd-dep
Environment="IMAGE={{ .RegistryDomain }}/giantswarm/aws-attach-etcd-dep:5a1d732d48bea22c174825dc5ab9f403a1b7d27c"
Environment="NAME=%p.service"
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c "docker run --rm -i \
      -v /dev:/dev \
      -v /etc/systemd/network:/etc/systemd/network \
      --privileged \
      --name ${NAME} \
      ${IMAGE} \
      --eni-device-index=1 \
      --eni-tag-key=Name \
      --eni-tag-value={{ .MasterENIName }} \
      --volume-device-name=/dev/xvdh \
      --volume-device-filesystem-type=ext4 \
      --volume-device-label=etcd \
      --volume-tag-key=Name \
      --volume-tag-value={{ .MasterEtcdVolumeName }}"
ExecStartPost=/usr/bin/systemctl daemon-reload
ExecStartPost=/usr/bin/systemctl restart systemd-networkd

[Install]
WantedBy=multi-user.target
`
<commit_msg>'adjust-aws-attach-dep-image' (#2437)<commit_after>package template

const Etcd3AttachDepService = `
[Unit]
Description=Attach etcd dependencies
Requires=network.target
After=network.target

[Service]
# image is from https://github.com/giantswarm/aws-attach-etcd-dep
Environment="IMAGE={{ .RegistryDomain }}/giantswarm/aws-attach-etcd-dep:b49bd45ca0138e51270bb4ef726d7f646c1e5c21"
Environment="NAME=%p.service"
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c "docker run --rm -i \
      -v /dev:/dev \
      -v /etc/systemd/network:/etc/systemd/network \
      --privileged \
      --name ${NAME} \
      ${IMAGE} \
      --eni-device-index=1 \
      --eni-tag-key=Name \
      --eni-tag-value={{ .MasterENIName }} \
      --volume-device-name=/dev/xvdh \
      --volume-device-filesystem-type=ext4 \
      --volume-device-label=etcd \
      --volume-tag-key=Name \
      --volume-tag-value={{ .MasterEtcdVolumeName }}"
ExecStartPost=/usr/bin/systemctl daemon-reload
ExecStartPost=/usr/bin/systemctl restart systemd-networkd

[Install]
WantedBy=multi-user.target
`
<|endoftext|>


<commit_before>package db

import (
	"bytes"

	"github.com/azmodb/llrb"
)

func rangeFunc(end []byte, rev, cur int64, vers bool, fn RangeFunc) llrb.Visitor {
	return func(elem llrb.Element) bool {
		p := elem.(*pair)
		if p.isDeleted() {
			return false
		}

		if end != nil && bytes.Compare(p.Key, end) >= 0 {
			return true
		}

		var rec *Record
		if rev > 0 {
			index, found := p.find(rev, false)
			if !found { // revision not found
				return false
			}
			if vers {
				rec = p.from(index, cur)
			} else {
				rec = p.last(cur)
			}
		} else {
			if vers {
				rec = p.from(0, cur)
			} else {
				rec = p.last(cur)
			}
		}
		return fn(p.Key, rec)
	}
}

// Range perform fn on all values stored in the tree over the interval
// [from, to) from left to right.
// If from is nil and to is nil it gets the keys in range [first, last].
// If from is nil and to is not nil it gets the keys in range
// [first, to].
// If from is not nil and to is not nil it gets the keys in range
// [from, to).
func (db *DB) Range(from, to []byte, rev int64, vers bool, fn RangeFunc) {
	tree := db.load()
	if from == nil && to == nil {
		tree.root.ForEach(rangeFunc(nil, rev, tree.rev, vers, fn))
		return
	}
	if from == nil && to != nil {
		tree.root.ForEach(rangeFunc(to, rev, tree.rev, vers, fn))
		return
	}

	switch cmp := bytes.Compare(from, to); {
	case cmp == 0: // invalid key sarch query range, report nothing
		return
	case cmp > 0: // invalid key sarch query range, report nothing
		return
	}

	fmatch, tmatch := newMatcher(from), newMatcher(to)
	defer func() {
		fmatch.Close()
		tmatch.Close()
	}()

	tree.root.Range(fmatch, tmatch, rangeFunc(nil, rev, tree.rev, vers, fn))
}

// RangeFunc is a function that operates on a key/value pair. If done is
// returned true, the RangeFunc is indicating that no further work needs
// to be done and so the traversal function should traverse no further.
// The key must be kept immutable.
type RangeFunc func(key []byte, rec *Record) (done bool)

// Get retrieves the value for a key at revision rev. If rev <= 0 Get
// returns the current value for a key.
func (db *DB) Get(key []byte, rev int64, vers bool) (rec *Record, err error) {
	rec, err = db.get(key, rev, vers)
	return rec, err
}

func (db *DB) get(key []byte, rev int64, vers bool) (*Record, error) {
	match := newMatcher(key)
	defer match.Close()
	tree := db.load()

	if elem := tree.root.Get(match); elem != nil {
		p := elem.(*pair)
		if p.isDeleted() {
			return nil, errKeyNotFound
		}

		var rec *Record
		if rev > 0 {
			index, found := p.find(rev, true)
			if !found {
				return nil, errRevisionNotFound
			}
			if vers {
				rec = p.from(index, tree.rev)
			} else {
				rec = p.at(index, tree.rev)
			}
		} else {
			if vers {
				rec = p.from(0, tree.rev)
			} else {
				rec = p.last(tree.rev)
			}
		}
		return rec, nil
	}
	return nil, errKeyNotFound
}
<commit_msg>inclusive range interval<commit_after>package db

import (
	"bytes"

	"github.com/azmodb/llrb"
)

func rangeFunc(end []byte, rev, cur int64, vers bool, fn RangeFunc) llrb.Visitor {
	return func(elem llrb.Element) bool {
		p := elem.(*pair)
		if p.isDeleted() {
			return false
		}

		if end != nil && bytes.Compare(p.Key, end) >= 0 {
			return true
		}

		var rec *Record
		if rev > 0 {
			index, found := p.find(rev, false)
			if !found { // revision not found
				return false
			}
			if vers {
				rec = p.from(index, cur)
			} else {
				rec = p.last(cur)
			}
		} else {
			if vers {
				rec = p.from(0, cur)
			} else {
				rec = p.last(cur)
			}
		}
		return fn(p.Key, rec)
	}
}

// Range perform fn on all values stored in the tree over the interval
// [from, to] from left to right.
// If from is nil and to is nil it gets the keys in range [first, last].
// If from is nil and to is not nil it gets the keys in range
// [first, to].
// If from is not nil and to is not nil it gets the keys in range
// [from, to].
func (db *DB) Range(from, to []byte, rev int64, vers bool, fn RangeFunc) {
	tree := db.load()
	if from == nil && to == nil {
		tree.root.ForEach(rangeFunc(nil, rev, tree.rev, vers, fn))
		return
	}
	if from == nil && to != nil {
		tree.root.ForEach(rangeFunc(to, rev, tree.rev, vers, fn))
		return
	}

	switch cmp := bytes.Compare(from, to); {
	case cmp == 0: // invalid key sarch query range, report nothing
		return
	case cmp > 0: // invalid key sarch query range, report nothing
		return
	}

	fmatch, tmatch := newMatcher(from), newMatcher(to)
	defer func() {
		fmatch.Close()
		tmatch.Close()
	}()

	tree.root.Range(fmatch, tmatch, rangeFunc(nil, rev, tree.rev, vers, fn))
}

// RangeFunc is a function that operates on a key/value pair. If done is
// returned true, the RangeFunc is indicating that no further work needs
// to be done and so the traversal function should traverse no further.
// The key must be kept immutable.
type RangeFunc func(key []byte, rec *Record) (done bool)

// Get retrieves the value for a key at revision rev. If rev <= 0 Get
// returns the current value for a key.
func (db *DB) Get(key []byte, rev int64, vers bool) (rec *Record, err error) {
	rec, err = db.get(key, rev, vers)
	return rec, err
}

func (db *DB) get(key []byte, rev int64, vers bool) (*Record, error) {
	match := newMatcher(key)
	defer match.Close()
	tree := db.load()

	if elem := tree.root.Get(match); elem != nil {
		p := elem.(*pair)
		if p.isDeleted() {
			return nil, errKeyNotFound
		}

		var rec *Record
		if rev > 0 {
			index, found := p.find(rev, true)
			if !found {
				return nil, errRevisionNotFound
			}
			if vers {
				rec = p.from(index, tree.rev)
			} else {
				rec = p.at(index, tree.rev)
			}
		} else {
			if vers {
				rec = p.from(0, tree.rev)
			} else {
				rec = p.last(tree.rev)
			}
		}
		return rec, nil
	}
	return nil, errKeyNotFound
}
<|endoftext|>


<commit_before>package vtrace_test

import (
	"bytes"
	"encoding/binary"
	"testing"
	"time"

	"v.io/core/veyron2/uniqueid"
	"v.io/core/veyron2/vtrace"
)

var nextid = uint64(1)

func id() uniqueid.ID {
	var out uniqueid.ID
	binary.BigEndian.PutUint64(out[8:], nextid)
	nextid++
	return out
}

func TestFormat(t *testing.T) {
	trid := id()
	trstart := time.Date(2014, 11, 6, 13, 1, 22, 400000000, time.UTC)
	spanIDs := make([]uniqueid.ID, 4)
	for i := range spanIDs {
		spanIDs[i] = id()
	}
	tr := vtrace.TraceRecord{
		ID: trid,
		Spans: []vtrace.SpanRecord{
			{
				ID:     spanIDs[0],
				Parent: trid,
				Name:   "",
				Start:  trstart.UnixNano(),
			},
			{
				ID:     spanIDs[1],
				Parent: spanIDs[0],
				Name:   "Child1",
				Start:  trstart.Add(time.Second).UnixNano(),
				End:    trstart.Add(10 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[2],
				Parent: spanIDs[0],
				Name:   "Child2",
				Start:  trstart.Add(20 * time.Second).UnixNano(),
				End:    trstart.Add(30 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[3],
				Parent: spanIDs[1],
				Name:   "GrandChild1",
				Start:  trstart.Add(3 * time.Second).UnixNano(),
				End:    trstart.Add(8 * time.Second).UnixNano(),
				Annotations: []vtrace.Annotation{
					{
						Message: "First Annotation",
						When:    trstart.Add(4 * time.Second).UnixNano(),
					},
					{
						Message: "Second Annotation",
						When:    trstart.Add(6 * time.Second).UnixNano(),
					},
				},
			},
		},
	}

	var buf bytes.Buffer
	vtrace.FormatTrace(&buf, &tr, time.UTC)
	want := `Trace - 00000000000000000000000000000001 (2014-11-06 13:01:22.400000 UTC, ??)
    Span - Child1 [id: 00000003 parent 00000002] (1s, 10s)
        Span - GrandChild1 [id: 00000005 parent 00000003] (3s, 8s)
            @4s First Annotation
            @6s Second Annotation
    Span - Child2 [id: 00000004 parent 00000002] (20s, 30s)
`
	if got := buf.String(); got != want {
		t.Errorf("Incorrect output, want\n%sgot\n%s", want, got)
	}
}

func TestFormatWithMissingSpans(t *testing.T) {
	trid := id()
	trstart := time.Date(2014, 11, 6, 13, 1, 22, 400000000, time.UTC)
	spanIDs := make([]uniqueid.ID, 6)
	for i := range spanIDs {
		spanIDs[i] = id()
	}
	tr := vtrace.TraceRecord{
		ID: trid,
		Spans: []vtrace.SpanRecord{
			{
				ID:     spanIDs[0],
				Parent: trid,
				Name:   "",
				Start:  trstart.UnixNano(),
			},
			{
				ID:     spanIDs[1],
				Parent: spanIDs[0],
				Name:   "Child1",
				Start:  trstart.Add(time.Second).UnixNano(),
				End:    trstart.Add(10 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[3],
				Parent: spanIDs[2],
				Name:   "Decendant2",
				Start:  trstart.Add(15 * time.Second).UnixNano(),
				End:    trstart.Add(24 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[4],
				Parent: spanIDs[2],
				Name:   "Decendant1",
				Start:  trstart.Add(12 * time.Second).UnixNano(),
				End:    trstart.Add(18 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[5],
				Parent: spanIDs[1],
				Name:   "GrandChild1",
				Start:  trstart.Add(3 * time.Second).UnixNano(),
				End:    trstart.Add(8 * time.Second).UnixNano(),
				Annotations: []vtrace.Annotation{
					{
						Message: "Second Annotation",
						When:    trstart.Add(6 * time.Second).UnixNano(),
					},
					{
						Message: "First Annotation",
						When:    trstart.Add(4 * time.Second).UnixNano(),
					},
				},
			},
		},
	}

	var buf bytes.Buffer
	vtrace.FormatTrace(&buf, &tr, time.UTC)
	want := `Trace - 00000000000000000000000000000006 (2014-11-06 13:01:22.400000 UTC, ??)
    Span - Child1 [id: 00000008 parent 00000007] (1s, 10s)
        Span - GrandChild1 [id: 0000000c parent 00000008] (3s, 8s)
            @4s First Annotation
            @6s Second Annotation
    Span - Missing Data [id: 00000000 parent 00000000] (??, ??)
        Span - Decendant1 [id: 0000000b parent 00000009] (12s, 18s)
        Span - Decendant2 [id: 0000000a parent 00000009] (15s, 24s)
`

	if got := buf.String(); got != want {
		t.Errorf("Incorrect output, want\n%sgot\n%s", want, got)
	}
}
<commit_msg>TBR: veyron2/vtrace: fix test broken by go/vcl/2720<commit_after>package vtrace_test

import (
	"bytes"
	"encoding/binary"
	"testing"
	"time"

	"v.io/core/veyron2/uniqueid"
	"v.io/core/veyron2/vtrace"
)

var nextid = uint64(1)

func id() uniqueid.ID {
	var out uniqueid.ID
	binary.BigEndian.PutUint64(out[8:], nextid)
	nextid++
	return out
}

func TestFormat(t *testing.T) {
	trid := id()
	trstart := time.Date(2014, 11, 6, 13, 1, 22, 400000000, time.UTC)
	spanIDs := make([]uniqueid.ID, 4)
	for i := range spanIDs {
		spanIDs[i] = id()
	}
	tr := vtrace.TraceRecord{
		ID: trid,
		Spans: []vtrace.SpanRecord{
			{
				ID:     spanIDs[0],
				Parent: trid,
				Name:   "",
				Start:  trstart.UnixNano(),
			},
			{
				ID:     spanIDs[1],
				Parent: spanIDs[0],
				Name:   "Child1",
				Start:  trstart.Add(time.Second).UnixNano(),
				End:    trstart.Add(10 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[2],
				Parent: spanIDs[0],
				Name:   "Child2",
				Start:  trstart.Add(20 * time.Second).UnixNano(),
				End:    trstart.Add(30 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[3],
				Parent: spanIDs[1],
				Name:   "GrandChild1",
				Start:  trstart.Add(3 * time.Second).UnixNano(),
				End:    trstart.Add(8 * time.Second).UnixNano(),
				Annotations: []vtrace.Annotation{
					{
						Message: "First Annotation",
						When:    trstart.Add(4 * time.Second).UnixNano(),
					},
					{
						Message: "Second Annotation",
						When:    trstart.Add(6 * time.Second).UnixNano(),
					},
				},
			},
		},
	}

	var buf bytes.Buffer
	vtrace.FormatTrace(&buf, &tr, time.UTC)
	want := `Trace - 30783030303030303030303030303030303030303030303030303030303030303031 (2014-11-06 13:01:22.400000 UTC, ??)
    Span - Child1 [id: 00000003 parent 00000002] (1s, 10s)
        Span - GrandChild1 [id: 00000005 parent 00000003] (3s, 8s)
            @4s First Annotation
            @6s Second Annotation
    Span - Child2 [id: 00000004 parent 00000002] (20s, 30s)
`
	if got := buf.String(); got != want {
		t.Errorf("Incorrect output, want\n%sgot\n%s", want, got)
	}
}

func TestFormatWithMissingSpans(t *testing.T) {
	trid := id()
	trstart := time.Date(2014, 11, 6, 13, 1, 22, 400000000, time.UTC)
	spanIDs := make([]uniqueid.ID, 6)
	for i := range spanIDs {
		spanIDs[i] = id()
	}
	tr := vtrace.TraceRecord{
		ID: trid,
		Spans: []vtrace.SpanRecord{
			{
				ID:     spanIDs[0],
				Parent: trid,
				Name:   "",
				Start:  trstart.UnixNano(),
			},
			{
				ID:     spanIDs[1],
				Parent: spanIDs[0],
				Name:   "Child1",
				Start:  trstart.Add(time.Second).UnixNano(),
				End:    trstart.Add(10 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[3],
				Parent: spanIDs[2],
				Name:   "Decendant2",
				Start:  trstart.Add(15 * time.Second).UnixNano(),
				End:    trstart.Add(24 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[4],
				Parent: spanIDs[2],
				Name:   "Decendant1",
				Start:  trstart.Add(12 * time.Second).UnixNano(),
				End:    trstart.Add(18 * time.Second).UnixNano(),
			},
			{
				ID:     spanIDs[5],
				Parent: spanIDs[1],
				Name:   "GrandChild1",
				Start:  trstart.Add(3 * time.Second).UnixNano(),
				End:    trstart.Add(8 * time.Second).UnixNano(),
				Annotations: []vtrace.Annotation{
					{
						Message: "Second Annotation",
						When:    trstart.Add(6 * time.Second).UnixNano(),
					},
					{
						Message: "First Annotation",
						When:    trstart.Add(4 * time.Second).UnixNano(),
					},
				},
			},
		},
	}

	var buf bytes.Buffer
	vtrace.FormatTrace(&buf, &tr, time.UTC)
	want := `Trace - 30783030303030303030303030303030303030303030303030303030303030303036 (2014-11-06 13:01:22.400000 UTC, ??)
    Span - Child1 [id: 00000008 parent 00000007] (1s, 10s)
        Span - GrandChild1 [id: 0000000c parent 00000008] (3s, 8s)
            @4s First Annotation
            @6s Second Annotation
    Span - Missing Data [id: 00000000 parent 00000000] (??, ??)
        Span - Decendant1 [id: 0000000b parent 00000009] (12s, 18s)
        Span - Decendant2 [id: 0000000a parent 00000009] (15s, 24s)
`

	if got := buf.String(); got != want {
		t.Errorf("Incorrect output, want\n%sgot\n%s", want, got)
	}
}
<|endoftext|>