<commit_before>package main

import (
	"fmt"
	"os"
	"time"
)

func main() {
	refresh := os.Getenv("REFRESH_TOKEN")
	client, err := NewClient(refresh)

	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("export REFRESH_TOKEN=" + client.Credentials.RefreshToken + "\n\n")

	a, err := client.GetCandles(8049, time.Now().AddDate(-1, 0, 0), time.Now(), "OneDay")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(a)
}
<commit_msg>Restructured examples<commit_after><|endoftext|>


<commit_before>package benchhash2

import (
	"math/rand"
	"testing"
)

type hashPair struct {
	label   string
	newFunc func() HashMap
}

var (
	hashPairs = []hashPair{
		hashPair{"GoMap", NewGoMap},
		hashPair{"GotomicMap", NewGotomicMap},
		hashPair{"ShardedGoMap8", NewShardedGoMap8},
		hashPair{"ShardedGoMap16", NewShardedGoMap16},
		hashPair{"ShardedGoMap32", NewShardedGoMap32},
		hashPair{"ShardedGoMap64", NewShardedGoMap64},
	}
)

func BenchmarkControl(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
		}
	})
}

func BenchmarkControl2(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
			rand.Uint32()
		}
	})
}

func BenchmarkControl3(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
		}
	})
}

func BenchmarkControl5(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
		}
	})
}

func BenchmarkControl7(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
		}
	})
}

func BenchmarkRead(b *testing.B) {
	for _, p := range hashPairs {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Get(rand.Uint32())
				}
			})
		})
	}
}

func BenchmarkWrite(b *testing.B) {
	for _, p := range hashPairs {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Put(rand.Uint32(), rand.Uint32())
				}
			})
		})
	}
}

// Equal number of reads and writes.
func BenchmarkReadWrite(b *testing.B) {
	for _, p := range hashPairs {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Put(rand.Uint32(), rand.Uint32())
					h.Get(rand.Uint32())
				}
			})
		})
	}
}

// Three reads, one write.
func BenchmarkRead3Write1(b *testing.B) {
	for _, p := range hashPairs {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Put(rand.Uint32(), rand.Uint32())
					h.Get(rand.Uint32())
					h.Get(rand.Uint32())
					h.Get(rand.Uint32())
				}
			})
		})
	}
}

// Three writes, one read.
func BenchmarkRead1Write3(b *testing.B) {
	for _, p := range hashPairs {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Put(rand.Uint32(), rand.Uint32())
					h.Put(rand.Uint32(), rand.Uint32())
					h.Put(rand.Uint32(), rand.Uint32())
					h.Get(rand.Uint32())
				}
			})
		})
	}
}
<commit_msg>Update<commit_after>package benchhash2

import (
	"math/rand"
	"testing"
)

type experiment struct {
	label   string
	newFunc func() HashMap
}

var (
	experiments = []experiment{
		experiment{"GoMap", NewGoMap},
		experiment{"GotomicMap", NewGotomicMap},
		experiment{"ShardedGoMap8", NewShardedGoMap8},
		experiment{"ShardedGoMap16", NewShardedGoMap16},
		experiment{"ShardedGoMap32", NewShardedGoMap32},
		experiment{"ShardedGoMap64", NewShardedGoMap64},
	}
)

func BenchmarkControl(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
		}
	})
}

func BenchmarkControl2(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
			rand.Uint32()
		}
	})
}

func BenchmarkControl3(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
		}
	})
}

func BenchmarkControl5(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
		}
	})
}

func BenchmarkControl7(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
			rand.Uint32()
		}
	})
}

func BenchmarkRead(b *testing.B) {
	for _, p := range experiments {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Get(rand.Uint32())
				}
			})
		})
	}
}

func BenchmarkWrite(b *testing.B) {
	for _, p := range experiments {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Put(rand.Uint32(), rand.Uint32())
				}
			})
		})
	}
}

// Equal number of reads and writes.
func BenchmarkReadWrite(b *testing.B) {
	for _, p := range experiments {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Put(rand.Uint32(), rand.Uint32())
					h.Get(rand.Uint32())
				}
			})
		})
	}
}

// Three reads, one write.
func BenchmarkRead3Write1(b *testing.B) {
	for _, p := range experiments {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Put(rand.Uint32(), rand.Uint32())
					h.Get(rand.Uint32())
					h.Get(rand.Uint32())
					h.Get(rand.Uint32())
				}
			})
		})
	}
}

// Three writes, one read.
func BenchmarkRead1Write3(b *testing.B) {
	for _, p := range experiments {
		b.Run(p.label, func(b *testing.B) {
			h := p.newFunc()
			b.StartTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					h.Put(rand.Uint32(), rand.Uint32())
					h.Put(rand.Uint32(), rand.Uint32())
					h.Put(rand.Uint32(), rand.Uint32())
					h.Get(rand.Uint32())
				}
			})
		})
	}
}
<|endoftext|>


<commit_before>package zwave

import (
	"bytes"
	"fmt"
	"time"
)

type frameParseState int

const (
	minFrameSize uint8 = 3
	maxFrameSize uint8 = 88
)

const (
	readTimeout time.Duration = 1500 * time.Millisecond
	ackTimeCan1 time.Duration = 500 * time.Millisecond
	ackTimeCan2 time.Duration = 500 * time.Millisecond
	ackTime1    time.Duration = 1000 * time.Millisecond
	ackTime2    time.Duration = 2000 * time.Millisecond
)

const (
	FRS_SOF_HUNT frameParseState = iota
	FRS_LENGTH
	FRS_TYPE
	FRS_DATA
	FRS_CHECKSUM
)

type FrameLayer struct {
	transport  *TransportLayer
	parseState frameParseState
}

func NewFrameLayer(transport *TransportLayer) *FrameLayer {
	frameLayer := &FrameLayer{
		transport: transport,
	}

	return frameLayer
}

func (f *FrameLayer) Write(bytes []byte) {
	f.transport.Write(bytes)
}

func (f *FrameLayer) Read() <-chan *Frame {
	frames := make(chan *Frame)
	go f.readFromTransport(frames)
	return frames
}

func (f *FrameLayer) sendAck() error {
	_, err := f.transport.Write([]byte{FrameSOFAck})
	return err
}

func (f *FrameLayer) sendNak() error {
	_, err := f.transport.Write([]byte{FrameSOFNak})
	return err
}

func (f *FrameLayer) readFromTransport(frames chan<- *Frame) {

	f.parseState = FRS_SOF_HUNT

	timeout := time.NewTimer(readTimeout)
	timeout.Stop()

	var sof, length, frameType, counter uint8
	payload := bytes.NewBuffer([]byte{})

	inputBytes := f.transport.Read()

	for {
		select {
		case <-timeout.C:
			f.parseState = FRS_SOF_HUNT
			payload.Reset()

		case currentByte := <-inputBytes:
			switch f.parseState {

			case FRS_SOF_HUNT:
				switch currentByte {
				case FrameSOFData:
					sof = currentByte
					f.parseState = FRS_LENGTH
					timeout.Reset(readTimeout)

				case FrameSOFAck:
					f.parseState = FRS_SOF_HUNT
					// @todo make ACK channel
				case FrameSOFCan:
					f.parseState = FRS_SOF_HUNT
					// @todo make CAN channel
				case FrameSOFNak:
					f.parseState = FRS_SOF_HUNT
					// @todo make NAK channel
				}

			case FRS_LENGTH:
				length = currentByte
				if length < minFrameSize || length > maxFrameSize {
					f.parseState = FRS_SOF_HUNT
				} else {
					counter = length - 2
					f.parseState = FRS_TYPE
				}

			case FRS_TYPE:
				frameType = currentByte
				counter--
				f.parseState = FRS_DATA

			case FRS_DATA:
				if counter > 0 {
					payload.WriteByte(currentByte)
					counter--
				} else {
					f.parseState = FRS_CHECKSUM
				}

			case FRS_CHECKSUM:
				f.parseState = FRS_SOF_HUNT

				frame := &Frame{
					Header:   sof,
					Length:   length,
					Type:     frameType,
					Payload:  payload.Bytes(),
					Checksum: currentByte,
				}

				payload.Reset()
				timeout.Stop()

				if frame.VerifyChecksum() == nil {
					fmt.Println("yo")
					frames <- frame
				} else {
					fmt.Println("invalid frame:", frame)
					// @todo send NAK
				}

			}
		}
	}
}
<commit_msg>send ack/nak based on checksum<commit_after>package zwave

import (
	"bytes"
	"time"
)

type frameParseState int

const (
	minFrameSize uint8 = 3
	maxFrameSize uint8 = 88
)

const (
	readTimeout time.Duration = 1500 * time.Millisecond
	ackTimeCan1 time.Duration = 500 * time.Millisecond
	ackTimeCan2 time.Duration = 500 * time.Millisecond
	ackTime1    time.Duration = 1000 * time.Millisecond
	ackTime2    time.Duration = 2000 * time.Millisecond
)

const (
	FRS_SOF_HUNT frameParseState = iota
	FRS_LENGTH
	FRS_TYPE
	FRS_DATA
	FRS_CHECKSUM
)

type FrameLayer struct {
	transport  *TransportLayer
	parseState frameParseState
}

func NewFrameLayer(transport *TransportLayer) *FrameLayer {
	frameLayer := &FrameLayer{
		transport: transport,
	}

	return frameLayer
}

func (f *FrameLayer) Write(bytes []byte) {
	f.transport.Write(bytes)
}

func (f *FrameLayer) Read() <-chan *Frame {
	frames := make(chan *Frame)
	go f.readFromTransport(frames)
	return frames
}

func (f *FrameLayer) sendAck() error {
	_, err := f.transport.Write([]byte{FrameSOFAck})
	return err
}

func (f *FrameLayer) sendNak() error {
	_, err := f.transport.Write([]byte{FrameSOFNak})
	return err
}

func (f *FrameLayer) readFromTransport(frames chan<- *Frame) {

	f.parseState = FRS_SOF_HUNT

	timeout := time.NewTimer(readTimeout)
	timeout.Stop()

	var sof, length, frameType, counter uint8
	payload := bytes.NewBuffer([]byte{})

	inputBytes := f.transport.Read()

	for {
		select {
		case <-timeout.C:
			f.parseState = FRS_SOF_HUNT
			payload.Reset()

		case currentByte := <-inputBytes:
			switch f.parseState {

			case FRS_SOF_HUNT:
				switch currentByte {
				case FrameSOFData:
					sof = currentByte
					f.parseState = FRS_LENGTH
					timeout.Reset(readTimeout)

				case FrameSOFAck:
					f.parseState = FRS_SOF_HUNT
					// @todo make ACK channel
				case FrameSOFCan:
					f.parseState = FRS_SOF_HUNT
					// @todo make CAN channel
				case FrameSOFNak:
					f.parseState = FRS_SOF_HUNT
					// @todo make NAK channel
				}

			case FRS_LENGTH:
				length = currentByte
				if length < minFrameSize || length > maxFrameSize {
					f.parseState = FRS_SOF_HUNT
				} else {
					counter = length - 2
					f.parseState = FRS_TYPE
				}

			case FRS_TYPE:
				frameType = currentByte
				counter--
				f.parseState = FRS_DATA

			case FRS_DATA:
				if counter > 0 {
					payload.WriteByte(currentByte)
					counter--
				} else {
					f.parseState = FRS_CHECKSUM
				}

			case FRS_CHECKSUM:
				f.parseState = FRS_SOF_HUNT

				frame := &Frame{
					Header:   sof,
					Length:   length,
					Type:     frameType,
					Payload:  payload.Bytes(),
					Checksum: currentByte,
				}

				payload.Reset()
				timeout.Stop()

				if frame.VerifyChecksum() == nil {
					frames <- frame
					f.sendAck()
				} else {
					// @todo logging?
					f.sendNak()
				}

			}
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"
)

type Validate struct {
	root  string // workdir
	Train string // name of training executable
	Prod  string // name of production executable

	Readme string // name of the README file
}

func NewValidate(dir string) (Validate, error) {
	var err error

	v := Validate{root: dir}

	exes := make([]string, 0)
	// find executables
	err = filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
		if fi.IsDir() {
			return nil
		}
		if strings.Contains(strings.ToLower(path), "readme") {
			v.Readme = path
		}

		// FIXME: better way ?
		if !strings.Contains(fi.Mode().String(), "x") {
			return nil
		}
		exes = append(exes, path)
		// printf(">>> %s\n", path)
		if strings.Contains(strings.ToLower(path), "train") {
			v.Train = path
		}
		if strings.Contains(strings.ToLower(path), "prod") {
			v.Prod = path
		}
		return err
	})

	if len(exes) <= 0 {
		return v, fmt.Errorf("hml: could not find any suitable executable in zip-file")
	}

	if v.Train == "" && v.Prod == "" {
		// take first one
		v.Train = exes[0]
		v.Prod = exes[0]
	}

	return v, err
}

func (v Validate) Run() error {
	var err error

	printf("\n")
	err = v.run_training()
	if err != nil {
		return err
	}

	printf("\n")
	err = v.run_prod()
	if err != nil {
		return err
	}

	return err
}

func (v Validate) run_training() error {
	var err error
	printf("::: run training...\n")
	dir := filepath.Join(v.root, "hml-train")
	err = os.MkdirAll(dir, 0755)
	if err != nil {
		return err
	}

	cmd := exec.Command(v.Train, "training.csv", "trained.xml")
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Dir = dir

	errch := make(chan error)
	go func() {
		err = cmd.Start()
		if err != nil {
			errch <- err
			return
		}
		errch <- cmd.Wait()
	}()

	duration := 5 * time.Minute
	select {
	case <-time.After(duration):
		cmd.Process.Kill()
		return fmt.Errorf("hml: training timed out (%v)\n", duration)
	case err = <-errch:
		return err
	}

	printf("::: run training... [ok]\n")
	return err
}

func (v Validate) run_prod() error {
	var err error
	printf("::: run prod...\n")
	dir := filepath.Join(v.root, "hml-prod")
	err = os.MkdirAll(dir, 0755)
	if err != nil {
		return err
	}

	cmd := exec.Command(v.Prod, "test.csv", "trained.xml", "scores_test.csv")
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Dir = dir

	errch := make(chan error)
	go func() {
		err = cmd.Start()
		if err != nil {
			errch <- err
			return
		}
		errch <- cmd.Wait()
	}()

	duration := 1 * time.Hour
	select {
	case <-time.After(duration):
		cmd.Process.Kill()
		return fmt.Errorf("hml: prod timed out (%v)\n", duration)
	case err = <-errch:
		return err
	}

	printf("::: run prod... [ok]\n")
	return err
}
<commit_msg>hml: prod->prediction<commit_after>package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"
)

type Validate struct {
	root  string // workdir
	Train string // name of training executable
	Prod  string // name of production executable

	Readme string // name of the README file
}

func NewValidate(dir string) (Validate, error) {
	var err error

	v := Validate{root: dir}

	exes := make([]string, 0)
	// find executables
	err = filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
		if fi.IsDir() {
			return nil
		}
		if strings.Contains(strings.ToLower(path), "readme") {
			v.Readme = path
		}

		// FIXME: better way ?
		if !strings.Contains(fi.Mode().String(), "x") {
			return nil
		}
		exes = append(exes, path)
		// printf(">>> %s\n", path)
		if strings.Contains(strings.ToLower(path), "train") {
			v.Train = path
		}
		if strings.Contains(strings.ToLower(path), "prod") {
			v.Prod = path
		}
		return err
	})

	if len(exes) <= 0 {
		return v, fmt.Errorf("hml: could not find any suitable executable in zip-file")
	}

	if v.Train == "" && v.Prod == "" {
		// take first one
		v.Train = exes[0]
		v.Prod = exes[0]
	}

	return v, err
}

func (v Validate) Run() error {
	var err error

	printf("\n")
	err = v.run_training()
	if err != nil {
		return err
	}

	printf("\n")
	err = v.run_pred()
	if err != nil {
		return err
	}

	return err
}

func (v Validate) run_training() error {
	var err error
	printf("::: run training...\n")
	dir := filepath.Join(v.root, "hml-train")
	err = os.MkdirAll(dir, 0755)
	if err != nil {
		return err
	}

	cmd := exec.Command(v.Train, "training.csv", "trained.xml")
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Dir = dir

	errch := make(chan error)
	go func() {
		err = cmd.Start()
		if err != nil {
			errch <- err
			return
		}
		errch <- cmd.Wait()
	}()

	duration := 5 * time.Minute
	select {
	case <-time.After(duration):
		cmd.Process.Kill()
		return fmt.Errorf("hml: training timed out (%v)\n", duration)
	case err = <-errch:
		return err
	}

	printf("::: run training... [ok]\n")
	return err
}

func (v Validate) run_pred() error {
	var err error
	printf("::: run prediction...\n")
	dir := filepath.Join(v.root, "hml-prediction")
	err = os.MkdirAll(dir, 0755)
	if err != nil {
		return err
	}

	cmd := exec.Command(v.Prod, "test.csv", "trained.xml", "scores_test.csv")
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Dir = dir

	errch := make(chan error)
	go func() {
		err = cmd.Start()
		if err != nil {
			errch <- err
			return
		}
		errch <- cmd.Wait()
	}()

	duration := 1 * time.Hour
	select {
	case <-time.After(duration):
		cmd.Process.Kill()
		return fmt.Errorf("hml: prediction timed out (%v)\n", duration)
	case err = <-errch:
		break
	}

	if err != nil {
		printf("::: run prediction... [ERR]\n")
		return err
	}

	printf("::: run prediction... [ok]\n")
	return err
}
<|endoftext|>


<commit_before>/*
Copyright 2016 The Kubernetes 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 awsup

import (
	"fmt"
	"time"

	"github.com/aws/aws-sdk-go/aws/client"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/golang/glog"
)

// LoggingRetryer adds some logging when we are retrying, so we have some idea what is happening
// Right now it is very basic - e.g. it only logs when we retry (so doesn't log when we fail due to too many retries)
type LoggingRetryer struct {
	client.DefaultRetryer
}

var _ request.Retryer = &LoggingRetryer{}

func newLoggingRetryer(maxRetries int) *LoggingRetryer {
	return &LoggingRetryer{
		client.DefaultRetryer{NumMaxRetries: maxRetries},
	}
}

func (l LoggingRetryer) RetryRules(r *request.Request) time.Duration {
	duration := l.DefaultRetryer.RetryRules(r)

	service := r.ClientInfo.ServiceName
	name := "?"
	if r.Operation != nil {
		name = r.Operation.Name
	}
	methodDescription := service + "/" + name

	var errorDescription string
	if r.Error != nil {
		// We could check aws error Code & Message, but we expect them to be in the string
		errorDescription = fmt.Sprintf("%v", r.Error)
	} else {
		errorDescription = fmt.Sprintf("%d %s", r.HTTPResponse.StatusCode, r.HTTPResponse.Status)
	}

	glog.Infof("Retryable error (%s) from %s - will retry after delay of %v", errorDescription, methodDescription, duration)

	return duration
}
<commit_msg>Retry Logging<commit_after>/*
Copyright 2016 The Kubernetes 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 awsup

import (
	"fmt"
	"time"

	"github.com/aws/aws-sdk-go/aws/client"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/golang/glog"
)

// LoggingRetryer adds some logging when we are retrying, so we have some idea what is happening
// Right now it is very basic - e.g. it only logs when we retry (so doesn't log when we fail due to too many retries)
type LoggingRetryer struct {
	client.DefaultRetryer
}

var _ request.Retryer = &LoggingRetryer{}

func newLoggingRetryer(maxRetries int) *LoggingRetryer {
	return &LoggingRetryer{
		client.DefaultRetryer{NumMaxRetries: maxRetries},
	}
}

func (l LoggingRetryer) RetryRules(r *request.Request) time.Duration {
	duration := l.DefaultRetryer.RetryRules(r)

	service := r.ClientInfo.ServiceName
	name := "?"
	if r.Operation != nil {
		name = r.Operation.Name
	}
	methodDescription := service + "/" + name

	var errorDescription string
	if r.Error != nil {
		// We could check aws error Code & Message, but we expect them to be in the string
		errorDescription = fmt.Sprintf("%v", r.Error)
	} else {
		errorDescription = fmt.Sprintf("%d %s", r.HTTPResponse.StatusCode, r.HTTPResponse.Status)
	}

	glog.V(2).Infof("Retryable error (%s) from %s - will retry after delay of %v", errorDescription, methodDescription, duration)

	return duration
}
<|endoftext|>


<commit_before>package skiplist

import (
	"bytes"
	"math/rand"
	"strconv"
	"testing"
)

const (
	maxN = 10000
)

func TestInsertAndFind(t *testing.T) {

	k0 := []byte("0")
	var list *SkipList

	var listPointer *SkipList
	listPointer.Insert(k0)
	if _, ok := listPointer.Find(k0); ok {
		t.Fail()
	}

	list = New()
	if _, ok := list.Find(k0); ok {
		t.Fail()
	}
	if !list.IsEmpty() {
		t.Fail()
	}

	// Test at the beginning of the list.
	for i := 0; i < maxN; i++ {
		key := []byte(strconv.Itoa(maxN-i))
		list.Insert(key)
	}
	for i := 0; i < maxN; i++ {
		key := []byte(strconv.Itoa(maxN-i))
		if _, ok := list.Find(key); !ok {
			t.Fail()
		}
	}

	list = New()
	// Test at the end of the list.
	for i := 0; i < maxN; i++ {
		key := []byte(strconv.Itoa(i))
		list.Insert(key)
	}
	for i := 0; i < maxN; i++ {
		key := []byte(strconv.Itoa(i))
		if _, ok := list.Find(key); !ok {
			t.Fail()
		}
	}

	list = New()
	// Test at random positions in the list.
	rList := rand.Perm(maxN)
	for _, e := range rList {
		key := []byte(strconv.Itoa(e))
		println("insert", e)
		list.Insert(key)
	}
	for _, e := range rList {
		key := []byte(strconv.Itoa(e))
		println("find", e)
		if _, ok := list.Find(key); !ok {
			t.Fail()
		}
	}
	println("print list")
	list.println()

}

func Element(x int) []byte {
	return []byte(strconv.Itoa(x))
}

func TestDelete(t *testing.T) {

	k0 := []byte("0")

	var list *SkipList

	// Delete on empty list
	list.Delete(k0)

	list = New()

	list.Delete(k0)
	if !list.IsEmpty() {
		t.Fail()
	}

	list.Insert(k0)
	list.Delete(k0)
	if !list.IsEmpty() {
		t.Fail()
	}

	// Delete elements at the beginning of the list.
	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}
	for i := 0; i < maxN; i++ {
		list.Delete(Element(i))
	}
	if !list.IsEmpty() {
		t.Fail()
	}

	list = New()
	// Delete elements at the end of the list.
	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}
	for i := 0; i < maxN; i++ {
		list.Delete(Element(maxN - i - 1))
	}
	if !list.IsEmpty() {
		t.Fail()
	}

	list = New()
	// Delete elements at random positions in the list.
	rList := rand.Perm(maxN)
	for _, e := range rList {
		list.Insert(Element(e))
	}
	for _, e := range rList {
		list.Delete(Element(e))
	}
	if !list.IsEmpty() {
		t.Fail()
	}
}

func TestNext(t *testing.T) {
	list := New()

	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}

	smallest := list.GetSmallestNode()
	largest := list.GetLargestNode()

	lastNode := smallest
	node := lastNode
	for node != largest {
		node = list.Next(node)
		// Must always be incrementing here!
		if bytes.Compare(node.Values[0], lastNode.Values[0]) <= 0 {
			t.Fail()
		}
		// Next.Prev must always point to itself!
		if list.Next(list.Prev(node)) != node {
			t.Fail()
		}
		lastNode = node
	}

	if list.Next(largest) != smallest {
		t.Fail()
	}
}

func TestPrev(t *testing.T) {
	list := New()

	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}

	smallest := list.GetSmallestNode()
	largest := list.GetLargestNode()

	lastNode := largest
	node := lastNode
	for node != smallest {
		node = list.Prev(node)
		// Must always be incrementing here!
		if bytes.Compare(node.Values[0], lastNode.Values[0]) >= 0 {
			t.Fail()
		}
		// Next.Prev must always point to itself!
		if list.Prev(list.Next(node)) != node {
			t.Fail()
		}
		lastNode = node
	}

	if list.Prev(smallest) != largest {
		t.Fail()
	}
}

func TestGetNodeCount(t *testing.T) {
	list := New()

	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}

	if list.GetNodeCount() != maxN {
		t.Fail()
	}
}
<commit_msg>TestFindGreaterOrEqual<commit_after>package skiplist

import (
	"bytes"
	"fmt"
	"math/rand"
	"strconv"
	"testing"
)

const (
	maxN = 10000
)

func TestInsertAndFind(t *testing.T) {

	k0 := []byte("0")
	var list *SkipList

	var listPointer *SkipList
	listPointer.Insert(k0)
	if _, ok := listPointer.Find(k0); ok {
		t.Fail()
	}

	list = New()
	if _, ok := list.Find(k0); ok {
		t.Fail()
	}
	if !list.IsEmpty() {
		t.Fail()
	}

	// Test at the beginning of the list.
	for i := 0; i < maxN; i++ {
		key := []byte(strconv.Itoa(maxN-i))
		list.Insert(key)
	}
	for i := 0; i < maxN; i++ {
		key := []byte(strconv.Itoa(maxN-i))
		if _, ok := list.Find(key); !ok {
			t.Fail()
		}
	}

	list = New()
	// Test at the end of the list.
	for i := 0; i < maxN; i++ {
		key := []byte(strconv.Itoa(i))
		list.Insert(key)
	}
	for i := 0; i < maxN; i++ {
		key := []byte(strconv.Itoa(i))
		if _, ok := list.Find(key); !ok {
			t.Fail()
		}
	}

	list = New()
	// Test at random positions in the list.
	rList := rand.Perm(maxN)
	for _, e := range rList {
		key := []byte(strconv.Itoa(e))
		println("insert", e)
		list.Insert(key)
	}
	for _, e := range rList {
		key := []byte(strconv.Itoa(e))
		println("find", e)
		if _, ok := list.Find(key); !ok {
			t.Fail()
		}
	}
	println("print list")
	list.println()

}

func Element(x int) []byte {
	return []byte(strconv.Itoa(x))
}

func TestDelete(t *testing.T) {

	k0 := []byte("0")

	var list *SkipList

	// Delete on empty list
	list.Delete(k0)

	list = New()

	list.Delete(k0)
	if !list.IsEmpty() {
		t.Fail()
	}

	list.Insert(k0)
	list.Delete(k0)
	if !list.IsEmpty() {
		t.Fail()
	}

	// Delete elements at the beginning of the list.
	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}
	for i := 0; i < maxN; i++ {
		list.Delete(Element(i))
	}
	if !list.IsEmpty() {
		t.Fail()
	}

	list = New()
	// Delete elements at the end of the list.
	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}
	for i := 0; i < maxN; i++ {
		list.Delete(Element(maxN - i - 1))
	}
	if !list.IsEmpty() {
		t.Fail()
	}

	list = New()
	// Delete elements at random positions in the list.
	rList := rand.Perm(maxN)
	for _, e := range rList {
		list.Insert(Element(e))
	}
	for _, e := range rList {
		list.Delete(Element(e))
	}
	if !list.IsEmpty() {
		t.Fail()
	}
}

func TestNext(t *testing.T) {
	list := New()

	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}

	smallest := list.GetSmallestNode()
	largest := list.GetLargestNode()

	lastNode := smallest
	node := lastNode
	for node != largest {
		node = list.Next(node)
		// Must always be incrementing here!
		if bytes.Compare(node.Values[0], lastNode.Values[0]) <= 0 {
			t.Fail()
		}
		// Next.Prev must always point to itself!
		if list.Next(list.Prev(node)) != node {
			t.Fail()
		}
		lastNode = node
	}

	if list.Next(largest) != smallest {
		t.Fail()
	}
}

func TestPrev(t *testing.T) {
	list := New()

	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}

	smallest := list.GetSmallestNode()
	largest := list.GetLargestNode()

	lastNode := largest
	node := lastNode
	for node != smallest {
		node = list.Prev(node)
		// Must always be incrementing here!
		if bytes.Compare(node.Values[0], lastNode.Values[0]) >= 0 {
			t.Fail()
		}
		// Next.Prev must always point to itself!
		if list.Prev(list.Next(node)) != node {
			t.Fail()
		}
		lastNode = node
	}

	if list.Prev(smallest) != largest {
		t.Fail()
	}
}

func TestGetNodeCount(t *testing.T) {
	list := New()

	for i := 0; i < maxN; i++ {
		list.Insert(Element(i))
	}

	if list.GetNodeCount() != maxN {
		t.Fail()
	}
}

func TestFindGreaterOrEqual(t *testing.T) {

	maxNumber := maxN * 100

	var list *SkipList
	var listPointer *SkipList

	// Test on empty list.
	if _, ok := listPointer.FindGreaterOrEqual(Element(0)); ok {
		t.Fail()
	}

	list = New()

	for i := 0; i < maxN; i++ {
		list.Insert(Element(rand.Intn(maxNumber)))
	}

	for i := 0; i < maxN; i++ {
		key := Element(rand.Intn(maxNumber))
		if v, ok := list.FindGreaterOrEqual(key); ok {
			// if f is v should be bigger than the element before
			if bytes.Compare(v.Prev.Key, key) >= 0 {
				fmt.Printf("PrevV: %s\n    key: %s\n\n", string(v.Prev.Key), string(key))
				t.Fail()
			}
			// v should be bigger or equal to f
			// If we compare directly, we get an equal key with a difference on the 10th decimal point, which fails.
			if bytes.Compare(v.Values[0], key) < 0 {
				fmt.Printf("v: %s\n    key: %s\n\n", string(v.Values[0]), string(key))
				t.Fail()
			}
		} else {
			lastV := list.GetLargestNode().GetValue()
			// It is OK, to fail, as long as f is bigger than the last element.
			if bytes.Compare(key, lastV) <= 0 {
				fmt.Printf("lastV: %s\n    key: %s\n\n", string(lastV), string(key))
				t.Fail()
			}
		}
	}

}<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"log"
	"strconv"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/batch"
	"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
	"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
	"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func init() {
	resource.AddTestSweepers("aws_batch_job_queue", &resource.Sweeper{
		Name: "aws_batch_job_queue",
		F:    testSweepBatchJobQueues,
	})
}

func testSweepBatchJobQueues(region string) error {
	client, err := sharedClientForRegion(region)
	if err != nil {
		return fmt.Errorf("error getting client: %s", err)
	}
	conn := client.(*AWSClient).batchconn

	out, err := conn.DescribeJobQueues(&batch.DescribeJobQueuesInput{})
	if err != nil {
		if testSweepSkipSweepError(err) {
			log.Printf("[WARN] Skipping Batch Job Queue sweep for %s: %s", region, err)
			return nil
		}
		return fmt.Errorf("Error retrieving Batch Job Queues: %s", err)
	}
	for _, jobQueue := range out.JobQueues {
		name := jobQueue.JobQueueName

		log.Printf("[INFO] Disabling Batch Job Queue: %s", *name)
		err := disableBatchJobQueue(*name, conn)
		if err != nil {
			log.Printf("[ERROR] Failed to disable Batch Job Queue %s: %s", *name, err)
			continue
		}

		log.Printf("[INFO] Deleting Batch Job Queue: %s", *name)
		err = deleteBatchJobQueue(*name, conn)
		if err != nil {
			log.Printf("[ERROR] Failed to delete Batch Job Queue %s: %s", *name, err)
		}
	}

	return nil
}

func TestAccAWSBatchJobQueue_basic(t *testing.T) {
	var jq batch.JobQueueDetail
	ri := acctest.RandInt()
	config := fmt.Sprintf(testAccBatchJobQueueBasic, ri)
	resourceName := "aws_batch_job_queue.test_queue"
	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckBatchJobQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: config,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBatchJobQueueExists(resourceName, &jq),
					testAccCheckBatchJobQueueAttributes(&jq),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSBatchJobQueue_disappears(t *testing.T) {
	var jobQueue1 batch.JobQueueDetail
	resourceName := "aws_batch_job_queue.test_queue"
	rInt := acctest.RandInt()

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
		Steps: []resource.TestStep{
			{
				Config: fmt.Sprintf(testAccBatchJobQueueBasic, rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBatchJobQueueExists(resourceName, &jobQueue1),
					testAccCheckBatchJobQueueDisappears(&jobQueue1),
				),
				ExpectNonEmptyPlan: true,
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSBatchJobQueue_update(t *testing.T) {
	var jq batch.JobQueueDetail
	ri := acctest.RandInt()
	config := fmt.Sprintf(testAccBatchJobQueueBasic, ri)
	updateConfig := fmt.Sprintf(testAccBatchJobQueueUpdate, ri)
	resourceName := "aws_batch_job_queue.test_queue"
	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckBatchJobQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: config,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBatchJobQueueExists(resourceName, &jq),
					testAccCheckBatchJobQueueAttributes(&jq),
				),
			},
			{
				Config: updateConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBatchJobQueueExists(resourceName, &jq),
					testAccCheckBatchJobQueueAttributes(&jq),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func testAccCheckBatchJobQueueExists(n string, jq *batch.JobQueueDetail) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		log.Printf("State: %#v", s.RootModule().Resources)
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No Batch Job Queue ID is set")
		}

		conn := testAccProvider.Meta().(*AWSClient).batchconn
		name := rs.Primary.Attributes["name"]
		queue, err := getJobQueue(conn, name)
		if err != nil {
			return err
		}
		if queue == nil {
			return fmt.Errorf("Not found: %s", n)
		}
		*jq = *queue

		return nil
	}
}

func testAccCheckBatchJobQueueAttributes(jq *batch.JobQueueDetail) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		if !strings.HasPrefix(*jq.JobQueueName, "tf_acctest_batch_job_queue") {
			return fmt.Errorf("Bad Job Queue name: %s", *jq.JobQueueName)
		}
		for _, rs := range s.RootModule().Resources {
			if rs.Type != "aws_batch_job_queue" {
				continue
			}
			if *jq.JobQueueArn != rs.Primary.Attributes["arn"] {
				return fmt.Errorf("Bad Job Queue ARN\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["arn"], *jq.JobQueueArn)
			}
			if *jq.State != rs.Primary.Attributes["state"] {
				return fmt.Errorf("Bad Job Queue State\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["state"], *jq.State)
			}
			priority, err := strconv.ParseInt(rs.Primary.Attributes["priority"], 10, 64)
			if err != nil {
				return err
			}
			if *jq.Priority != priority {
				return fmt.Errorf("Bad Job Queue Priority\n\t expected: %s\n\tgot: %d\n", rs.Primary.Attributes["priority"], *jq.Priority)
			}
		}
		return nil
	}
}

func testAccCheckBatchJobQueueDestroy(s *terraform.State) error {
	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_batch_job_queue" {
			continue
		}
		conn := testAccProvider.Meta().(*AWSClient).batchconn
		jq, err := getJobQueue(conn, rs.Primary.Attributes["name"])
		if err == nil {
			if jq != nil {
				return fmt.Errorf("Error: Job Queue still exists")
			}
		}
		return nil
	}
	return nil
}

func testAccCheckBatchJobQueueDisappears(jobQueue *batch.JobQueueDetail) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		conn := testAccProvider.Meta().(*AWSClient).batchconn
		name := aws.StringValue(jobQueue.JobQueueName)

		err := disableBatchJobQueue(name, conn)
		if err != nil {
			return fmt.Errorf("error disabling Batch Job Queue (%s): %s", name, err)
		}

		return deleteBatchJobQueue(name, conn)
	}
}

const testAccBatchJobQueueBaseConfig = `
########## ecs_instance_role ##########

resource "aws_iam_role" "ecs_instance_role" {
  name = "ecs_instance_role_%[1]d"
  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
	{
	    "Action": "sts:AssumeRole",
	    "Effect": "Allow",
	    "Principal": {
		"Service": "ec2.amazonaws.com"
	    }
	}
    ]
}
EOF
}

resource "aws_iam_role_policy_attachment" "ecs_instance_role" {
  role       = "${aws_iam_role.ecs_instance_role.name}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}

resource "aws_iam_instance_profile" "ecs_instance_role" {
  name  = "ecs_instance_role_%[1]d"
  role = "${aws_iam_role.ecs_instance_role.name}"
}

########## aws_batch_service_role ##########

resource "aws_iam_role" "aws_batch_service_role" {
  name = "aws_batch_service_role_%[1]d"
  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
	{
	    "Action": "sts:AssumeRole",
	    "Effect": "Allow",
	    "Principal": {
		"Service": "batch.amazonaws.com"
	    }
	}
    ]
}
EOF
}

resource "aws_iam_role_policy_attachment" "aws_batch_service_role" {
  role       = "${aws_iam_role.aws_batch_service_role.name}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole"
}

########## security group ##########

resource "aws_security_group" "test_acc" {
  name = "aws_batch_compute_environment_security_group_%[1]d"
}

########## subnets ##########

resource "aws_vpc" "test_acc" {
  cidr_block = "10.1.0.0/16"
  tags = {
    Name = "terraform-testacc-batch-job-queue"
  }
}

resource "aws_subnet" "test_acc" {
  vpc_id = "${aws_vpc.test_acc.id}"
  cidr_block = "10.1.1.0/24"
  tags = {
    Name = "tf-acc-batch-job-queue"
  }
}

resource "aws_batch_compute_environment" "test_environment" {
  compute_environment_name = "tf_acctest_batch_compute_environment_%[1]d"
  compute_resources {
    instance_role = "${aws_iam_role.aws_batch_service_role.arn}"
    instance_type = ["m3.medium"]
    max_vcpus = 1
    min_vcpus = 0
    security_group_ids = ["${aws_security_group.test_acc.id}"]
    subnets = ["${aws_subnet.test_acc.id}"]
    type = "EC2"
  }
  service_role = "${aws_iam_role.aws_batch_service_role.arn}"
  type = "MANAGED"
  depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}`

var testAccBatchJobQueueBasic = testAccBatchJobQueueBaseConfig + `
resource "aws_batch_job_queue" "test_queue" {
  name = "tf_acctest_batch_job_queue_%[1]d"
  state = "ENABLED"
  priority = 1
  compute_environments = ["${aws_batch_compute_environment.test_environment.arn}"]
}`

var testAccBatchJobQueueUpdate = testAccBatchJobQueueBaseConfig + `
resource "aws_batch_job_queue" "test_queue" {
  name = "tf_acctest_batch_job_queue_%[1]d"
  state = "DISABLED"
  priority = 2
  compute_environments = ["${aws_batch_compute_environment.test_environment.arn}"]
}`
<commit_msg>tests/resource/aws_batch_job_queue: Remove extraneous import testing from disappears test<commit_after>package aws

import (
	"fmt"
	"log"
	"strconv"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/batch"
	"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
	"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
	"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func init() {
	resource.AddTestSweepers("aws_batch_job_queue", &resource.Sweeper{
		Name: "aws_batch_job_queue",
		F:    testSweepBatchJobQueues,
	})
}

func testSweepBatchJobQueues(region string) error {
	client, err := sharedClientForRegion(region)
	if err != nil {
		return fmt.Errorf("error getting client: %s", err)
	}
	conn := client.(*AWSClient).batchconn

	out, err := conn.DescribeJobQueues(&batch.DescribeJobQueuesInput{})
	if err != nil {
		if testSweepSkipSweepError(err) {
			log.Printf("[WARN] Skipping Batch Job Queue sweep for %s: %s", region, err)
			return nil
		}
		return fmt.Errorf("Error retrieving Batch Job Queues: %s", err)
	}
	for _, jobQueue := range out.JobQueues {
		name := jobQueue.JobQueueName

		log.Printf("[INFO] Disabling Batch Job Queue: %s", *name)
		err := disableBatchJobQueue(*name, conn)
		if err != nil {
			log.Printf("[ERROR] Failed to disable Batch Job Queue %s: %s", *name, err)
			continue
		}

		log.Printf("[INFO] Deleting Batch Job Queue: %s", *name)
		err = deleteBatchJobQueue(*name, conn)
		if err != nil {
			log.Printf("[ERROR] Failed to delete Batch Job Queue %s: %s", *name, err)
		}
	}

	return nil
}

func TestAccAWSBatchJobQueue_basic(t *testing.T) {
	var jq batch.JobQueueDetail
	ri := acctest.RandInt()
	config := fmt.Sprintf(testAccBatchJobQueueBasic, ri)
	resourceName := "aws_batch_job_queue.test_queue"
	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckBatchJobQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: config,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBatchJobQueueExists(resourceName, &jq),
					testAccCheckBatchJobQueueAttributes(&jq),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSBatchJobQueue_disappears(t *testing.T) {
	var jobQueue1 batch.JobQueueDetail
	resourceName := "aws_batch_job_queue.test_queue"
	rInt := acctest.RandInt()

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSLaunchTemplateDestroy,
		Steps: []resource.TestStep{
			{
				Config: fmt.Sprintf(testAccBatchJobQueueBasic, rInt),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBatchJobQueueExists(resourceName, &jobQueue1),
					testAccCheckBatchJobQueueDisappears(&jobQueue1),
				),
				ExpectNonEmptyPlan: true,
			},
		},
	})
}

func TestAccAWSBatchJobQueue_update(t *testing.T) {
	var jq batch.JobQueueDetail
	ri := acctest.RandInt()
	config := fmt.Sprintf(testAccBatchJobQueueBasic, ri)
	updateConfig := fmt.Sprintf(testAccBatchJobQueueUpdate, ri)
	resourceName := "aws_batch_job_queue.test_queue"
	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckBatchJobQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: config,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBatchJobQueueExists(resourceName, &jq),
					testAccCheckBatchJobQueueAttributes(&jq),
				),
			},
			{
				Config: updateConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBatchJobQueueExists(resourceName, &jq),
					testAccCheckBatchJobQueueAttributes(&jq),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func testAccCheckBatchJobQueueExists(n string, jq *batch.JobQueueDetail) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		log.Printf("State: %#v", s.RootModule().Resources)
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No Batch Job Queue ID is set")
		}

		conn := testAccProvider.Meta().(*AWSClient).batchconn
		name := rs.Primary.Attributes["name"]
		queue, err := getJobQueue(conn, name)
		if err != nil {
			return err
		}
		if queue == nil {
			return fmt.Errorf("Not found: %s", n)
		}
		*jq = *queue

		return nil
	}
}

func testAccCheckBatchJobQueueAttributes(jq *batch.JobQueueDetail) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		if !strings.HasPrefix(*jq.JobQueueName, "tf_acctest_batch_job_queue") {
			return fmt.Errorf("Bad Job Queue name: %s", *jq.JobQueueName)
		}
		for _, rs := range s.RootModule().Resources {
			if rs.Type != "aws_batch_job_queue" {
				continue
			}
			if *jq.JobQueueArn != rs.Primary.Attributes["arn"] {
				return fmt.Errorf("Bad Job Queue ARN\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["arn"], *jq.JobQueueArn)
			}
			if *jq.State != rs.Primary.Attributes["state"] {
				return fmt.Errorf("Bad Job Queue State\n\t expected: %s\n\tgot: %s\n", rs.Primary.Attributes["state"], *jq.State)
			}
			priority, err := strconv.ParseInt(rs.Primary.Attributes["priority"], 10, 64)
			if err != nil {
				return err
			}
			if *jq.Priority != priority {
				return fmt.Errorf("Bad Job Queue Priority\n\t expected: %s\n\tgot: %d\n", rs.Primary.Attributes["priority"], *jq.Priority)
			}
		}
		return nil
	}
}

func testAccCheckBatchJobQueueDestroy(s *terraform.State) error {
	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_batch_job_queue" {
			continue
		}
		conn := testAccProvider.Meta().(*AWSClient).batchconn
		jq, err := getJobQueue(conn, rs.Primary.Attributes["name"])
		if err == nil {
			if jq != nil {
				return fmt.Errorf("Error: Job Queue still exists")
			}
		}
		return nil
	}
	return nil
}

func testAccCheckBatchJobQueueDisappears(jobQueue *batch.JobQueueDetail) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		conn := testAccProvider.Meta().(*AWSClient).batchconn
		name := aws.StringValue(jobQueue.JobQueueName)

		err := disableBatchJobQueue(name, conn)
		if err != nil {
			return fmt.Errorf("error disabling Batch Job Queue (%s): %s", name, err)
		}

		return deleteBatchJobQueue(name, conn)
	}
}

const testAccBatchJobQueueBaseConfig = `
########## ecs_instance_role ##########

resource "aws_iam_role" "ecs_instance_role" {
  name = "ecs_instance_role_%[1]d"
  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
	{
	    "Action": "sts:AssumeRole",
	    "Effect": "Allow",
	    "Principal": {
		"Service": "ec2.amazonaws.com"
	    }
	}
    ]
}
EOF
}

resource "aws_iam_role_policy_attachment" "ecs_instance_role" {
  role       = "${aws_iam_role.ecs_instance_role.name}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}

resource "aws_iam_instance_profile" "ecs_instance_role" {
  name  = "ecs_instance_role_%[1]d"
  role = "${aws_iam_role.ecs_instance_role.name}"
}

########## aws_batch_service_role ##########

resource "aws_iam_role" "aws_batch_service_role" {
  name = "aws_batch_service_role_%[1]d"
  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
	{
	    "Action": "sts:AssumeRole",
	    "Effect": "Allow",
	    "Principal": {
		"Service": "batch.amazonaws.com"
	    }
	}
    ]
}
EOF
}

resource "aws_iam_role_policy_attachment" "aws_batch_service_role" {
  role       = "${aws_iam_role.aws_batch_service_role.name}"
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole"
}

########## security group ##########

resource "aws_security_group" "test_acc" {
  name = "aws_batch_compute_environment_security_group_%[1]d"
}

########## subnets ##########

resource "aws_vpc" "test_acc" {
  cidr_block = "10.1.0.0/16"
  tags = {
    Name = "terraform-testacc-batch-job-queue"
  }
}

resource "aws_subnet" "test_acc" {
  vpc_id = "${aws_vpc.test_acc.id}"
  cidr_block = "10.1.1.0/24"
  tags = {
    Name = "tf-acc-batch-job-queue"
  }
}

resource "aws_batch_compute_environment" "test_environment" {
  compute_environment_name = "tf_acctest_batch_compute_environment_%[1]d"
  compute_resources {
    instance_role = "${aws_iam_role.aws_batch_service_role.arn}"
    instance_type = ["m3.medium"]
    max_vcpus = 1
    min_vcpus = 0
    security_group_ids = ["${aws_security_group.test_acc.id}"]
    subnets = ["${aws_subnet.test_acc.id}"]
    type = "EC2"
  }
  service_role = "${aws_iam_role.aws_batch_service_role.arn}"
  type = "MANAGED"
  depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}`

var testAccBatchJobQueueBasic = testAccBatchJobQueueBaseConfig + `
resource "aws_batch_job_queue" "test_queue" {
  name = "tf_acctest_batch_job_queue_%[1]d"
  state = "ENABLED"
  priority = 1
  compute_environments = ["${aws_batch_compute_environment.test_environment.arn}"]
}`

var testAccBatchJobQueueUpdate = testAccBatchJobQueueBaseConfig + `
resource "aws_batch_job_queue" "test_queue" {
  name = "tf_acctest_batch_job_queue_%[1]d"
  state = "DISABLED"
  priority = 2
  compute_environments = ["${aws_batch_compute_environment.test_environment.arn}"]
}`
<|endoftext|>


<commit_before>// +build !gtk_3_6,!gtk_3_8,!gtk_3_10,!gtk_3_12,!gtk_3_14

// See: https://developer.gnome.org/gtk3/3.16/api-index-3-16.html

package gtk

// #include <gtk/gtk.h>
// #include "gtk_since_3_16.go.h"
import "C"
import (
	"unsafe"

	"github.com/gotk3/gotk3/glib"
)

const(
	POLICY_EXTERNAL  PolicyType = C.GTK_POLICY_EXTERNAL
)

func init() {
	tm := []glib.TypeMarshaler{

		// Objects/Interfaces
		{glib.Type(C.gtk_popover_menu_get_type()), marshalPopoverMenu},
		{glib.Type(C.gtk_model_button_get_type()), marshalModelButton},
		{glib.Type(C.gtk_stack_sidebar_get_type()), marshalStackSidebar},
	}
	glib.RegisterGValueMarshalers(tm)

	//Contribute to casting
	for k, v := range map[string]WrapFn{
		"GtkPopoverMenu":  wrapPopoverMenu,
		"GtkModelButton":  wrapModelButton,
		"GtkStackSidebar": wrapStackSidebar,
	} {
		WrapMap[k] = v
	}
}

// SetOverlayScrolling is a wrapper around gtk_scrolled_window_set_overlay_scrolling().
func (v *ScrolledWindow) SetOverlayScrolling(scrolling bool) {
	C.gtk_scrolled_window_set_overlay_scrolling(v.native(), gbool(scrolling))
}

// GetOverlayScrolling is a wrapper around gtk_scrolled_window_get_overlay_scrolling().
func (v *ScrolledWindow) GetOverlayScrolling() bool {
	return gobool(C.gtk_scrolled_window_get_overlay_scrolling(v.native()))
}

// SetWideHandle is a wrapper around gtk_paned_set_wide_handle().
func (v *Paned) SetWideHandle(wide bool) {
	C.gtk_paned_set_wide_handle(v.native(), gbool(wide))
}

// GetWideHandle is a wrapper around gtk_paned_get_wide_handle().
func (v *Paned) GetWideHandle() bool {
	return gobool(C.gtk_paned_get_wide_handle(v.native()))
}

// GetXAlign is a wrapper around gtk_label_get_xalign().
func (v *Label) GetXAlign() float64 {
	c := C.gtk_label_get_xalign(v.native())
	return float64(c)
}

// GetYAlign is a wrapper around gtk_label_get_yalign().
func (v *Label) GetYAlign() float64 {
	c := C.gtk_label_get_yalign(v.native())
	return float64(c)
}

// SetXAlign is a wrapper around gtk_label_set_xalign().
func (v *Label) SetXAlign(n float64) {
	C.gtk_label_set_xalign(v.native(), C.gfloat(n))
}

// SetYAlign is a wrapper around gtk_label_set_yalign().
func (v *Label) SetYAlign(n float64) {
	C.gtk_label_set_yalign(v.native(), C.gfloat(n))
}

/*
* GtkModelButton
*/

// ModelButton is a representation of GTK's GtkModelButton.
type ModelButton struct {
	Button
 }

 func (v *ModelButton) native() *C.GtkModelButton {
	 if v == nil || v.GObject == nil {
		 return nil
	 }

	 p := unsafe.Pointer(v.GObject)
	 return C.toGtkModelButton(p)
 }

 func marshalModelButton(p uintptr) (interface{}, error) {
	 c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
	 return wrapModelButton(glib.Take(unsafe.Pointer(c))), nil
 }

 func wrapModelButton(obj *glib.Object) *ModelButton {
	 actionable := wrapActionable(obj)
	 return &ModelButton{Button{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}, actionable}}
 }

 // ModelButtonNew is a wrapper around gtk_model_button_new
 func ModelButtonNew() (*ModelButton, error) {
	 c := C.gtk_model_button_new()
	 if c == nil {
		 return nil, nilPtrErr
	 }
	 return wrapModelButton(glib.Take(unsafe.Pointer(c))), nil
 }

/*
 * GtkPopoverMenu
 */

// PopoverMenu is a representation of GTK's GtkPopoverMenu.
type PopoverMenu struct {
	Popover
}

func (v *PopoverMenu) native() *C.GtkPopoverMenu {
	if v == nil || v.GObject == nil {
		return nil
	}

	p := unsafe.Pointer(v.GObject)
	return C.toGtkPopoverMenu(p)
}

func marshalPopoverMenu(p uintptr) (interface{}, error) {
	c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
	return wrapPopoverMenu(glib.Take(unsafe.Pointer(c))), nil
}

func wrapPopoverMenu(obj *glib.Object) *PopoverMenu {
	return &PopoverMenu{Popover{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}}}
}

// PopoverMenuNew is a wrapper around gtk_popover_menu_new
func PopoverMenuNew() (*PopoverMenu, error) {
	c := C.gtk_popover_menu_new()
	if c == nil {
		return nil, nilPtrErr
	}
	return wrapPopoverMenu(glib.Take(unsafe.Pointer(c))), nil
}

// OpenSubmenu is a wrapper around gtk_popover_menu_open_submenu
func (v *PopoverMenu) OpenSubmenu(name string) {
	cstr1 := (*C.gchar)(C.CString(name))
	defer C.free(unsafe.Pointer(cstr1))

	C.gtk_popover_menu_open_submenu(v.native(), cstr1)
}

/*
 * GtkStackSidebar
 */

// StackSidebar is a representation of GTK's GtkStackSidebar.
type StackSidebar struct {
	Bin
}

// native returns a pointer to the underlying GtkStack.
func (v *StackSidebar) native() *C.GtkStackSidebar {
	if v == nil || v.GObject == nil {
		return nil
	}
	p := unsafe.Pointer(v.GObject)
	return C.toGtkStackSidebar(p)
}

func marshalStackSidebar(p uintptr) (interface{}, error) {
	c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
	obj := glib.Take(unsafe.Pointer(c))
	return wrapStackSidebar(obj), nil
}

func wrapStackSidebar(obj *glib.Object) *StackSidebar {
	return &StackSidebar{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}}
}

// StackSidebarNew is a wrapper around gtk_stack_sidebar_new().
func StackSidebarNew() (*StackSidebar, error) {
	c := C.gtk_stack_sidebar_new()
	if c == nil {
		return nil, nilPtrErr
	}
	return wrapStackSidebar(glib.Take(unsafe.Pointer(c))), nil
}

func (v *StackSidebar) SetStack(stack *Stack) {
	C.gtk_stack_sidebar_set_stack(v.native(), stack.native())
}

func (v *StackSidebar) GetStack() *Stack {
	c := C.gtk_stack_sidebar_get_stack(v.native())
	if c == nil {
		return nil
	}
	return wrapStack(glib.Take(unsafe.Pointer(c)))
}

// GrabFocusWithoutSelecting is a wrapper for gtk_entry_grab_focus_without_selecting()
func (v *Entry) GrabFocusWithoutSelecting() {
	C.gtk_entry_grab_focus_without_selecting(v.native())
}

// InsertMarkup() is a wrapper around  gtk_text_buffer_insert_markup()
func (v *TextBuffer) InsertMarkup(start *TextIter, text string) {
	cstr := C.CString(text)
	defer C.free(unsafe.Pointer(cstr))
	C.gtk_text_buffer_insert_markup(v.native(), (*C.GtkTextIter)(start), (*C.gchar)(cstr), C.gint(len(text)))
}
<commit_msg>add binding for GtkButtonRole<commit_after>// +build !gtk_3_6,!gtk_3_8,!gtk_3_10,!gtk_3_12,!gtk_3_14

// See: https://developer.gnome.org/gtk3/3.16/api-index-3-16.html

package gtk

// #include <gtk/gtk.h>
// #include "gtk_since_3_16.go.h"
import "C"
import (
	"unsafe"

	"github.com/gotk3/gotk3/glib"
)

const(
	POLICY_EXTERNAL  PolicyType = C.GTK_POLICY_EXTERNAL
)

func init() {
	tm := []glib.TypeMarshaler{

		// Objects/Interfaces
		{glib.Type(C.gtk_button_role_get_type()), marshalButtonRole},
		{glib.Type(C.gtk_popover_menu_get_type()), marshalPopoverMenu},
		{glib.Type(C.gtk_model_button_get_type()), marshalModelButton},
		{glib.Type(C.gtk_stack_sidebar_get_type()), marshalStackSidebar},
	}
	glib.RegisterGValueMarshalers(tm)

	//Contribute to casting
	for k, v := range map[string]WrapFn{
		"GtkPopoverMenu":  wrapPopoverMenu,
		"GtkModelButton":  wrapModelButton,
		"GtkStackSidebar": wrapStackSidebar,
	} {
		WrapMap[k] = v
	}
}

/*
 * Constants
 */

// ButtonRole is a representation of GTK's GtkButtonRole.
type ButtonRole int

const (
	BUTTON_ROLE_NORMAL ButtonRole = C.GTK_BUTTON_ROLE_NORMAL
	BUTTON_ROLE_CHECK  ButtonRole = C.GTK_BUTTON_ROLE_CHECK
	BUTTON_ROLE_RADIO  ButtonRole = C.GTK_BUTTON_ROLE_RADIO
)

func marshalButtonRole(p uintptr) (interface{}, error) {
	c := C.g_value_get_enum((*C.GValue)(unsafe.Pointer(p)))
	return ButtonRole(c), nil
}

/*
 * GtkScrolledWindow
 */

// SetOverlayScrolling is a wrapper around gtk_scrolled_window_set_overlay_scrolling().
func (v *ScrolledWindow) SetOverlayScrolling(scrolling bool) {
	C.gtk_scrolled_window_set_overlay_scrolling(v.native(), gbool(scrolling))
}

// GetOverlayScrolling is a wrapper around gtk_scrolled_window_get_overlay_scrolling().
func (v *ScrolledWindow) GetOverlayScrolling() bool {
	return gobool(C.gtk_scrolled_window_get_overlay_scrolling(v.native()))
}

/*
 * GtkPaned
 */

// SetWideHandle is a wrapper around gtk_paned_set_wide_handle().
func (v *Paned) SetWideHandle(wide bool) {
	C.gtk_paned_set_wide_handle(v.native(), gbool(wide))
}

// GetWideHandle is a wrapper around gtk_paned_get_wide_handle().
func (v *Paned) GetWideHandle() bool {
	return gobool(C.gtk_paned_get_wide_handle(v.native()))
}

/*
 * GtkLabel
 */

// GetXAlign is a wrapper around gtk_label_get_xalign().
func (v *Label) GetXAlign() float64 {
	c := C.gtk_label_get_xalign(v.native())
	return float64(c)
}

// GetYAlign is a wrapper around gtk_label_get_yalign().
func (v *Label) GetYAlign() float64 {
	c := C.gtk_label_get_yalign(v.native())
	return float64(c)
}

// SetXAlign is a wrapper around gtk_label_set_xalign().
func (v *Label) SetXAlign(n float64) {
	C.gtk_label_set_xalign(v.native(), C.gfloat(n))
}

// SetYAlign is a wrapper around gtk_label_set_yalign().
func (v *Label) SetYAlign(n float64) {
	C.gtk_label_set_yalign(v.native(), C.gfloat(n))
}

/*
* GtkModelButton
*/

// ModelButton is a representation of GTK's GtkModelButton.
type ModelButton struct {
	Button
 }

 func (v *ModelButton) native() *C.GtkModelButton {
	 if v == nil || v.GObject == nil {
		 return nil
	 }

	 p := unsafe.Pointer(v.GObject)
	 return C.toGtkModelButton(p)
 }

 func marshalModelButton(p uintptr) (interface{}, error) {
	 c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
	 return wrapModelButton(glib.Take(unsafe.Pointer(c))), nil
 }

 func wrapModelButton(obj *glib.Object) *ModelButton {
	 actionable := wrapActionable(obj)
	 return &ModelButton{Button{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}, actionable}}
 }

 // ModelButtonNew is a wrapper around gtk_model_button_new
 func ModelButtonNew() (*ModelButton, error) {
	 c := C.gtk_model_button_new()
	 if c == nil {
		 return nil, nilPtrErr
	 }
	 return wrapModelButton(glib.Take(unsafe.Pointer(c))), nil
 }

/*
 * GtkPopoverMenu
 */

// PopoverMenu is a representation of GTK's GtkPopoverMenu.
type PopoverMenu struct {
	Popover
}

func (v *PopoverMenu) native() *C.GtkPopoverMenu {
	if v == nil || v.GObject == nil {
		return nil
	}

	p := unsafe.Pointer(v.GObject)
	return C.toGtkPopoverMenu(p)
}

func marshalPopoverMenu(p uintptr) (interface{}, error) {
	c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
	return wrapPopoverMenu(glib.Take(unsafe.Pointer(c))), nil
}

func wrapPopoverMenu(obj *glib.Object) *PopoverMenu {
	return &PopoverMenu{Popover{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}}}
}

// PopoverMenuNew is a wrapper around gtk_popover_menu_new
func PopoverMenuNew() (*PopoverMenu, error) {
	c := C.gtk_popover_menu_new()
	if c == nil {
		return nil, nilPtrErr
	}
	return wrapPopoverMenu(glib.Take(unsafe.Pointer(c))), nil
}

// OpenSubmenu is a wrapper around gtk_popover_menu_open_submenu
func (v *PopoverMenu) OpenSubmenu(name string) {
	cstr1 := (*C.gchar)(C.CString(name))
	defer C.free(unsafe.Pointer(cstr1))

	C.gtk_popover_menu_open_submenu(v.native(), cstr1)
}

/*
 * GtkStackSidebar
 */

// StackSidebar is a representation of GTK's GtkStackSidebar.
type StackSidebar struct {
	Bin
}

// native returns a pointer to the underlying GtkStack.
func (v *StackSidebar) native() *C.GtkStackSidebar {
	if v == nil || v.GObject == nil {
		return nil
	}
	p := unsafe.Pointer(v.GObject)
	return C.toGtkStackSidebar(p)
}

func marshalStackSidebar(p uintptr) (interface{}, error) {
	c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
	obj := glib.Take(unsafe.Pointer(c))
	return wrapStackSidebar(obj), nil
}

func wrapStackSidebar(obj *glib.Object) *StackSidebar {
	return &StackSidebar{Bin{Container{Widget{glib.InitiallyUnowned{obj}}}}}
}

// StackSidebarNew is a wrapper around gtk_stack_sidebar_new().
func StackSidebarNew() (*StackSidebar, error) {
	c := C.gtk_stack_sidebar_new()
	if c == nil {
		return nil, nilPtrErr
	}
	return wrapStackSidebar(glib.Take(unsafe.Pointer(c))), nil
}

func (v *StackSidebar) SetStack(stack *Stack) {
	C.gtk_stack_sidebar_set_stack(v.native(), stack.native())
}

func (v *StackSidebar) GetStack() *Stack {
	c := C.gtk_stack_sidebar_get_stack(v.native())
	if c == nil {
		return nil
	}
	return wrapStack(glib.Take(unsafe.Pointer(c)))
}

/*
 * GtkEntry
 */

// GrabFocusWithoutSelecting is a wrapper for gtk_entry_grab_focus_without_selecting()
func (v *Entry) GrabFocusWithoutSelecting() {
	C.gtk_entry_grab_focus_without_selecting(v.native())
}

/*
 * GtkTextBuffer
 */

// InsertMarkup() is a wrapper around  gtk_text_buffer_insert_markup()
func (v *TextBuffer) InsertMarkup(start *TextIter, text string) {
	cstr := C.CString(text)
	defer C.free(unsafe.Pointer(cstr))
	C.gtk_text_buffer_insert_markup(v.native(), (*C.GtkTextIter)(start), (*C.gchar)(cstr), C.gint(len(text)))
}
<|endoftext|>


<commit_before>// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package cookie // import "miniflux.app/http/cookie"

import (
	"net/http"
	"time"
)

// Cookie names.
const (
	CookieSessionID     = "sessionID"
	CookieUserSessionID = "userSessionID"

	// Cookie duration in days.
	cookieDuration = 30
)

// New creates a new cookie.
func New(name, value string, isHTTPS bool, path string) *http.Cookie {
	return &http.Cookie{
		Name:     name,
		Value:    value,
		Path:     basePath(path),
		Secure:   isHTTPS,
		HttpOnly: true,
		Expires:  time.Now().Add(cookieDuration * 24 * time.Hour),
	}
}

// Expired returns an expired cookie.
func Expired(name string, isHTTPS bool, path string) *http.Cookie {
	return &http.Cookie{
		Name:     name,
		Value:    "",
		Path:     basePath(path),
		Secure:   isHTTPS,
		HttpOnly: true,
		MaxAge:   -1,
		Expires:  time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
	}
}

func basePath(path string) string {
	if path == "" {
		return "/"
	}
	return path
}
<commit_msg>Set cookie attribute SameSite to strict mode<commit_after>// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package cookie // import "miniflux.app/http/cookie"

import (
	"net/http"
	"time"
)

// Cookie names.
const (
	CookieSessionID     = "sessionID"
	CookieUserSessionID = "userSessionID"

	// Cookie duration in days.
	cookieDuration = 30
)

// New creates a new cookie.
func New(name, value string, isHTTPS bool, path string) *http.Cookie {
	return &http.Cookie{
		Name:     name,
		Value:    value,
		Path:     basePath(path),
		Secure:   isHTTPS,
		HttpOnly: true,
		Expires:  time.Now().Add(cookieDuration * 24 * time.Hour),
		SameSite: http.SameSiteStrictMode,
	}
}

// Expired returns an expired cookie.
func Expired(name string, isHTTPS bool, path string) *http.Cookie {
	return &http.Cookie{
		Name:     name,
		Value:    "",
		Path:     basePath(path),
		Secure:   isHTTPS,
		HttpOnly: true,
		MaxAge:   -1,
		Expires:  time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
		SameSite: http.SameSiteStrictMode,
	}
}

func basePath(path string) string {
	if path == "" {
		return "/"
	}
	return path
}
<|endoftext|>


<commit_before>/*
   Copyright 2013 Juliano Martinez <juliano@martinez.io>

   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.

      Based on http://github.com/nf/webfront

   @author: Juliano Martinez
*/

package http_server

import (
	"errors"
	"fmt"
	"github.com/fiorix/go-redis/redis"
	hpr_utils "github.com/ncode/hot-potato-router/utils"
	"log"
	"net"
	"net/http"
	"net/http/httputil"
	"os"
	"strconv"
	"strings"
	"sync"
	"time"
)

var (
	cfg = hpr_utils.NewConfig()
	rc  = redis.New(cfg.Options["redis"]["server_list"])
)

func xff(req *http.Request) string {
	remote_addr := strings.Split(req.RemoteAddr, ":")
	if len(remote_addr) == 0 {
		return ""
	}
	return remote_addr[0]
}

type Server struct {
	mu      sync.RWMutex
	last    time.Time
	proxy   map[string][]Proxy
	backend map[string]int
}

type Proxy struct {
	Connections int64
	Backend     string
	handler     http.Handler
}

func Listen(fd int, addr string) net.Listener {
	var l net.Listener
	var err error
	if fd >= 3 {
		l, err = net.FileListener(os.NewFile(uintptr(fd), "http"))
	} else {
		l, err = net.Listen("tcp", addr)
	}
	if err != nil {
		log.Fatal(err)
	}
	return l
}

func NewServer(probe time.Duration) (*Server, error) {
	s := new(Server)
	s.proxy = make(map[string][]Proxy)
	s.backend = make(map[string]int)
	go s.probe_backends(probe)
	return s, nil
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if h := s.handler(r); h != nil {
		client := xff(r)
		hpr_utils.Log(fmt.Sprintf("Request from: %s Url: %s", client, r.Host))
		r.Header.Add("X-Forwarded-For", client)
		r.Header.Add("X-Real-IP", client)
		h.ServeHTTP(w, r)
		return
	}
	http.Error(w, "Not found.", http.StatusNotFound)
}

func (s *Server) handler(req *http.Request) http.Handler {
	h := req.Host
	if i := strings.Index(h, ":"); i >= 0 {
		h = h[:i]
	}

	_, ok := s.proxy[h]
	if !ok {
		f, _ := rc.ZRange(fmt.Sprintf("hpr-backends::%s", h), 0, -1, true)
		if len(f) == 0 {
			return nil
		}

		var url string
		for _, be := range f {
			count, err := strconv.Atoi(be)
			if err != nil {
				url = be
				continue
			}

			for r := 0; r <= count; r++ {
				s.mu.Lock()
				s.proxy[h] = append(s.proxy[h],
					Proxy{0, fmt.Sprintf("http://%s", url), makeHandler(url)})
				s.mu.Unlock()
			}
		}
	}
	return s.Next(h)
}

func (s *Server) populate_proxies(host string) (err error) {
	f, _ := rc.ZRange(fmt.Sprintf("hpr-backends::%s", host), 0, -1, true)
	if len(f) == 0 {
		return errors.New("Backend list is empty")
	}

	var url string
	for _, be := range f {
		count, err := strconv.Atoi(be)
		if err != nil {
			url = be
			continue
		}

		for r := 0; r <= count; r++ {
			s.mu.Lock()
			s.proxy[host] = append(s.proxy[host],
				Proxy{0, fmt.Sprintf("http://%s", url), makeHandler(url)})
			s.mu.Unlock()
		}
	}
	return
}

/* TODO: Implement more balance algorithms */
func (s *Server) Next(h string) http.Handler {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.backend[h]++
	total := len(s.proxy[h])
	if s.backend[h] == total {
		s.backend[h] = 0
	}
	hpr_utils.Log(fmt.Sprintf(
		"Using backend: %s Url: %s", s.proxy[h][s.backend[h]].Backend, h))
	return s.proxy[h][s.backend[h]].handler
}

/* TODO: Implement more probes */
func (s *Server) probe_backends(probe time.Duration) {
	transport := http.Transport{Dial: dialTimeout}
	client := &http.Client{
		Transport: &transport,
	}

	for {
		time.Sleep(probe)

		// s.mu.Lock()
		for vhost, backends := range s.proxy {
			err := s.populate_proxies(vhost)
			fmt.Printf("%v", backends)
			fmt.Println(len(backends))
			for backend := range backends {
				fmt.Println(backend)
				hpr_utils.Log(fmt.Sprintf(
					"vhost: %s backends: %s", vhost, s.proxy[vhost][backend].Backend))
				if err != nil {
					hpr_utils.Log(fmt.Sprintf("Removing backend %s", s.proxy[vhost][backend].Backend))
				}
				_, err := client.Get(s.proxy[vhost][backend].Backend)
				if err != nil {
					hpr_utils.Check(err, "Dead backend")
				} else {
					hpr_utils.Log(fmt.Sprintf("Alive: %s", s.proxy[vhost][backend].Backend))
				}
			}

		}
		// s.mu.Unlock()
	}
}

func dialTimeout(network, addr string) (net.Conn, error) {
	timeout := time.Duration(2 * time.Second)
	return net.DialTimeout(network, addr, timeout)
}

func makeHandler(f string) http.Handler {
	if f != "" {
		return &httputil.ReverseProxy{
			Director: func(req *http.Request) {
				req.URL.Scheme = "http"
				req.URL.Host = f
			},
		}
	}
	return nil
}
<commit_msg>cleaning code<commit_after>/*
   Copyright 2013 Juliano Martinez <juliano@martinez.io>

   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.

      Based on http://github.com/nf/webfront

   @author: Juliano Martinez
*/

package http_server

import (
	"errors"
	"fmt"
	"github.com/fiorix/go-redis/redis"
	hpr_utils "github.com/ncode/hot-potato-router/utils"
	"log"
	"net"
	"net/http"
	"net/http/httputil"
	"os"
	"strconv"
	"strings"
	"sync"
	"time"
)

var (
	cfg = hpr_utils.NewConfig()
	rc  = redis.New(cfg.Options["redis"]["server_list"])
)

func xff(req *http.Request) string {
	remote_addr := strings.Split(req.RemoteAddr, ":")
	if len(remote_addr) == 0 {
		return ""
	}
	return remote_addr[0]
}

type Server struct {
	mu      sync.RWMutex
	proxy   map[string][]Proxy
	backend map[string]int
}

type Proxy struct {
	Alive *bool
	//	last    time.Time
	Backend string
	handler http.Handler
}

func Listen(fd int, addr string) net.Listener {
	var l net.Listener
	var err error
	if fd >= 3 {
		l, err = net.FileListener(os.NewFile(uintptr(fd), "http"))
	} else {
		l, err = net.Listen("tcp", addr)
	}
	if err != nil {
		log.Fatal(err)
	}
	return l
}

func NewServer(probe time.Duration) (*Server, error) {
	s := new(Server)
	s.proxy = make(map[string][]Proxy)
	s.backend = make(map[string]int)
	go s.probe_backends(probe)
	return s, nil
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if h := s.handler(r); h != nil {
		client := xff(r)
		hpr_utils.Log(fmt.Sprintf("Request from: %s Url: %s", client, r.Host))
		r.Header.Add("X-Forwarded-For", client)
		r.Header.Add("X-Real-IP", client)
		h.ServeHTTP(w, r)
		return
	}
	http.Error(w, "Not found.", http.StatusNotFound)
}

func (s *Server) handler(req *http.Request) http.Handler {
	h := req.Host
	if i := strings.Index(h, ":"); i >= 0 {
		h = h[:i]
	}

	_, ok := s.proxy[h]
	if !ok {
		s.populate_proxies(h)
	}
	return s.Next(h)
}

func (s *Server) populate_proxies(host string) (err error) {
	f, _ := rc.ZRange(fmt.Sprintf("hpr-backends::%s", host), 0, -1, true)
	if len(f) == 0 {
		return errors.New("Backend list is empty")
	}

	var url string
	for _, be := range f {
		count, err := strconv.Atoi(be)
		if err != nil {
			url = be
			continue
		}

		for r := 0; r <= count; r++ {
			s.mu.Lock()
			b := true
			s.proxy[host] = append(s.proxy[host],
				Proxy{&b, fmt.Sprintf("http://%s", url), makeHandler(url)})
			s.mu.Unlock()
		}
	}
	return
}

/* TODO: Implement more balance algorithms */
func (s *Server) Next(h string) http.Handler {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.backend[h]++
	total := len(s.proxy[h])
	if s.backend[h] == total {
		s.backend[h] = 0
	}
	hpr_utils.Log(fmt.Sprintf(
		"Using backend: %s Url: %s", s.proxy[h][s.backend[h]].Backend, h))
	return s.proxy[h][s.backend[h]].handler
}

/* TODO: Implement more probes */
func (s *Server) probe_backends(probe time.Duration) {
	transport := http.Transport{Dial: dialTimeout}
	client := &http.Client{
		Transport: &transport,
	}

	for {
		time.Sleep(probe)

		// s.mu.Lock()
		for vhost, backends := range s.proxy {
			// err := s.populate_proxies(vhost)
			fmt.Printf("%v", backends)
			fmt.Println(len(backends))
			for backend := range backends {
				fmt.Println(backend)
				hpr_utils.Log(fmt.Sprintf(
					"vhost: %s backends: %s", vhost, s.proxy[vhost][backend].Backend))
				/* if err != nil {
					hpr_utils.Log(fmt.Sprintf("Removing backend %s", s.proxy[vhost][backend].Backend))
				} */
				_, err := client.Get(s.proxy[vhost][backend].Backend)
				if err != nil {
					hpr_utils.Check(err, "Dead backend")
				} else {
					hpr_utils.Log(fmt.Sprintf("Alive: %s", s.proxy[vhost][backend].Backend))

				}
			}

		}
		// s.mu.Unlock()
	}
}

func dialTimeout(network, addr string) (net.Conn, error) {
	timeout := time.Duration(2 * time.Second)
	return net.DialTimeout(network, addr, timeout)
}

func makeHandler(f string) http.Handler {
	if f != "" {
		return &httputil.ReverseProxy{
			Director: func(req *http.Request) {
				req.URL.Scheme = "http"
				req.URL.Host = f
			},
		}
	}
	return nil
}
<|endoftext|>


<commit_before>/*
   Copyright 2013 Juliano Martinez <juliano@martinez.io>

   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.

      Based on http://github.com/nf/webfront

   @author: Juliano Martinez
*/

package http_server

import (
	"errors"
	"fmt"
	"github.com/fiorix/go-redis/redis"
	"github.com/ncode/hot-potato-router/utils"
	"log"
	"net"
	"net/http"
	"net/http/httputil"
	"strconv"
	"strings"
	"sync"
	"time"
)

var (
	cfg = utils.NewConfig()
	rc  = redis.New(cfg.Options["redis"]["server_list"])
)

func xff(req *http.Request) string {
	remote_addr := strings.Split(req.RemoteAddr, ":")
	if len(remote_addr) == 0 {
		return ""
	}
	return remote_addr[0]
}

type Server struct {
	mu      sync.RWMutex
	proxy   map[string][]Proxy
	backend map[string]int
	vcount  map[string]map[string]int
}

type Proxy struct {
	//	last    time.Time
	Backend string
	handler http.Handler
}

func Listen(addr string) net.Listener {
	var l net.Listener
	var err error

	l, err = net.Listen("tcp", addr)
	if err != nil {
		log.Fatal(err)
	}
	return l
}

func NewServer(probe time.Duration) (*Server, error) {
	s := new(Server)
	s.proxy = make(map[string][]Proxy)
	s.backend = make(map[string]int)
	s.vcount = make(map[string]map[string]int)
	go s.probe_backends(probe)
	return s, nil
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if h := s.handler(r); h != nil {
		client := xff(r)
		utils.Log(fmt.Sprintf("Request from: %s Url: %s", client, r.Host))
		r.Header.Add("X-Forwarded-For", client)
		r.Header.Add("X-Real-IP", client)
		h.ServeHTTP(w, r)
		return
	}
	http.Error(w, "Not found.", http.StatusNotFound)
}

func (s *Server) handler(req *http.Request) http.Handler {
	vhost := req.Host
	if i := strings.Index(vhost, ":"); i >= 0 {
		vhost = vhost[:i]
	}

	_, ok := s.proxy[vhost]
	if !ok {
		err := s.populate_proxies(vhost)
		if err != nil {
			utils.Log(fmt.Sprintf("%s for vhost %s", err, vhost))
			return nil
		}
	}
	return s.Next(vhost)
}

func (s *Server) populate_proxies(vhost string) (err error) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	f, _ := rc.ZRange(fmt.Sprintf("hpr-backends::%s", vhost), 0, -1, true)
	if len(f) == 0 {
		if len(s.proxy[vhost]) > 0 {
			delete(s.proxy, vhost)
			delete(s.backend, vhost)
			delete(s.vcount, vhost)
		}
		return errors.New("Backend list is empty")
	}

	var url string
	for _, be := range f {
		count, err := strconv.Atoi(be)
		if err != nil {
			url = be
			continue
		}
		backend := fmt.Sprintf("http://%s", url)
		for r := 1; r <= count; r++ {
			if r <= s.vcount[vhost][backend] {
				continue
			}
			s.proxy[vhost] = append(s.proxy[vhost], Proxy{backend, makeHandler(url)})
			v, ready := s.vcount[vhost]
			if !ready {
				v = make(map[string]int)
				s.vcount[vhost] = v
			}
			s.vcount[vhost][backend]++
			utils.Log(fmt.Sprintf("Backend %s with %d handlers for %s", backend, s.vcount[vhost][backend], vhost))
		}
	}
	return
}

/* TODO: Implement more balance algorithms */
func (s *Server) Next(vhost string) http.Handler {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.backend[vhost]++
	total := len(s.proxy[vhost])
	if s.backend[vhost] >= total {
		s.backend[vhost] = 0
	}
	utils.Log(fmt.Sprintf(
		"Using backend: %s Url: %s", s.proxy[vhost][s.backend[vhost]].Backend, vhost))
	return s.proxy[vhost][s.backend[vhost]].handler
}

/* TODO: Implement more probes */
func (s *Server) probe_backends(probe time.Duration) {
	transport := http.Transport{Dial: dialTimeout}
	client := &http.Client{
		Transport: &transport,
	}

	for {
		time.Sleep(probe)
		for vhost, backends := range s.proxy {
			fmt.Println(backends)
			err := s.populate_proxies(vhost)
			if err != nil {
				utils.Log(fmt.Sprintf("Cleaned entries from vhost: %s", vhost))
				continue
			}
			is_dead := make(map[string]bool)
			removed := 0
			for backend := range backends {
				backend = backend - removed
				utils.Log(fmt.Sprintf(
					"vhost: %s backends: %s", vhost, s.proxy[vhost][backend].Backend))
				if is_dead[s.proxy[vhost][backend].Backend] == true {
					utils.Log(fmt.Sprintf("Removing dead backend: %s", s.proxy[vhost][backend].Backend))
					s.mu.Lock()
					s.proxy[vhost] = s.proxy[vhost][:backend+copy(s.proxy[vhost][backend:], s.proxy[vhost][backend+1:])]
					s.vcount[vhost][s.proxy[vhost][backend].Backend]--
					s.mu.Unlock()
					removed++
					continue
				}

				_, err := client.Get(s.proxy[vhost][backend].Backend)
				if err != nil {
					utils.Log(fmt.Sprintf("Removing dead backend: %s with error %s", s.proxy[vhost][backend].Backend, err))
					is_dead[s.proxy[vhost][backend].Backend] = true
					s.mu.Lock()
					s.proxy[vhost] = s.proxy[vhost][:backend+copy(s.proxy[vhost][backend:], s.proxy[vhost][backend+1:])]
					s.vcount[vhost][s.proxy[vhost][backend].Backend]--
					s.mu.Unlock()
					removed++
				} else {
					utils.Log(fmt.Sprintf("Alive: %s", s.proxy[vhost][backend].Backend))
				}
			}

		}

	}
}

func dialTimeout(network, addr string) (net.Conn, error) {
	timeout := time.Duration(2 * time.Second)
	return net.DialTimeout(network, addr, timeout)
}

func makeHandler(f string) http.Handler {
	if f != "" {
		return &httputil.ReverseProxy{
			Director: func(req *http.Request) {
				req.URL.Scheme = "http"
				req.URL.Host = f
			},
		}
	}
	return nil
}
<commit_msg>debug de bahia no<commit_after>/*
   Copyright 2013 Juliano Martinez <juliano@martinez.io>

   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.

      Based on http://github.com/nf/webfront

   @author: Juliano Martinez
*/

package http_server

import (
	"errors"
	"fmt"
	"github.com/fiorix/go-redis/redis"
	"github.com/ncode/hot-potato-router/utils"
	"log"
	"net"
	"net/http"
	"net/http/httputil"
	"strconv"
	"strings"
	"sync"
	"time"
)

var (
	cfg = utils.NewConfig()
	rc  = redis.New(cfg.Options["redis"]["server_list"])
)

func xff(req *http.Request) string {
	remote_addr := strings.Split(req.RemoteAddr, ":")
	if len(remote_addr) == 0 {
		return ""
	}
	return remote_addr[0]
}

type Server struct {
	mu      sync.RWMutex
	proxy   map[string][]Proxy
	backend map[string]int
	vcount  map[string]map[string]int
}

type Proxy struct {
	//	last    time.Time
	Backend string
	handler http.Handler
}

func Listen(addr string) net.Listener {
	var l net.Listener
	var err error

	l, err = net.Listen("tcp", addr)
	if err != nil {
		log.Fatal(err)
	}
	return l
}

func NewServer(probe time.Duration) (*Server, error) {
	s := new(Server)
	s.proxy = make(map[string][]Proxy)
	s.backend = make(map[string]int)
	s.vcount = make(map[string]map[string]int)
	go s.probe_backends(probe)
	return s, nil
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if h := s.handler(r); h != nil {
		client := xff(r)
		utils.Log(fmt.Sprintf("Request from: %s Url: %s", client, r.Host))
		r.Header.Add("X-Forwarded-For", client)
		r.Header.Add("X-Real-IP", client)
		h.ServeHTTP(w, r)
		return
	}
	http.Error(w, "Not found.", http.StatusNotFound)
}

func (s *Server) handler(req *http.Request) http.Handler {
	vhost := req.Host
	if i := strings.Index(vhost, ":"); i >= 0 {
		vhost = vhost[:i]
	}

	_, ok := s.proxy[vhost]
	if !ok {
		err := s.populate_proxies(vhost)
		if err != nil {
			utils.Log(fmt.Sprintf("%s for vhost %s", err, vhost))
			return nil
		}
	}
	return s.Next(vhost)
}

func (s *Server) populate_proxies(vhost string) (err error) {
	s.mu.RLock()
	defer s.mu.RUnlock()
	f, _ := rc.ZRange(fmt.Sprintf("hpr-backends::%s", vhost), 0, -1, true)
	if len(f) == 0 {
		if len(s.proxy[vhost]) > 0 {
			delete(s.proxy, vhost)
			delete(s.backend, vhost)
			delete(s.vcount, vhost)
		}
		return errors.New("Backend list is empty")
	}

	var url string
	for _, be := range f {
		count, err := strconv.Atoi(be)
		if err != nil {
			url = be
			continue
		}
		backend := fmt.Sprintf("http://%s", url)
		for r := 1; r <= count; r++ {
			if r <= s.vcount[vhost][backend] {
				continue
			}
			s.proxy[vhost] = append(s.proxy[vhost], Proxy{backend, makeHandler(url)})
			v, ready := s.vcount[vhost]
			if !ready {
				v = make(map[string]int)
				s.vcount[vhost] = v
			}
			s.vcount[vhost][backend]++
			utils.Log(fmt.Sprintf("Backend %s with %d handlers for %s", backend, s.vcount[vhost][backend], vhost))
		}
		fmt.Println(s.vcount[vhost][backend])
	}
	return
}

/* TODO: Implement more balance algorithms */
func (s *Server) Next(vhost string) http.Handler {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.backend[vhost]++
	total := len(s.proxy[vhost])
	if s.backend[vhost] >= total {
		s.backend[vhost] = 0
	}
	utils.Log(fmt.Sprintf(
		"Using backend: %s Url: %s", s.proxy[vhost][s.backend[vhost]].Backend, vhost))
	return s.proxy[vhost][s.backend[vhost]].handler
}

/* TODO: Implement more probes */
func (s *Server) probe_backends(probe time.Duration) {
	transport := http.Transport{Dial: dialTimeout}
	client := &http.Client{
		Transport: &transport,
	}

	for {
		time.Sleep(probe)
		for vhost, backends := range s.proxy {
			fmt.Println(backends)
			err := s.populate_proxies(vhost)
			if err != nil {
				utils.Log(fmt.Sprintf("Cleaned entries from vhost: %s", vhost))
				continue
			}
			is_dead := make(map[string]bool)
			removed := 0
			for backend := range backends {
				backend = backend - removed
				utils.Log(fmt.Sprintf(
					"vhost: %s backends: %s", vhost, s.proxy[vhost][backend].Backend))
				if is_dead[s.proxy[vhost][backend].Backend] == true {
					utils.Log(fmt.Sprintf("Removing dead backend: %s", s.proxy[vhost][backend].Backend))
					s.mu.Lock()
					s.proxy[vhost] = s.proxy[vhost][:backend+copy(s.proxy[vhost][backend:], s.proxy[vhost][backend+1:])]
					s.vcount[vhost][s.proxy[vhost][backend].Backend]--
					s.mu.Unlock()
					removed++
					continue
				}

				_, err := client.Get(s.proxy[vhost][backend].Backend)
				if err != nil {
					utils.Log(fmt.Sprintf("Removing dead backend: %s with error %s", s.proxy[vhost][backend].Backend, err))
					is_dead[s.proxy[vhost][backend].Backend] = true
					s.mu.Lock()
					s.proxy[vhost] = s.proxy[vhost][:backend+copy(s.proxy[vhost][backend:], s.proxy[vhost][backend+1:])]
					s.vcount[vhost][s.proxy[vhost][backend].Backend]--
					s.mu.Unlock()
					removed++
				} else {
					utils.Log(fmt.Sprintf("Alive: %s", s.proxy[vhost][backend].Backend))
				}
			}

		}

	}
}

func dialTimeout(network, addr string) (net.Conn, error) {
	timeout := time.Duration(2 * time.Second)
	return net.DialTimeout(network, addr, timeout)
}

func makeHandler(f string) http.Handler {
	if f != "" {
		return &httputil.ReverseProxy{
			Director: func(req *http.Request) {
				req.URL.Scheme = "http"
				req.URL.Host = f
			},
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package missinggo

import "net/http"

// A http.ResponseWriter that tracks the status of the response. The status
// code, and number of bytes written for example.
type StatusResponseWriter struct {
	http.ResponseWriter
	Code         int
	BytesWritten int64
}

func (me *StatusResponseWriter) Write(b []byte) (n int, err error) {
	if me.Code == 0 {
		me.Code = 200
	}
	n, err = me.ResponseWriter.Write(b)
	me.BytesWritten += int64(n)
	return
}

func (me *StatusResponseWriter) WriteHeader(code int) {
	me.ResponseWriter.WriteHeader(code)
	me.Code = code
}
<commit_msg>Add TTFB<commit_after>package missinggo

import (
	"net/http"
	"time"

	"github.com/anacrolix/missinggo/assert"
)

// A http.ResponseWriter that tracks the status of the response. The status
// code, and number of bytes written for example.
type StatusResponseWriter struct {
	http.ResponseWriter
	Code         int
	BytesWritten int64
	Started      time.Time
	Ttfb         time.Duration
}

func (me *StatusResponseWriter) Write(b []byte) (n int, err error) {
	if me.BytesWritten == 0 && len(b) > 0 {
		assert.False(me.Started.IsZero())
		me.Ttfb = time.Since(me.Started)
	}
	n, err = me.ResponseWriter.Write(b)
	me.BytesWritten += int64(n)
	if me.Code == 0 {
		me.Code = 200
	}
	return
}

func (me *StatusResponseWriter) WriteHeader(code int) {
	me.ResponseWriter.WriteHeader(code)
	me.Code = code
}
<|endoftext|>