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

// Package ipaddr provides basic functions for the manipulation of IP
// address prefixes and subsequent addresses as described in RFC 4632
// and RFC 4291.
//
//
// Examples:
//
//	i := big.NewInt(530000)
//	p, err := ipaddr.NewPrefix(net.ParseIP("10.128.0.0"), ipaddr.IPv4PrefixLen-i.BitLen())
//	if err != nil {
//		// error handling
//	}
//	for _, sub := range p.Subnets(3)
//		subs := sub.Subnets(2) {
//		for _, sub := range subs {
//			if sub.Bits(sub.Len()-1, 1)&0x1 != 0 {
//				// do something
//			}
//		}
//		sum := ipaddr.SummaryPrefix(subs[i:j])
//	}
package ipaddr

import (
	"errors"
	"math/big"
	"net"
)

var errInvalidArgument = errors.New("invalid arugument")

// A Prefix represents an IP address prefix.
type Prefix interface {
	// Contains reports whether the prefix includes the given ip.
	Contains(ip net.IP) bool

	// Overlaps reports whether the prefix overlaps with the given
	// prefix.
	Overlaps(prefix Prefix) bool

	// Equal reports whether the prefix and the given prefix are
	// equal.
	Equal(prefix Prefix) bool

	// String returns the string form of the prefix.
	String() string

	// Len returns the length of the prefix in bits.
	Len() int

	// NumAddr returns the number of addresses possible in the
	// prefix.
	NumAddr() *big.Int

	// Bits returns the nbits bit sequence extracted from the
	// prefix at position pos.
	Bits(pos, nbits int) uint32

	// Addr returns the address of the prefix.
	Addr() net.IP

	// LastAddr returns the last address in the address range of
	// the prefix. It returns the address of the prefix when the
	// prefix contains only one address.
	LastAddr() net.IP

	// Hostmask returns the host mask, the inverse mask of the
	// prefix's network mask.
	Hostmask() net.IPMask

	// Netmask returns the network mask of the prefix.
	Netmask() net.IPMask

	// Hosts returns the list of addresses that are assignable to
	// hosts, nodes that are not routers or other intermediate
	// systems, beginning with the given address begin for the
	// prefix neither 0.0.0.0/0 nor ::/0.
	//
	// Note that it will take a bit long time when the prefix is a
	// shorter one, and it also does not distinguish any multicast
	// addresses correctly for now.
	Hosts(begin net.IP) []net.IP

	// HostIter generates and returns the iterator that iterates
	// over the list of addresses that are assignable to hosts,
	// beginning with the given address's next address.
	//
	// Note that it does not identify host-assignable addresses
	// when the prefix is 0.0.0.0/0 or ::/0, and it also does not
	// distinguish any multicast addresses correctly for now.
	HostIter(first net.IP) <-chan net.IP

	// Subnets returns the list of prefixes that are splitted from
	// the prefix, into small address blocks by nbits which
	// represents a number of subnetworks in power of 2 notation.
	Subnets(nbits int) []Prefix

	// SubnetIter generates and returns the iterator that iterates
	// over the list of prefixes that are splitted from the
	// prefix, into small address blocks by nbits which represents
	// a number of subnetworks in power of 2 notation.
	SubnetIter(nbits int) <-chan Prefix

	// Exclude returns the list of prefixes that do not contain
	// the given prefix.
	Exclude(prefix Prefix) []Prefix

	// Set replaces the existing address and prefix length of the
	// prefix with ip and nbits.
	Set(ip net.IP, nbits int) error
}

// NewPrefix returns a new Prefix.
func NewPrefix(ip net.IP, nbits int) (Prefix, error) {
	if ipv4 := ip.To4(); ipv4 != nil && 0 <= nbits && nbits <= IPv4PrefixLen {
		return newIPv4(ipToIPv4Int(ipv4), byte(nbits)), nil
	} else if ipv6 := ip.To16(); ipv6 != nil && ipv6.To4() == nil && 0 <= nbits && nbits <= IPv6PrefixLen {
		return newIPv6(ipToIPv6Int(ipv6), byte(nbits)), nil
	}
	return nil, errInvalidArgument
}

// ComparePrefix returns an integer comparing two prefixes. The result
// will be 0 if a == b, -1 if a < b, and +1 if a > b.
func ComparePrefix(a, b Prefix) int {
	switch a.(type) {
	case *IPv4:
		return ipv4ComparePrefix(a.(*IPv4), b.(*IPv4))
	case *IPv6:
		return ipv6ComparePrefix(a.(*IPv6), b.(*IPv6))
	default:
		panic("unknown address family")
	}
}

// SummaryPrefix tries to find out a shortest common prefix for the
// given prefixes. It returns nil when no suitable prefix is found.
func SummaryPrefix(prefixes []Prefix) Prefix {
	if len(prefixes) == 0 {
		return nil
	} else if len(prefixes) == 1 {
		return prefixes[0]
	}
	switch prefixes[0].(type) {
	case *IPv4:
		p := ipv4SummaryPrefix(prefixes)
		if p == nil {
			return nil
		}
		return p
	case *IPv6:
		p := ipv6SummaryPrefix(prefixes)
		if p == nil {
			return nil
		}
		return p
	default:
		return nil
	}
}
<commit_msg>ipaddr: drop not good examples from doc<commit_after>// Copyright 2013 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.

// Package ipaddr provides basic functions for the manipulation of IP
// address prefixes and subsequent addresses as described in RFC 4632
// and RFC 4291.
package ipaddr

import (
	"errors"
	"math/big"
	"net"
)

var errInvalidArgument = errors.New("invalid arugument")

// A Prefix represents an IP address prefix.
type Prefix interface {
	// Contains reports whether the prefix includes the given ip.
	Contains(ip net.IP) bool

	// Overlaps reports whether the prefix overlaps with the given
	// prefix.
	Overlaps(prefix Prefix) bool

	// Equal reports whether the prefix and the given prefix are
	// equal.
	Equal(prefix Prefix) bool

	// String returns the string form of the prefix.
	String() string

	// Len returns the length of the prefix in bits.
	Len() int

	// NumAddr returns the number of addresses possible in the
	// prefix.
	NumAddr() *big.Int

	// Bits returns the nbits bit sequence extracted from the
	// prefix at position pos.
	Bits(pos, nbits int) uint32

	// Addr returns the address of the prefix.
	Addr() net.IP

	// LastAddr returns the last address in the address range of
	// the prefix. It returns the address of the prefix when the
	// prefix contains only one address.
	LastAddr() net.IP

	// Hostmask returns the host mask, the inverse mask of the
	// prefix's network mask.
	Hostmask() net.IPMask

	// Netmask returns the network mask of the prefix.
	Netmask() net.IPMask

	// Hosts returns the list of addresses that are assignable to
	// hosts, nodes that are not routers or other intermediate
	// systems, beginning with the given address begin for the
	// prefix neither 0.0.0.0/0 nor ::/0.
	//
	// Note that it will take a bit long time when the prefix is a
	// shorter one, and it also does not distinguish any multicast
	// addresses correctly for now.
	Hosts(begin net.IP) []net.IP

	// HostIter generates and returns the iterator that iterates
	// over the list of addresses that are assignable to hosts,
	// beginning with the given address's next address.
	//
	// Note that it does not identify host-assignable addresses
	// when the prefix is 0.0.0.0/0 or ::/0, and it also does not
	// distinguish any multicast addresses correctly for now.
	HostIter(first net.IP) <-chan net.IP

	// Subnets returns the list of prefixes that are splitted from
	// the prefix, into small address blocks by nbits which
	// represents a number of subnetworks in power of 2 notation.
	Subnets(nbits int) []Prefix

	// SubnetIter generates and returns the iterator that iterates
	// over the list of prefixes that are splitted from the
	// prefix, into small address blocks by nbits which represents
	// a number of subnetworks in power of 2 notation.
	SubnetIter(nbits int) <-chan Prefix

	// Exclude returns the list of prefixes that do not contain
	// the given prefix.
	Exclude(prefix Prefix) []Prefix

	// Set replaces the existing address and prefix length of the
	// prefix with ip and nbits.
	Set(ip net.IP, nbits int) error
}

// NewPrefix returns a new Prefix.
func NewPrefix(ip net.IP, nbits int) (Prefix, error) {
	if ipv4 := ip.To4(); ipv4 != nil && 0 <= nbits && nbits <= IPv4PrefixLen {
		return newIPv4(ipToIPv4Int(ipv4), byte(nbits)), nil
	} else if ipv6 := ip.To16(); ipv6 != nil && ipv6.To4() == nil && 0 <= nbits && nbits <= IPv6PrefixLen {
		return newIPv6(ipToIPv6Int(ipv6), byte(nbits)), nil
	}
	return nil, errInvalidArgument
}

// ComparePrefix returns an integer comparing two prefixes. The result
// will be 0 if a == b, -1 if a < b, and +1 if a > b.
func ComparePrefix(a, b Prefix) int {
	switch a.(type) {
	case *IPv4:
		return ipv4ComparePrefix(a.(*IPv4), b.(*IPv4))
	case *IPv6:
		return ipv6ComparePrefix(a.(*IPv6), b.(*IPv6))
	default:
		panic("unknown address family")
	}
}

// SummaryPrefix tries to find out a shortest common prefix for the
// given prefixes. It returns nil when no suitable prefix is found.
func SummaryPrefix(prefixes []Prefix) Prefix {
	if len(prefixes) == 0 {
		return nil
	} else if len(prefixes) == 1 {
		return prefixes[0]
	}
	switch prefixes[0].(type) {
	case *IPv4:
		p := ipv4SummaryPrefix(prefixes)
		if p == nil {
			return nil
		}
		return p
	case *IPv6:
		p := ipv6SummaryPrefix(prefixes)
		if p == nil {
			return nil
		}
		return p
	default:
		return nil
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"math"
	"time"
	"os"
	"strconv"
)

func getPrimes7(n int) []int {
	if n < 2 {
		return []int{}
	}
	if n == 2 {
		return []int{2}
	}

	s := []int{}
	for i := 3; i <= n; i += 2 {
		s = append(s, i)
	}

	var j int
	m := 3
	mroot := int(math.Sqrt(float64(n)))
	half := len(s)
	for i := 0; m <= mroot; {
		if s[i] != 0 {
			j = (m*m - 3) / 2
			s[j] = 0
			for j < half {
				s[j] = 0
				j += m
			}
		}
		i++
		m = 2*i + 3
	}

	res := []int{}
	res = append(res, 2)
	for _, v := range s {
		if v != 0 {
			res = append(res, v)
		}
	}

	return res
}

func main() {
	var startTime = int32(time.Now().Unix())
	var periodTime, _ = strconv.ParseInt(os.Getenv("RUN_TIME"), 10, 32)

	var res []int

	for (int32(time.Now().Unix()) - startTime) < int32(periodTime) {
		res = getPrimes7(10000000)
		fmt.Printf("Found %d prime numbers.\n", len(res))
	}
}
<commit_msg>using time.Since<commit_after>package main

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

func getPrimes7(n int) []int {
	if n < 2 {
		return []int{}
	}
	if n == 2 {
		return []int{2}
	}

	s := []int{}
	for i := 3; i <= n; i += 2 {
		s = append(s, i)
	}

	var j int
	m := 3
	mroot := int(math.Sqrt(float64(n)))
	half := len(s)
	for i := 0; m <= mroot; {
		if s[i] != 0 {
			j = (m*m - 3) / 2
			s[j] = 0
			for j < half {
				s[j] = 0
				j += m
			}
		}
		i++
		m = 2*i + 3
	}

	res := []int{}
	res = append(res, 2)
	for _, v := range s {
		if v != 0 {
			res = append(res, v)
		}
	}

	return res
}

func main() {
	var startTime = time.Now()
	var periodTime, _ = time.ParseDuration(os.Getenv("RUN_TIME") + "s")

	var res []int

	for time.Since(startTime) < periodTime {
		res = getPrimes7(10000000)
		fmt.Printf("Found %d prime numbers.\n", len(res))
	}
}
<|endoftext|>


<commit_before>// probes.go
//
// This file implements the probe API calls

package atlas

import (
	"fmt"
	"github.com/bndr/gopencils"
	"log"
)

// GetProbe returns data for a single probe
func GetProbe(id int) (p *Probe, err error) {
	auth := WantAuth()
	api := gopencils.Api(apiEndpoint, auth)
	r, err := api.Res("probes").Id(id, &p).Get()
	if err != nil {
		err = fmt.Errorf("err: %v - r:%v\n", err, r)
		return
	}
	return
}

// ProbesList is our main answer
type ProbesList struct {
	Count    int
	Next     string
	Previous string
	Results  []Probe
}

// GetProbes returns data for a collection of probes
func GetProbes(opts map[string]string) (p []Probe, err error) {
	log.Printf("GetProbes: opts=%+v", opts)
	auth := WantAuth()
	api := gopencils.Api(apiEndpoint, auth)

	var rawlist ProbesList

	r, err := api.Res("probes", &rawlist).Get(opts)
	log.Printf("rawlist=%+v r=%+v", rawlist, r)
	if err != nil {
		err = fmt.Errorf("%v - r:%v\n", err, r)
		return
	}

	// Empty answer
	if rawlist.Count == 0 {
		return nil, fmt.Errorf("empty probe list")
	}

	if rawlist.Next != "" {
		// We have pagination

	}
	p = rawlist.Results
	fmt.Printf("r: %#v\np: %#v\n", r, p)
	return
}
<commit_msg>Implement pagination of results.<commit_after>// probes.go
//
// This file implements the probe API calls

package atlas

import (
	"fmt"
	"github.com/bndr/gopencils"
	"log"
	"regexp"
	"strconv"
)

// GetProbe returns data for a single probe
func GetProbe(id int) (p *Probe, err error) {
	auth := WantAuth()
	api := gopencils.Api(apiEndpoint, auth)
	r, err := api.Res("probes").Id(id, &p).Get()
	if err != nil {
		err = fmt.Errorf("err: %v - r:%v\n", err, r)
		return
	}
	return
}

// ProbesList is our main answer
type ProbesList struct {
	Count    int
	Next     string
	Previous string
	Results  []Probe
}

// getPageNum returns the value of the page= parameter
func getPageNum(url string) (page int) {
	re := regexp.MustCompile(`page=(\d+)`)
	if m := re.FindStringSubmatch(url); len(m) >= 0 {
		pn, _ := strconv.ParseInt(m[1], 10, 32)
		return int(pn)
	}
	return 0
}

// GetProbes returns data for a collection of probes
func GetProbes(opts map[string]string) (p []Probe, err error) {
	log.Printf("GetProbes: opts=%+v", opts)
	auth := WantAuth()
	api := gopencils.Api(apiEndpoint, auth)

	var rawlist ProbesList

	r, err := api.Res("probes", &rawlist).Get(opts)
	log.Printf("rawlist=%+v r=%+v", rawlist, r)
	if err != nil {
		err = fmt.Errorf("%v - r:%v\n", err, r)
		return
	}

	// Empty answer
	if rawlist.Count == 0 {
		return nil, fmt.Errorf("empty probe list")
	}

	var res []Probe

	res = append(res, rawlist.Results...)
	if rawlist.Next != "" {
		// We have pagination
		for pn := 2; pn != 0; getPageNum(rawlist.Next) {
			opts["page"] = string(pn)
			r, err = api.Res("probes", &rawlist).Get(opts)
			res = append(res, rawlist.Results...)
		}
	}
	p = res
	fmt.Printf("r: %#v\np: %#v\n", r, p)
	return
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
)

func smallest(x[] int) int {
	small:= x[0]
	for _, value := range x {
		if value < small {
			small = value
		}
	}
	return small;
}

func main() {

	x := []int{
		48, 96, 86, 68,
		57, 82, 63, 70,
		37, 34, 83, 27,
		19, 97, 9, 17,
	}
	small := smallest(x)

	fmt.Println("Smallest Number is ", small)

}
<commit_msg>Formatted<commit_after>package main

import (
	"fmt"
)

func smallest(x []int) int {
	small := x[0]
	for _, value := range x {
		if value < small {
			small = value
		}
	}
	return small
}

func main() {

	x := []int{
		48, 96, 86, 68,
		57, 82, 63, 70,
		37, 34, 83, 27,
		19, 97, 9, 17,
	}
	small := smallest(x)

	fmt.Println("Smallest Number is ", small)

}
<|endoftext|>


<commit_before>package memsearch

import (
	"fmt"
	"github.com/mozilla/masche/memaccess"
	"os"
	"os/exec"
	"testing"
)

var needle []byte = []byte("Find This!")

var buffersToFind = [][]byte{
	[]byte{0xc, 0xa, 0xf, 0xe},
	[]byte{0xd, 0xe, 0xa, 0xd, 0xb, 0xe, 0xe, 0xf},
	[]byte{0xb, 0xe, 0xb, 0xe, 0xf, 0xe, 0x0},
}

var notPresent = []byte("this string should generate a list of bytes not present in the process")

func printSoftErrors(softerrors []error) {
	for _, err := range softerrors {
		fmt.Fprintln(os.Stderr, err.Error())
	}
}

func TestSearchInOtherProcess(t *testing.T) {
	//TODO(mvanotti): Right now the command is hardcoded. We should decide how to fix this.
	cmd := exec.Command("../test/tools/test.exe")

	childout, err := cmd.StdoutPipe()
	if err != nil {
		t.Fatal(err)
	}
	defer childout.Close()

	if err := cmd.Start(); err != nil {
		t.Fatal(err)
	}
	defer cmd.Process.Kill()

	// Wait until the process writes something to stdout, so we know it has initialized all its memory.
	if read, err := childout.Read(make([]byte, 1)); err != nil || read != 1 {
		t.Fatal(err)
	}

	pid := uint(cmd.Process.Pid)
	reader, err, softerrors := memaccess.NewProcessMemoryReader(pid)
	printSoftErrors(softerrors)
	if err != nil {
		t.Fatal(err)
	}
	defer reader.Close()

	for i, buf := range buffersToFind {
		found, _, err, softerrors := FindNext(reader, 0, buf)
		printSoftErrors(softerrors)
		if err != nil {
			t.Fatal(err)
		} else if !found {
			t.Fatalf("memoryGrep failed for case %d, the following buffer should be found: %+v", i, buf)
		}
	}

	// This must not be present
	found, _, err, softerrors := FindNext(reader, 0, notPresent)
	printSoftErrors(softerrors)
	if err != nil {
		t.Fatal(err)
	} else if found {
		t.Fatalf("memoryGrep failed, it found a sequense of bytes that it shouldn't")
	}

}

func testFindString(t *testing.T) {
	pid := uint(os.Getpid())

	reader, err, softerrors := memaccess.NewProcessMemoryReader(pid)
	printSoftErrors(softerrors)
	if err != nil {
		t.Fatal(err)
	}
	defer reader.Close()

	found, _, err, softerrors := FindNext(reader, 0, needle)
	printSoftErrors(softerrors)
	if err != nil {
		t.Fatal(err)
	} else if !found {
		t.Fatalf("memoryGrep failed, searching for %s, should be True", needle)
	}
}
<commit_msg>[memsearch] create launchProcess & testFindString<commit_after>package memsearch

import (
	"fmt"
	"github.com/mozilla/masche/memaccess"
	"os"
	"os/exec"
	"testing"
)

var needle []byte = []byte("Find This!")

var buffersToFind = [][]byte{
	[]byte{0xc, 0xa, 0xf, 0xe},
	[]byte{0xd, 0xe, 0xa, 0xd, 0xb, 0xe, 0xe, 0xf},
	[]byte{0xb, 0xe, 0xb, 0xe, 0xf, 0xe, 0x0},
}

var notPresent = []byte("this string should generate a list of bytes not present in the process")

func printSoftErrors(softerrors []error) {
	for _, err := range softerrors {
		fmt.Fprintln(os.Stderr, err.Error())
	}
}

// launchProcess starts a process and waits until it writes something to stdout: that way we know it has been initialized.
// the process launched should write to stdout once it has been fully initialized.
func launchProcess(file string) (*exec.Cmd, error) {
	cmd := exec.Command(file)

	childout, err := cmd.StdoutPipe()
	if err != nil {
		return nil, err
	}
	defer childout.Close()

	if err := cmd.Start(); err != nil {
		return nil, err
	}

	// Wait until the process writes something to stdout, so we know it has initialized all its memory.
	if read, err := childout.Read(make([]byte, 1)); err != nil || read != 1 {
		closeProcess(cmd)
		return nil, err
	}

	return cmd, nil
}

func closeProcess(cmd *exec.Cmd) {
	cmd.Process.Kill()
}

func TestSearchInOtherProcess(t *testing.T) {
	//TODO(mvanotti): Right now the command is hardcoded. We should decide how to fix this.
	cmd, err := launchProcess("../test/tools/test.exe")
	if err != nil {
		t.Fatal(err)
	}
	defer closeProcess(cmd)

	pid := uint(cmd.Process.Pid)
	reader, err, softerrors := memaccess.NewProcessMemoryReader(pid)
	printSoftErrors(softerrors)
	if err != nil {
		t.Fatal(err)
	}
	defer reader.Close()

	for i, buf := range buffersToFind {
		found, _, err, softerrors := FindNext(reader, 0, buf)
		printSoftErrors(softerrors)
		if err != nil {
			t.Fatal(err)
		} else if !found {
			t.Fatalf("memoryGrep failed for case %d, the following buffer should be found: %+v", i, buf)
		}
	}

	// This must not be present
	found, _, err, softerrors := FindNext(reader, 0, notPresent)
	printSoftErrors(softerrors)
	if err != nil {
		t.Fatal(err)
	} else if found {
		t.Fatalf("memoryGrep failed, it found a sequense of bytes that it shouldn't")
	}
}
<|endoftext|>


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

// This file contains the mechanism to "linkify" html source
// text containing EBNF sections (as found in go_spec.html).
// The result is the input source text with the EBNF sections
// modified such that identifiers are linked to the respective
// definitions.

package main

import (
	"bytes"
	"fmt"
	"go/scanner"
	"go/token"
	"io"
)

type ebnfParser struct {
	out     io.Writer   // parser output
	src     []byte      // parser source
	file    *token.File // for position information
	scanner scanner.Scanner
	prev    int         // offset of previous token
	pos     token.Pos   // token position
	tok     token.Token // one token look-ahead
	lit     []byte      // token literal
}

func (p *ebnfParser) flush() {
	offs := p.file.Offset(p.pos)
	p.out.Write(p.src[p.prev:offs])
	p.prev = offs
}

func (p *ebnfParser) next() {
	p.flush()
	p.pos, p.tok, p.lit = p.scanner.Scan()
	if p.tok.IsKeyword() {
		// TODO Should keyword mapping always happen outside scanner?
		//      Or should there be a flag to scanner to enable keyword mapping?
		p.tok = token.IDENT
	}
}

func (p *ebnfParser) Error(pos token.Position, msg string) {
	fmt.Fprintf(p.out, `<span class="alert">error: %s</span>`, msg)
}

func (p *ebnfParser) errorExpected(pos token.Pos, msg string) {
	msg = "expected " + msg
	if pos == p.pos {
		// the error happened at the current position;
		// make the error message more specific
		msg += ", found '" + p.tok.String() + "'"
		if p.tok.IsLiteral() {
			msg += " " + string(p.lit)
		}
	}
	p.Error(p.file.Position(pos), msg)
}

func (p *ebnfParser) expect(tok token.Token) token.Pos {
	pos := p.pos
	if p.tok != tok {
		p.errorExpected(pos, "'"+tok.String()+"'")
	}
	p.next() // make progress in any case
	return pos
}

func (p *ebnfParser) parseIdentifier(def bool) {
	name := string(p.lit)
	p.expect(token.IDENT)
	if def {
		fmt.Fprintf(p.out, `<a id="%s">%s</a>`, name, name)
	} else {
		fmt.Fprintf(p.out, `<a href="#%s" class="noline">%s</a>`, name, name)
	}
	p.prev += len(name) // skip identifier when calling flush
}

func (p *ebnfParser) parseTerm() bool {
	switch p.tok {
	case token.IDENT:
		p.parseIdentifier(false)

	case token.STRING:
		p.next()
		if p.tok == token.ELLIPSIS {
			p.next()
			p.expect(token.STRING)
		}

	case token.LPAREN:
		p.next()
		p.parseExpression()
		p.expect(token.RPAREN)

	case token.LBRACK:
		p.next()
		p.parseExpression()
		p.expect(token.RBRACK)

	case token.LBRACE:
		p.next()
		p.parseExpression()
		p.expect(token.RBRACE)

	default:
		return false
	}

	return true
}

func (p *ebnfParser) parseSequence() {
	for p.parseTerm() {
	}
}

func (p *ebnfParser) parseExpression() {
	for {
		p.parseSequence()
		if p.tok != token.OR {
			break
		}
		p.next()
	}
}

func (p *ebnfParser) parseProduction() {
	p.parseIdentifier(true)
	p.expect(token.ASSIGN)
	p.parseExpression()
	p.expect(token.PERIOD)
}

func (p *ebnfParser) parse(fset *token.FileSet, out io.Writer, src []byte) {
	// initialize ebnfParser
	p.out = out
	p.src = src
	p.file = p.scanner.Init(fset, "", src, p, 0)
	p.next() // initializes pos, tok, lit

	// process source
	for p.tok != token.EOF {
		p.parseProduction()
	}
	p.flush()
}

// Markers around EBNF sections
var (
	openTag  = []byte(`<pre class="ebnf">`)
	closeTag = []byte(`</pre>`)
)

func linkify(out io.Writer, src []byte) {
	fset := token.NewFileSet()
	for len(src) > 0 {
		n := len(src)

		// i: beginning of EBNF text (or end of source)
		i := bytes.Index(src, openTag)
		if i < 0 {
			i = n - len(openTag)
		}
		i += len(openTag)

		// j: end of EBNF text (or end of source)
		j := bytes.Index(src[i:n], closeTag) // close marker
		if j < 0 {
			j = n - i
		}
		j += i

		// write text before EBNF
		out.Write(src[0:i])
		// parse and write EBNF
		var p ebnfParser
		p.parse(fset, out, src[i:j])

		// advance
		src = src[j:n]
	}
}
<commit_msg>godoc: don't call flush if position is not valid (fixes new godoc crash when looking at spec)<commit_after>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file contains the mechanism to "linkify" html source
// text containing EBNF sections (as found in go_spec.html).
// The result is the input source text with the EBNF sections
// modified such that identifiers are linked to the respective
// definitions.

package main

import (
	"bytes"
	"fmt"
	"go/scanner"
	"go/token"
	"io"
)

type ebnfParser struct {
	out     io.Writer   // parser output
	src     []byte      // parser source
	file    *token.File // for position information
	scanner scanner.Scanner
	prev    int         // offset of previous token
	pos     token.Pos   // token position
	tok     token.Token // one token look-ahead
	lit     []byte      // token literal
}

func (p *ebnfParser) flush() {
	offs := p.file.Offset(p.pos)
	p.out.Write(p.src[p.prev:offs])
	p.prev = offs
}

func (p *ebnfParser) next() {
	if p.pos.IsValid() {
		p.flush()
	}
	p.pos, p.tok, p.lit = p.scanner.Scan()
	if p.tok.IsKeyword() {
		// TODO Should keyword mapping always happen outside scanner?
		//      Or should there be a flag to scanner to enable keyword mapping?
		p.tok = token.IDENT
	}
}

func (p *ebnfParser) Error(pos token.Position, msg string) {
	fmt.Fprintf(p.out, `<span class="alert">error: %s</span>`, msg)
}

func (p *ebnfParser) errorExpected(pos token.Pos, msg string) {
	msg = "expected " + msg
	if pos == p.pos {
		// the error happened at the current position;
		// make the error message more specific
		msg += ", found '" + p.tok.String() + "'"
		if p.tok.IsLiteral() {
			msg += " " + string(p.lit)
		}
	}
	p.Error(p.file.Position(pos), msg)
}

func (p *ebnfParser) expect(tok token.Token) token.Pos {
	pos := p.pos
	if p.tok != tok {
		p.errorExpected(pos, "'"+tok.String()+"'")
	}
	p.next() // make progress in any case
	return pos
}

func (p *ebnfParser) parseIdentifier(def bool) {
	name := string(p.lit)
	p.expect(token.IDENT)
	if def {
		fmt.Fprintf(p.out, `<a id="%s">%s</a>`, name, name)
	} else {
		fmt.Fprintf(p.out, `<a href="#%s" class="noline">%s</a>`, name, name)
	}
	p.prev += len(name) // skip identifier when calling flush
}

func (p *ebnfParser) parseTerm() bool {
	switch p.tok {
	case token.IDENT:
		p.parseIdentifier(false)

	case token.STRING:
		p.next()
		if p.tok == token.ELLIPSIS {
			p.next()
			p.expect(token.STRING)
		}

	case token.LPAREN:
		p.next()
		p.parseExpression()
		p.expect(token.RPAREN)

	case token.LBRACK:
		p.next()
		p.parseExpression()
		p.expect(token.RBRACK)

	case token.LBRACE:
		p.next()
		p.parseExpression()
		p.expect(token.RBRACE)

	default:
		return false
	}

	return true
}

func (p *ebnfParser) parseSequence() {
	for p.parseTerm() {
	}
}

func (p *ebnfParser) parseExpression() {
	for {
		p.parseSequence()
		if p.tok != token.OR {
			break
		}
		p.next()
	}
}

func (p *ebnfParser) parseProduction() {
	p.parseIdentifier(true)
	p.expect(token.ASSIGN)
	p.parseExpression()
	p.expect(token.PERIOD)
}

func (p *ebnfParser) parse(fset *token.FileSet, out io.Writer, src []byte) {
	// initialize ebnfParser
	p.out = out
	p.src = src
	p.file = p.scanner.Init(fset, "", src, p, 0)
	p.next() // initializes pos, tok, lit

	// process source
	for p.tok != token.EOF {
		p.parseProduction()
	}
	p.flush()
}

// Markers around EBNF sections
var (
	openTag  = []byte(`<pre class="ebnf">`)
	closeTag = []byte(`</pre>`)
)

func linkify(out io.Writer, src []byte) {
	fset := token.NewFileSet()
	for len(src) > 0 {
		n := len(src)

		// i: beginning of EBNF text (or end of source)
		i := bytes.Index(src, openTag)
		if i < 0 {
			i = n - len(openTag)
		}
		i += len(openTag)

		// j: end of EBNF text (or end of source)
		j := bytes.Index(src[i:n], closeTag) // close marker
		if j < 0 {
			j = n - i
		}
		j += i

		// write text before EBNF
		out.Write(src[0:i])
		// parse and write EBNF
		var p ebnfParser
		p.parse(fset, out, src[i:j])

		// advance
		src = src[j:n]
	}
}
<|endoftext|>


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

package main

import (
	"bufio"
	"cmd/internal/browser"
	"flag"
	"fmt"
	"html/template"
	"internal/trace"
	"io"
	"log"
	"net"
	"net/http"
	"os"
	"runtime"
	"runtime/debug"
	"sync"

	_ "net/http/pprof" // Required to use pprof
)

const usageMessage = "" +
	`Usage of 'go tool trace':
Given a trace file produced by 'go test':
	go test -trace=trace.out pkg

Open a web browser displaying trace:
	go tool trace [flags] [pkg.test] trace.out

Generate a pprof-like profile from the trace:
    go tool trace -pprof=TYPE [pkg.test] trace.out

[pkg.test] argument is required for traces produced by Go 1.6 and below.
Go 1.7 does not require the binary argument.

Supported profile types are:
    - net: network blocking profile
    - sync: synchronization blocking profile
    - syscall: syscall blocking profile
    - sched: scheduler latency profile

Flags:
	-http=addr: HTTP service address (e.g., ':6060')
	-pprof=type: print a pprof-like profile instead
	-d: print debug info such as parsed events

Note that while the various profiles available when launching
'go tool trace' work on every browser, the trace viewer itself
(the 'view trace' page) comes from the Chrome/Chromium project
and is only actively tested on that browser.
`

var (
	httpFlag  = flag.String("http", "localhost:0", "HTTP service address (e.g., ':6060')")
	pprofFlag = flag.String("pprof", "", "print a pprof-like profile instead")
	debugFlag = flag.Bool("d", false, "print debug information such as parsed events list")

	// The binary file name, left here for serveSVGProfile.
	programBinary string
	traceFile     string
)

func main() {
	flag.Usage = func() {
		fmt.Fprintln(os.Stderr, usageMessage)
		os.Exit(2)
	}
	flag.Parse()

	// Go 1.7 traces embed symbol info and does not require the binary.
	// But we optionally accept binary as first arg for Go 1.5 traces.
	switch flag.NArg() {
	case 1:
		traceFile = flag.Arg(0)
	case 2:
		programBinary = flag.Arg(0)
		traceFile = flag.Arg(1)
	default:
		flag.Usage()
	}

	var pprofFunc func(io.Writer, *http.Request) error
	switch *pprofFlag {
	case "net":
		pprofFunc = pprofByGoroutine(computePprofIO)
	case "sync":
		pprofFunc = pprofByGoroutine(computePprofBlock)
	case "syscall":
		pprofFunc = pprofByGoroutine(computePprofSyscall)
	case "sched":
		pprofFunc = pprofByGoroutine(computePprofSched)
	}
	if pprofFunc != nil {
		if err := pprofFunc(os.Stdout, &http.Request{}); err != nil {
			dief("failed to generate pprof: %v\n", err)
		}
		os.Exit(0)
	}
	if *pprofFlag != "" {
		dief("unknown pprof type %s\n", *pprofFlag)
	}

	ln, err := net.Listen("tcp", *httpFlag)
	if err != nil {
		dief("failed to create server socket: %v\n", err)
	}

	log.Print("Parsing trace...")
	res, err := parseTrace()
	if err != nil {
		dief("%v\n", err)
	}

	if *debugFlag {
		trace.Print(res.Events)
		os.Exit(0)
	}
	reportMemoryUsage("after parsing trace")
	debug.FreeOSMemory()

	log.Print("Splitting trace...")
	ranges = splitTrace(res)
	reportMemoryUsage("after spliting trace")
	debug.FreeOSMemory()

	addr := "http://" + ln.Addr().String()
	log.Printf("Opening browser. Trace viewer is listening on %s", addr)
	browser.Open(addr)

	// Start http server.
	http.HandleFunc("/", httpMain)
	err = http.Serve(ln, nil)
	dief("failed to start http server: %v\n", err)
}

var ranges []Range

var loader struct {
	once sync.Once
	res  trace.ParseResult
	err  error
}

// parseEvents is a compatibility wrapper that returns only
// the Events part of trace.ParseResult returned by parseTrace.
func parseEvents() ([]*trace.Event, error) {
	res, err := parseTrace()
	if err != nil {
		return nil, err
	}
	return res.Events, err
}

func parseTrace() (trace.ParseResult, error) {
	loader.once.Do(func() {
		tracef, err := os.Open(traceFile)
		if err != nil {
			loader.err = fmt.Errorf("failed to open trace file: %v", err)
			return
		}
		defer tracef.Close()

		// Parse and symbolize.
		res, err := trace.Parse(bufio.NewReader(tracef), programBinary)
		if err != nil {
			loader.err = fmt.Errorf("failed to parse trace: %v", err)
			return
		}
		loader.res = res
	})
	return loader.res, loader.err
}

// httpMain serves the starting page.
func httpMain(w http.ResponseWriter, r *http.Request) {
	if err := templMain.Execute(w, ranges); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

var templMain = template.Must(template.New("").Parse(`

{{if $}}
	{{range $e := $}}
		View trace ({{$e.Name}})

	{{end}}

{{else}}
	View trace

{{end}}
Goroutine analysis

Network blocking profile (⬇)

Synchronization blocking profile (⬇)

Syscall blocking profile (⬇)

Scheduler latency profile (⬇)

User-defined tasks

User-defined regions

Minimum mutator utilization

`))

func dief(msg string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, msg, args...)
	os.Exit(1)
}

var debugMemoryUsage bool

func init() {
	v := os.Getenv("DEBUG_MEMORY_USAGE")
	debugMemoryUsage = v != ""
}

func reportMemoryUsage(msg string) {
	if !debugMemoryUsage {
		return
	}
	var s runtime.MemStats
	runtime.ReadMemStats(&s)
	w := os.Stderr
	fmt.Fprintf(w, "%s\n", msg)
	fmt.Fprintf(w, " Alloc:\t%d Bytes\n", s.Alloc)
	fmt.Fprintf(w, " Sys:\t%d Bytes\n", s.Sys)
	fmt.Fprintf(w, " HeapReleased:\t%d Bytes\n", s.HeapReleased)
	fmt.Fprintf(w, " HeapSys:\t%d Bytes\n", s.HeapSys)
	fmt.Fprintf(w, " HeapInUse:\t%d Bytes\n", s.HeapInuse)
	fmt.Fprintf(w, " HeapAlloc:\t%d Bytes\n", s.HeapAlloc)
	var dummy string
	fmt.Printf("Enter to continue...")
	fmt.Scanf("%s", &dummy)
}
<commit_msg>cmd/trace: use fmt.Print for newline-ending fixed string<commit_after>// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bufio"
	"cmd/internal/browser"
	"flag"
	"fmt"
	"html/template"
	"internal/trace"
	"io"
	"log"
	"net"
	"net/http"
	"os"
	"runtime"
	"runtime/debug"
	"sync"

	_ "net/http/pprof" // Required to use pprof
)

const usageMessage = "" +
	`Usage of 'go tool trace':
Given a trace file produced by 'go test':
	go test -trace=trace.out pkg

Open a web browser displaying trace:
	go tool trace [flags] [pkg.test] trace.out

Generate a pprof-like profile from the trace:
    go tool trace -pprof=TYPE [pkg.test] trace.out

[pkg.test] argument is required for traces produced by Go 1.6 and below.
Go 1.7 does not require the binary argument.

Supported profile types are:
    - net: network blocking profile
    - sync: synchronization blocking profile
    - syscall: syscall blocking profile
    - sched: scheduler latency profile

Flags:
	-http=addr: HTTP service address (e.g., ':6060')
	-pprof=type: print a pprof-like profile instead
	-d: print debug info such as parsed events

Note that while the various profiles available when launching
'go tool trace' work on every browser, the trace viewer itself
(the 'view trace' page) comes from the Chrome/Chromium project
and is only actively tested on that browser.
`

var (
	httpFlag  = flag.String("http", "localhost:0", "HTTP service address (e.g., ':6060')")
	pprofFlag = flag.String("pprof", "", "print a pprof-like profile instead")
	debugFlag = flag.Bool("d", false, "print debug information such as parsed events list")

	// The binary file name, left here for serveSVGProfile.
	programBinary string
	traceFile     string
)

func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, usageMessage)
		os.Exit(2)
	}
	flag.Parse()

	// Go 1.7 traces embed symbol info and does not require the binary.
	// But we optionally accept binary as first arg for Go 1.5 traces.
	switch flag.NArg() {
	case 1:
		traceFile = flag.Arg(0)
	case 2:
		programBinary = flag.Arg(0)
		traceFile = flag.Arg(1)
	default:
		flag.Usage()
	}

	var pprofFunc func(io.Writer, *http.Request) error
	switch *pprofFlag {
	case "net":
		pprofFunc = pprofByGoroutine(computePprofIO)
	case "sync":
		pprofFunc = pprofByGoroutine(computePprofBlock)
	case "syscall":
		pprofFunc = pprofByGoroutine(computePprofSyscall)
	case "sched":
		pprofFunc = pprofByGoroutine(computePprofSched)
	}
	if pprofFunc != nil {
		if err := pprofFunc(os.Stdout, &http.Request{}); err != nil {
			dief("failed to generate pprof: %v\n", err)
		}
		os.Exit(0)
	}
	if *pprofFlag != "" {
		dief("unknown pprof type %s\n", *pprofFlag)
	}

	ln, err := net.Listen("tcp", *httpFlag)
	if err != nil {
		dief("failed to create server socket: %v\n", err)
	}

	log.Print("Parsing trace...")
	res, err := parseTrace()
	if err != nil {
		dief("%v\n", err)
	}

	if *debugFlag {
		trace.Print(res.Events)
		os.Exit(0)
	}
	reportMemoryUsage("after parsing trace")
	debug.FreeOSMemory()

	log.Print("Splitting trace...")
	ranges = splitTrace(res)
	reportMemoryUsage("after spliting trace")
	debug.FreeOSMemory()

	addr := "http://" + ln.Addr().String()
	log.Printf("Opening browser. Trace viewer is listening on %s", addr)
	browser.Open(addr)

	// Start http server.
	http.HandleFunc("/", httpMain)
	err = http.Serve(ln, nil)
	dief("failed to start http server: %v\n", err)
}

var ranges []Range

var loader struct {
	once sync.Once
	res  trace.ParseResult
	err  error
}

// parseEvents is a compatibility wrapper that returns only
// the Events part of trace.ParseResult returned by parseTrace.
func parseEvents() ([]*trace.Event, error) {
	res, err := parseTrace()
	if err != nil {
		return nil, err
	}
	return res.Events, err
}

func parseTrace() (trace.ParseResult, error) {
	loader.once.Do(func() {
		tracef, err := os.Open(traceFile)
		if err != nil {
			loader.err = fmt.Errorf("failed to open trace file: %v", err)
			return
		}
		defer tracef.Close()

		// Parse and symbolize.
		res, err := trace.Parse(bufio.NewReader(tracef), programBinary)
		if err != nil {
			loader.err = fmt.Errorf("failed to parse trace: %v", err)
			return
		}
		loader.res = res
	})
	return loader.res, loader.err
}

// httpMain serves the starting page.
func httpMain(w http.ResponseWriter, r *http.Request) {
	if err := templMain.Execute(w, ranges); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

var templMain = template.Must(template.New("").Parse(`

{{if $}}
	{{range $e := $}}
		View trace ({{$e.Name}})

	{{end}}

{{else}}
	View trace

{{end}}
Goroutine analysis

Network blocking profile (⬇)

Synchronization blocking profile (⬇)

Syscall blocking profile (⬇)

Scheduler latency profile (⬇)

User-defined tasks

User-defined regions

Minimum mutator utilization

`))

func dief(msg string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, msg, args...)
	os.Exit(1)
}

var debugMemoryUsage bool

func init() {
	v := os.Getenv("DEBUG_MEMORY_USAGE")
	debugMemoryUsage = v != ""
}

func reportMemoryUsage(msg string) {
	if !debugMemoryUsage {
		return
	}
	var s runtime.MemStats
	runtime.ReadMemStats(&s)
	w := os.Stderr
	fmt.Fprintf(w, "%s\n", msg)
	fmt.Fprintf(w, " Alloc:\t%d Bytes\n", s.Alloc)
	fmt.Fprintf(w, " Sys:\t%d Bytes\n", s.Sys)
	fmt.Fprintf(w, " HeapReleased:\t%d Bytes\n", s.HeapReleased)
	fmt.Fprintf(w, " HeapSys:\t%d Bytes\n", s.HeapSys)
	fmt.Fprintf(w, " HeapInUse:\t%d Bytes\n", s.HeapInuse)
	fmt.Fprintf(w, " HeapAlloc:\t%d Bytes\n", s.HeapAlloc)
	var dummy string
	fmt.Printf("Enter to continue...")
	fmt.Scanf("%s", &dummy)
}
<|endoftext|>


<commit_before>package main

import (
    "fmt"
    "menteslibres.net/gosexy/redis"
)

func main() {

    var client  *redis.Client
    var err error

    client = redis.New()

    err = client.Connect("127.0.0.1",6973)

    if err != nil {
        fmt.Println("Connect failed: %s\n")
        return
    }

    fmt.Println("Connected to redis-server.")

  // Get list of Current coffee Items
    //var cl int64 // variable to store length of "coffees"
    cl,err := client.LLen("coffees")
    // grab "coffees" list and store it in "Coffees" array
    Coffees := make([]string,int(cl))
    Coffees,err = client.LRange("coffees",0,-1)
    //print out each of the items
    for n := 0; n <= int(cl); n++ {
        fmt.Println(Coffees[n])
    }

    client.Quit()

}

<commit_msg>changed server port # and changed how to print list<commit_after>package main

import (
    "fmt"
    "menteslibres.net/gosexy/redis"
)

func main() {

    var client  *redis.Client
    var err error

    client = redis.New()

    err = client.Connect("127.0.0.1",6379)

    if err != nil {
        fmt.Println("Connect failed: %s\n")
        return
    }

    fmt.Println("Connected to redis-server.")

  // Get list of Current coffee Items
    cl,err := client.LLen("coffees")// cl is a variable to store length of "coffees"
    // grab "coffees" list and store it in "Coffees" array
    Coffees := make([]string,int(cl))
    Coffees,err = client.LRange("coffees",0,-1)
    //print out each of the items
    fmt.Print(Coffees)
//    for n := 0; n <= int(cl); n++ {
//        fmt.Println(Coffees[n])
//    }

    client.Quit()

}

<|endoftext|>


<commit_before>package trace

import (
	"context"
	"fmt"
	"github.com/shawnfeng/sutil/sconf/center"
	"github.com/shawnfeng/sutil/slog/slog"
	"net/http"
	"strings"
	"sync"
)

const FilterUrls = "span_filter_urls"

const ListConfigSep = ","

var initApolloLock sync.Mutex

var apolloCenter center.ConfigCenter

var apolloSpanFilterConfig *spanFilterConfig

func InitTraceSpanFilter() error {
	fun := "TraceSpanFilter.init --> "
	ctx := context.Background()

	if err := initApolloCenter(ctx); err != nil {
		return err
	}

	urls, ok := apolloCenter.GetStringWithNamespace(ctx, center.DefaultApolloTraceNamespace, FilterUrls)
	if !ok {
		return fmt.Errorf("not get %s from apollo namespace %s", FilterUrls, center.DefaultApolloTraceNamespace)
	}
	slog.Infof(ctx, "%s get %s from apollo: %s", fun, FilterUrls, urls)

	urlList := strings.Split(urls, ListConfigSep)

	apolloSpanFilterConfig = &spanFilterConfig{
		urls: urlList,
	}

	apolloCenter.RegisterObserver(ctx, apolloSpanFilterConfig)
	return nil
}

func initApolloCenter(ctx context.Context) error {
	if apolloCenter != nil {
		return nil
	}

	initApolloLock.Lock()
	defer initApolloLock.Unlock()

	if apolloCenter != nil {
		return nil
	}

	var err error
	apolloCenter, err = center.NewConfigCenter(center.ApolloConfigCenter)
	if err != nil {
		return fmt.Errorf("new config center error, %s", err.Error())
	}

	namespaceList := []string{center.DefaultApolloTraceNamespace}
	err = apolloCenter.Init(ctx, center.DefaultApolloMiddlewareService, namespaceList)
	if err != nil {
		return fmt.Errorf("init apollo with service %s namespace %s error, %s",
			center.DefaultApolloMiddlewareService, strings.Join(namespaceList, " "), err.Error())
	}

	apolloCenter.StartWatchUpdate(ctx)
	return nil
}

type spanFilterConfig struct {
	mu sync.RWMutex

	urls []string
}

func (m *spanFilterConfig) updateUrls(urls []string) {
	m.mu.Lock()
	defer m.mu.Unlock()

	m.urls = urls
}

func (m *spanFilterConfig) filterUrl(url string) bool {
	m.mu.RLock()
	defer m.mu.RUnlock()

	for _, u := range m.urls {
		if u == url {
			return false
		}
	}

	return true
}

func (m *spanFilterConfig) HandleChangeEvent(event *center.ChangeEvent) {
	fun := "spanFilterConfig.HandleChangeEvent --> "
	ctx := context.Background()

	if event.Namespace != center.DefaultApolloTraceNamespace {
		return
	}

	for key, change := range event.Changes {
		if key == FilterUrls {
			slog.Infof(ctx, "%s get key %s from apollo, old value: %s, new value: %s", fun, key, change.OldValue, change.NewValue)
			urlList := strings.Split(change.NewValue, ListConfigSep)
			m.updateUrls(urlList)
		}
	}
}

func UrlSpanFilter(r *http.Request) bool {
	if apolloSpanFilterConfig != nil {
		return apolloSpanFilterConfig.filterUrl(r.URL.Path)
	}

	return true
}
<commit_msg>Modify apollo center init<commit_after>package trace

import (
	"context"
	"fmt"
	"github.com/shawnfeng/sutil/sconf/center"
	"github.com/shawnfeng/sutil/slog/slog"
	"net/http"
	"strings"
	"sync"
)

const FilterUrls = "span_filter_urls"

const ListConfigSep = ","

var initApolloLock sync.Mutex

var apolloCenter center.ConfigCenter

var apolloSpanFilterConfig *spanFilterConfig

func InitTraceSpanFilter() error {
	fun := "TraceSpanFilter.init --> "
	ctx := context.Background()

	if err := initApolloCenter(ctx); err != nil {
		return err
	}

	urls, ok := apolloCenter.GetStringWithNamespace(ctx, center.DefaultApolloTraceNamespace, FilterUrls)
	if !ok {
		return fmt.Errorf("not get %s from apollo namespace %s", FilterUrls, center.DefaultApolloTraceNamespace)
	}
	slog.Infof(ctx, "%s get %s from apollo: %s", fun, FilterUrls, urls)

	urlList := strings.Split(urls, ListConfigSep)

	apolloSpanFilterConfig = &spanFilterConfig{
		urls: urlList,
	}

	apolloCenter.RegisterObserver(ctx, apolloSpanFilterConfig)
	return nil
}

func initApolloCenter(ctx context.Context) error {
	if apolloCenter != nil {
		return nil
	}

	initApolloLock.Lock()
	defer initApolloLock.Unlock()

	if apolloCenter != nil {
		return nil
	}

	newCenter, err := center.NewConfigCenter(center.ApolloConfigCenter)
	if err != nil {
		return fmt.Errorf("new config center error, %s", err.Error())
	}

	namespaceList := []string{center.DefaultApolloTraceNamespace}
	err = newCenter.Init(ctx, center.DefaultApolloMiddlewareService, namespaceList)
	if err != nil {
		return fmt.Errorf("init apollo with service %s namespace %s error, %s",
			center.DefaultApolloMiddlewareService, strings.Join(namespaceList, " "), err.Error())
	}

	newCenter.StartWatchUpdate(ctx)

	apolloCenter = newCenter
	return nil
}

type spanFilterConfig struct {
	mu sync.RWMutex

	urls []string
}

func (m *spanFilterConfig) updateUrls(urls []string) {
	m.mu.Lock()
	defer m.mu.Unlock()

	m.urls = urls
}

func (m *spanFilterConfig) filterUrl(url string) bool {
	m.mu.RLock()
	defer m.mu.RUnlock()

	for _, u := range m.urls {
		if u == url {
			return false
		}
	}

	return true
}

func (m *spanFilterConfig) HandleChangeEvent(event *center.ChangeEvent) {
	fun := "spanFilterConfig.HandleChangeEvent --> "
	ctx := context.Background()

	if event.Namespace != center.DefaultApolloTraceNamespace {
		return
	}

	for key, change := range event.Changes {
		if key == FilterUrls {
			slog.Infof(ctx, "%s get key %s from apollo, old value: %s, new value: %s", fun, key, change.OldValue, change.NewValue)
			urlList := strings.Split(change.NewValue, ListConfigSep)
			m.updateUrls(urlList)
		}
	}
}

func UrlSpanFilter(r *http.Request) bool {
	if apolloSpanFilterConfig != nil {
		return apolloSpanFilterConfig.filterUrl(r.URL.Path)
	}

	return true
}
<|endoftext|>


<commit_before>package transport

import (
	"crypto/tls"
	"net"
	"os"
	"time"

	"github.com/cloudflare/cfssl/csr"
	"github.com/cloudflare/cfssl/errors"
	"github.com/cloudflare/cfssl/log"
	"github.com/cloudflare/cfssl/revoke"
	"github.com/cloudflare/cfssl/transport/ca"
	"github.com/cloudflare/cfssl/transport/core"
	"github.com/cloudflare/cfssl/transport/kp"
	"github.com/cloudflare/cfssl/transport/roots"
)

func envOrDefault(key, def string) string {
	val := os.Getenv(key)
	if val == "" {
		return def
	}
	return val
}

var (
	// NewKeyProvider is the function used to build key providers
	// from some identity.
	NewKeyProvider = func(id *core.Identity) (kp.KeyProvider, error) {
		return kp.NewStandardProvider(id)
	}

	// NewCA is used to load a configuration for a certificate
	// authority.
	NewCA = func(id *core.Identity) (ca.CertificateAuthority, error) {
		return ca.NewCFSSLProvider(id, nil)
	}
)

// A Transport is capable of providing transport-layer security using
// TLS.
type Transport struct {
	// Before defines how long before the certificate expires the
	// transport should start attempting to refresh the
	// certificate. For example, if this is 24h, then 24 hours
	// before the certificate expires the Transport will start
	// attempting to replace it.
	Before time.Duration

	// Provider contains a key management provider.
	Provider kp.KeyProvider

	// CA contains a mechanism for obtaining signed certificates.
	CA ca.CertificateAuthority

	// TrustStore contains the certificates trusted by this
	// transport.
	TrustStore *roots.TrustStore

	// ClientTrustStore contains the certificate authorities to
	// use in verifying client authentication certificates.
	ClientTrustStore *roots.TrustStore

	// Identity contains information about the entity that will be
	// used to construct certificates.
	Identity *core.Identity

	// Backoff is used to control the behaviour of a Transport
	// when it is attempting to automatically update a certificate
	// as part of AutoUpdate.
	Backoff *core.Backoff

	// RevokeSoftFail, if true, will cause a failure to check
	// revocation (such that the revocation status of a
	// certificate cannot be checked) to not be treated as an
	// error.
	RevokeSoftFail bool
}

// TLSClientAuthClientConfig returns a new client authentication TLS
// configuration that can be used for a client using client auth
// connecting to the named host.
func (tr *Transport) TLSClientAuthClientConfig(host string) (*tls.Config, error) {
	cert, err := tr.getCertificate()
	if err != nil {
		return nil, err
	}

	return &tls.Config{
		Certificates: []tls.Certificate{cert},
		RootCAs:      tr.TrustStore.Pool(),
		ServerName:   host,
		CipherSuites: core.CipherSuites,
		MinVersion:   tls.VersionTLS12,
		ClientAuth:   tls.RequireAndVerifyClientCert,
	}, nil
}

// TLSClientAuthServerConfig returns a new client authentication TLS
// configuration for servers expecting mutually authenticated
// clients. The clientAuth parameter should contain the root pool used
// to authenticate clients.
func (tr *Transport) TLSClientAuthServerConfig() (*tls.Config, error) {
	cert, err := tr.getCertificate()
	if err != nil {
		return nil, err
	}

	return &tls.Config{
		Certificates: []tls.Certificate{cert},
		RootCAs:      tr.TrustStore.Pool(),
		ClientCAs:    tr.ClientTrustStore.Pool(),
		ClientAuth:   tls.RequireAndVerifyClientCert,
		CipherSuites: core.CipherSuites,
		MinVersion:   tls.VersionTLS12,
	}, nil
}

// TLSServerConfig is a general server configuration that should be
// used for non-client authentication purposes, such as HTTPS.
func (tr *Transport) TLSServerConfig() (*tls.Config, error) {
	cert, err := tr.getCertificate()
	if err != nil {
		return nil, err
	}

	return &tls.Config{
		Certificates: []tls.Certificate{cert},
		CipherSuites: core.CipherSuites,
		MinVersion:   tls.VersionTLS12,
	}, nil
}

// New builds a new transport from an identity and a before time. The
// before time tells the transport how long before the certificate
// expires to start attempting to update when auto-updating. If before
// is longer than the certificate's lifetime, every update check will
// trigger a new certificate to be generated.
func New(before time.Duration, identity *core.Identity) (*Transport, error) {
	var tr = &Transport{
		Before:   before,
		Identity: identity,
		Backoff:  &core.Backoff{},
	}

	store, err := roots.New(identity.Roots)
	if err != nil {
		return nil, err
	}
	tr.TrustStore = store

	if len(identity.ClientRoots) > 0 {
		store, err = roots.New(identity.ClientRoots)
		if err != nil {
			return nil, err
		}
		tr.ClientTrustStore = store
	}

	tr.Provider, err = NewKeyProvider(identity)
	if err != nil {
		return nil, err
	}

	tr.CA, err = NewCA(identity)
	if err != nil {
		return nil, err
	}

	return tr, nil
}

// Lifespan returns how much time is left before the transport's
// certificate expires, or 0 if the certificate is not present or
// expired.
func (tr *Transport) Lifespan() time.Duration {
	cert := tr.Provider.Certificate()
	if cert == nil {
		return 0
	}

	now := time.Now()
	if now.After(cert.NotAfter) {
		return 0
	}

	now = now.Add(tr.Before)
	ls := cert.NotAfter.Sub(now)
	if ls < 0 {
		return 0
	}
	return ls
}

// RefreshKeys will make sure the Transport has loaded keys and has a
// valid certificate. It will handle any persistence, check that the
// certificate is valid (i.e. that its expiry date is within the
// Before date), and handle certificate reissuance as needed.
func (tr *Transport) RefreshKeys() (err error) {
	if !tr.Provider.Ready() {
		log.Debug("key and certificate aren't ready, loading")
		err = tr.Provider.Load()
		if err != nil && err != kp.ErrCertificateUnavailable {
			log.Debugf("failed to load keypair: %v", err)
			kr := tr.Identity.Request.KeyRequest
			if kr == nil {
				kr = csr.NewBasicKeyRequest()
			}

			err = tr.Provider.Generate(kr.Algo(), kr.Size())
			if err != nil {
				log.Debugf("failed to generate key: %v", err)
				return
			}
		}
	}

	lifespan := tr.Lifespan()
	if lifespan < tr.Before {
		log.Debugf("transport's certificate is out of date (lifespan %s)", lifespan)
		req, err := tr.Provider.CertificateRequest(tr.Identity.Request)
		if err != nil {
			log.Debugf("couldn't get a CSR: %v", err)
			return err
		}

		log.Debug("requesting certificate from CA")
		cert, err := tr.CA.SignCSR(req)
		if err != nil {
			log.Debugf("failed to get the certificate signed: %v", err)
			return err
		}

		log.Debug("giving the certificate to the provider")
		err = tr.Provider.SetCertificatePEM(cert)
		if err != nil {
			log.Debugf("failed to set the provider's certificate: %v", err)
			return err
		}

		log.Debug("storing the certificate")
		err = tr.Provider.Store()
		if err != nil {
			log.Debugf("the provider failed to store the certificate: %v", err)
			return err
		}
	}

	return nil
}

func (tr *Transport) getCertificate() (cert tls.Certificate, err error) {
	if !tr.Provider.Ready() {
		log.Debug("transport isn't ready; attempting to refresh keypair")
		err = tr.RefreshKeys()
		if err != nil {
			log.Debugf("transport couldn't get a certificate: %v", err)
			return
		}
	}

	cert, err = tr.Provider.X509KeyPair()
	if err != nil {
		log.Debugf("couldn't generate an X.509 keypair: %v", err)
	}

	return
}

// Dial initiates a TLS connection to an outbound server. It returns a
// TLS connection to the server.
func Dial(address string, tr *Transport) (*tls.Conn, error) {
	host, _, err := net.SplitHostPort(address)
	if err != nil {
		// Assume address is a hostname, and that it should
		// use the HTTPS port number.
		host = address
		address = net.JoinHostPort(address, "443")
	}

	cfg, err := tr.TLSClientAuthClientConfig(host)
	if err != nil {
		return nil, err
	}

	conn, err := tls.Dial("tcp", address, cfg)
	if err != nil {
		return nil, err
	}

	state := conn.ConnectionState()
	if len(state.VerifiedChains) == 0 {
		return nil, errors.New(errors.CertificateError, errors.VerifyFailed)
	}

	for _, chain := range state.VerifiedChains {
		for _, cert := range chain {
			revoked, ok := revoke.VerifyCertificate(cert)
			if (!tr.RevokeSoftFail && !ok) || revoked {
				return nil, errors.New(errors.CertificateError, errors.VerifyFailed)
			}
		}
	}

	return conn, nil
}

// AutoUpdate will automatically update the listener. If a non-nil
// certUpdates chan is provided, it will receive timestamps for
// reissued certificates. If errChan is non-nil, any errors that occur
// in the updater will be passed along.
func (tr *Transport) AutoUpdate(certUpdates chan<- time.Time, errChan chan<- error) {
	defer func() {
		if r := recover(); r != nil {
			log.Criticalf("AutoUpdate panicked: %v", r)
		}
	}()

	for {
		// Wait until it's time to update the certificate.
		target := time.Now().Add(tr.Lifespan())
		if PollInterval == 0 {
			<-time.After(tr.Lifespan())
		} else {
			pollWait(target)
		}

		// Keep trying to update the certificate until it's
		// ready.
		for {
			log.Debugf("attempting to refresh keypair")
			err := tr.RefreshKeys()
			if err == nil {
				break
			}

			delay := tr.Backoff.Duration()
			log.Debugf("failed to update certificate, will try again in %s", delay)
			if errChan != nil {
				errChan <- err
			}

			<-time.After(delay)
		}

		log.Debugf("certificate updated")
		if certUpdates != nil {
			certUpdates <- time.Now()
		}

		tr.Backoff.Reset()
	}
}
<commit_msg>transport: only call Store() if provider is persistent.<commit_after>package transport

import (
	"crypto/tls"
	"net"
	"os"
	"time"

	"github.com/cloudflare/cfssl/csr"
	"github.com/cloudflare/cfssl/errors"
	"github.com/cloudflare/cfssl/log"
	"github.com/cloudflare/cfssl/revoke"
	"github.com/cloudflare/cfssl/transport/ca"
	"github.com/cloudflare/cfssl/transport/core"
	"github.com/cloudflare/cfssl/transport/kp"
	"github.com/cloudflare/cfssl/transport/roots"
)

func envOrDefault(key, def string) string {
	val := os.Getenv(key)
	if val == "" {
		return def
	}
	return val
}

var (
	// NewKeyProvider is the function used to build key providers
	// from some identity.
	NewKeyProvider = func(id *core.Identity) (kp.KeyProvider, error) {
		return kp.NewStandardProvider(id)
	}

	// NewCA is used to load a configuration for a certificate
	// authority.
	NewCA = func(id *core.Identity) (ca.CertificateAuthority, error) {
		return ca.NewCFSSLProvider(id, nil)
	}
)

// A Transport is capable of providing transport-layer security using
// TLS.
type Transport struct {
	// Before defines how long before the certificate expires the
	// transport should start attempting to refresh the
	// certificate. For example, if this is 24h, then 24 hours
	// before the certificate expires the Transport will start
	// attempting to replace it.
	Before time.Duration

	// Provider contains a key management provider.
	Provider kp.KeyProvider

	// CA contains a mechanism for obtaining signed certificates.
	CA ca.CertificateAuthority

	// TrustStore contains the certificates trusted by this
	// transport.
	TrustStore *roots.TrustStore

	// ClientTrustStore contains the certificate authorities to
	// use in verifying client authentication certificates.
	ClientTrustStore *roots.TrustStore

	// Identity contains information about the entity that will be
	// used to construct certificates.
	Identity *core.Identity

	// Backoff is used to control the behaviour of a Transport
	// when it is attempting to automatically update a certificate
	// as part of AutoUpdate.
	Backoff *core.Backoff

	// RevokeSoftFail, if true, will cause a failure to check
	// revocation (such that the revocation status of a
	// certificate cannot be checked) to not be treated as an
	// error.
	RevokeSoftFail bool
}

// TLSClientAuthClientConfig returns a new client authentication TLS
// configuration that can be used for a client using client auth
// connecting to the named host.
func (tr *Transport) TLSClientAuthClientConfig(host string) (*tls.Config, error) {
	cert, err := tr.getCertificate()
	if err != nil {
		return nil, err
	}

	return &tls.Config{
		Certificates: []tls.Certificate{cert},
		RootCAs:      tr.TrustStore.Pool(),
		ServerName:   host,
		CipherSuites: core.CipherSuites,
		MinVersion:   tls.VersionTLS12,
		ClientAuth:   tls.RequireAndVerifyClientCert,
	}, nil
}

// TLSClientAuthServerConfig returns a new client authentication TLS
// configuration for servers expecting mutually authenticated
// clients. The clientAuth parameter should contain the root pool used
// to authenticate clients.
func (tr *Transport) TLSClientAuthServerConfig() (*tls.Config, error) {
	cert, err := tr.getCertificate()
	if err != nil {
		return nil, err
	}

	return &tls.Config{
		Certificates: []tls.Certificate{cert},
		RootCAs:      tr.TrustStore.Pool(),
		ClientCAs:    tr.ClientTrustStore.Pool(),
		ClientAuth:   tls.RequireAndVerifyClientCert,
		CipherSuites: core.CipherSuites,
		MinVersion:   tls.VersionTLS12,
	}, nil
}

// TLSServerConfig is a general server configuration that should be
// used for non-client authentication purposes, such as HTTPS.
func (tr *Transport) TLSServerConfig() (*tls.Config, error) {
	cert, err := tr.getCertificate()
	if err != nil {
		return nil, err
	}

	return &tls.Config{
		Certificates: []tls.Certificate{cert},
		CipherSuites: core.CipherSuites,
		MinVersion:   tls.VersionTLS12,
	}, nil
}

// New builds a new transport from an identity and a before time. The
// before time tells the transport how long before the certificate
// expires to start attempting to update when auto-updating. If before
// is longer than the certificate's lifetime, every update check will
// trigger a new certificate to be generated.
func New(before time.Duration, identity *core.Identity) (*Transport, error) {
	var tr = &Transport{
		Before:   before,
		Identity: identity,
		Backoff:  &core.Backoff{},
	}

	store, err := roots.New(identity.Roots)
	if err != nil {
		return nil, err
	}
	tr.TrustStore = store

	if len(identity.ClientRoots) > 0 {
		store, err = roots.New(identity.ClientRoots)
		if err != nil {
			return nil, err
		}
		tr.ClientTrustStore = store
	}

	tr.Provider, err = NewKeyProvider(identity)
	if err != nil {
		return nil, err
	}

	tr.CA, err = NewCA(identity)
	if err != nil {
		return nil, err
	}

	return tr, nil
}

// Lifespan returns how much time is left before the transport's
// certificate expires, or 0 if the certificate is not present or
// expired.
func (tr *Transport) Lifespan() time.Duration {
	cert := tr.Provider.Certificate()
	if cert == nil {
		return 0
	}

	now := time.Now()
	if now.After(cert.NotAfter) {
		return 0
	}

	now = now.Add(tr.Before)
	ls := cert.NotAfter.Sub(now)
	if ls < 0 {
		return 0
	}
	return ls
}

// RefreshKeys will make sure the Transport has loaded keys and has a
// valid certificate. It will handle any persistence, check that the
// certificate is valid (i.e. that its expiry date is within the
// Before date), and handle certificate reissuance as needed.
func (tr *Transport) RefreshKeys() (err error) {
	if !tr.Provider.Ready() {
		log.Debug("key and certificate aren't ready, loading")
		err = tr.Provider.Load()
		if err != nil && err != kp.ErrCertificateUnavailable {
			log.Debugf("failed to load keypair: %v", err)
			kr := tr.Identity.Request.KeyRequest
			if kr == nil {
				kr = csr.NewBasicKeyRequest()
			}

			err = tr.Provider.Generate(kr.Algo(), kr.Size())
			if err != nil {
				log.Debugf("failed to generate key: %v", err)
				return
			}
		}
	}

	lifespan := tr.Lifespan()
	if lifespan < tr.Before {
		log.Debugf("transport's certificate is out of date (lifespan %s)", lifespan)
		req, err := tr.Provider.CertificateRequest(tr.Identity.Request)
		if err != nil {
			log.Debugf("couldn't get a CSR: %v", err)
			return err
		}

		log.Debug("requesting certificate from CA")
		cert, err := tr.CA.SignCSR(req)
		if err != nil {
			log.Debugf("failed to get the certificate signed: %v", err)
			return err
		}

		log.Debug("giving the certificate to the provider")
		err = tr.Provider.SetCertificatePEM(cert)
		if err != nil {
			log.Debugf("failed to set the provider's certificate: %v", err)
			return err
		}

		if tr.Provider.Persistent() {
			log.Debug("storing the certificate")
			err = tr.Provider.Store()
			if err != nil {
				log.Debugf("the provider failed to store the certificate: %v", err)
				return err
			}
		}
	}

	return nil
}

func (tr *Transport) getCertificate() (cert tls.Certificate, err error) {
	if !tr.Provider.Ready() {
		log.Debug("transport isn't ready; attempting to refresh keypair")
		err = tr.RefreshKeys()
		if err != nil {
			log.Debugf("transport couldn't get a certificate: %v", err)
			return
		}
	}

	cert, err = tr.Provider.X509KeyPair()
	if err != nil {
		log.Debugf("couldn't generate an X.509 keypair: %v", err)
	}

	return
}

// Dial initiates a TLS connection to an outbound server. It returns a
// TLS connection to the server.
func Dial(address string, tr *Transport) (*tls.Conn, error) {
	host, _, err := net.SplitHostPort(address)
	if err != nil {
		// Assume address is a hostname, and that it should
		// use the HTTPS port number.
		host = address
		address = net.JoinHostPort(address, "443")
	}

	cfg, err := tr.TLSClientAuthClientConfig(host)
	if err != nil {
		return nil, err
	}

	conn, err := tls.Dial("tcp", address, cfg)
	if err != nil {
		return nil, err
	}

	state := conn.ConnectionState()
	if len(state.VerifiedChains) == 0 {
		return nil, errors.New(errors.CertificateError, errors.VerifyFailed)
	}

	for _, chain := range state.VerifiedChains {
		for _, cert := range chain {
			revoked, ok := revoke.VerifyCertificate(cert)
			if (!tr.RevokeSoftFail && !ok) || revoked {
				return nil, errors.New(errors.CertificateError, errors.VerifyFailed)
			}
		}
	}

	return conn, nil
}

// AutoUpdate will automatically update the listener. If a non-nil
// certUpdates chan is provided, it will receive timestamps for
// reissued certificates. If errChan is non-nil, any errors that occur
// in the updater will be passed along.
func (tr *Transport) AutoUpdate(certUpdates chan<- time.Time, errChan chan<- error) {
	defer func() {
		if r := recover(); r != nil {
			log.Criticalf("AutoUpdate panicked: %v", r)
		}
	}()

	for {
		// Wait until it's time to update the certificate.
		target := time.Now().Add(tr.Lifespan())
		if PollInterval == 0 {
			<-time.After(tr.Lifespan())
		} else {
			pollWait(target)
		}

		// Keep trying to update the certificate until it's
		// ready.
		for {
			log.Debugf("attempting to refresh keypair")
			err := tr.RefreshKeys()
			if err == nil {
				break
			}

			delay := tr.Backoff.Duration()
			log.Debugf("failed to update certificate, will try again in %s", delay)
			if errChan != nil {
				errChan <- err
			}

			<-time.After(delay)
		}

		log.Debugf("certificate updated")
		if certUpdates != nil {
			certUpdates <- time.Now()
		}

		tr.Backoff.Reset()
	}
}
<|endoftext|>


<commit_before>// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// 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 sdb

import (
	. "github.com/jacobsa/oglematchers"
	. "github.com/jacobsa/ogletest"
	"testing"
)

func TestPut(t *testing.T) { RunTests(t) }

////////////////////////////////////////////////////////////////////////
// PutAttributes
////////////////////////////////////////////////////////////////////////

type PutTest struct {
	domainTest

	item ItemName
	updates []PutUpdate
	preconditions []Precondition

	err error
}

func init() { RegisterTestSuite(&PutTest{}) }

func (t *PutTest) SetUp(i *TestInfo) {
	// Call common setup code.
	t.domainTest.SetUp(i)

	// Make the request legal by default.
	t.item = "foo"
	t.updates = []PutUpdate{PutUpdate{"bar", "baz", false}}
}

func (t *PutTest) callDomain() {
	t.err = t.domain.PutAttributes(t.item, t.updates, t.preconditions)
}

func (t *PutTest) EmptyItemName() {
	t.item = ""

	// Call
	t.callDomain()

	ExpectThat(t.err, Error(HasSubstr("Invalid")))
	ExpectThat(t.err, Error(HasSubstr("item name")))
}

func (t *PutTest) InvalidItemName() {
	ExpectEq("TODO", "")
}

func (t *PutTest) ZeroUpdates() {
	ExpectEq("TODO", "")
}

func (t *PutTest) TooManyUpdates() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OneAttributeNameEmpty() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OneAttributeNameInvalid() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OneAttributeValueInvalid() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OnePreconditionNameInvalid() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OnePreconditionValueInvalid() {
	ExpectEq("TODO", "")
}

func (t *PutTest) NoPreconditions() {
	ExpectEq("TODO", "")
}

func (t *PutTest) SomePreconditions() {
	ExpectEq("TODO", "")
}

func (t *PutTest) ConnReturnsError() {
	ExpectEq("TODO", "")
}

func (t *PutTest) ConnSaysOkay() {
	ExpectEq("TODO", "")
}

////////////////////////////////////////////////////////////////////////
// BatchPutAttributes
////////////////////////////////////////////////////////////////////////

type BatchPutTest struct {
	domainTest
}

func init() { RegisterTestSuite(&BatchPutTest{}) }

func (t *BatchPutTest) DoesFoo() {
	ExpectEq("TODO", "")
}
<commit_msg>PutTest.InvalidItemName<commit_after>// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// 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 sdb

import (
	. "github.com/jacobsa/oglematchers"
	. "github.com/jacobsa/ogletest"
	"testing"
)

func TestPut(t *testing.T) { RunTests(t) }

////////////////////////////////////////////////////////////////////////
// PutAttributes
////////////////////////////////////////////////////////////////////////

type PutTest struct {
	domainTest

	item ItemName
	updates []PutUpdate
	preconditions []Precondition

	err error
}

func init() { RegisterTestSuite(&PutTest{}) }

func (t *PutTest) SetUp(i *TestInfo) {
	// Call common setup code.
	t.domainTest.SetUp(i)

	// Make the request legal by default.
	t.item = "foo"
	t.updates = []PutUpdate{PutUpdate{"bar", "baz", false}}
}

func (t *PutTest) callDomain() {
	t.err = t.domain.PutAttributes(t.item, t.updates, t.preconditions)
}

func (t *PutTest) EmptyItemName() {
	t.item = ""

	// Call
	t.callDomain()

	ExpectThat(t.err, Error(HasSubstr("Invalid")))
	ExpectThat(t.err, Error(HasSubstr("item name")))
}

func (t *PutTest) InvalidItemName() {
	t.item = "taco\x80\x81\x82"

	// Call
	t.callDomain()

	ExpectThat(t.err, Error(HasSubstr("Invalid")))
	ExpectThat(t.err, Error(HasSubstr("item name")))
	ExpectThat(t.err, Error(HasSubstr(string(t.item))))
}

func (t *PutTest) ZeroUpdates() {
	ExpectEq("TODO", "")
}

func (t *PutTest) TooManyUpdates() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OneAttributeNameEmpty() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OneAttributeNameInvalid() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OneAttributeValueInvalid() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OnePreconditionNameInvalid() {
	ExpectEq("TODO", "")
}

func (t *PutTest) OnePreconditionValueInvalid() {
	ExpectEq("TODO", "")
}

func (t *PutTest) NoPreconditions() {
	ExpectEq("TODO", "")
}

func (t *PutTest) SomePreconditions() {
	ExpectEq("TODO", "")
}

func (t *PutTest) ConnReturnsError() {
	ExpectEq("TODO", "")
}

func (t *PutTest) ConnSaysOkay() {
	ExpectEq("TODO", "")
}

////////////////////////////////////////////////////////////////////////
// BatchPutAttributes
////////////////////////////////////////////////////////////////////////

type BatchPutTest struct {
	domainTest
}

func init() { RegisterTestSuite(&BatchPutTest{}) }

func (t *BatchPutTest) DoesFoo() {
	ExpectEq("TODO", "")
}
<|endoftext|>


<commit_before>package service

import (
	"sync"
	"time"

	"github.com/ReneGa/tweetcount-microservices/recorder/datamapper"
	"github.com/ReneGa/tweetcount-microservices/recorder/domain"
	"github.com/ReneGa/tweetcount-microservices/recorder/gateway"
)

type Tweets struct {
	sync.Mutex
	DataMapper       *datamapper.Queries
	Gateway          gateway.Tweets
	writerRegistered map[string]bool
}

func copyTweets(from chan domain.Tweet, to chan domain.Tweet) {
	for tweet := range from {
		to <- tweet
	}
}

func (t *Tweets) registerWriter(query string) bool {
	t.Lock()
	if t.writerRegistered == nil {
		t.writerRegistered = map[string]bool{}
	}
	alreadyRegistered := t.writerRegistered[query]
	t.writerRegistered[query] = true
	t.Unlock()
	return !alreadyRegistered
}

func (t *Tweets) unregisterWriter(query string) {
	t.Lock()
	t.writerRegistered[query] = false
	t.Unlock()
}

func (t *Tweets) copyAndRecordTweets(query string, history datamapper.Tweets, freshTweets domain.Tweets, out chan domain.Tweet, stop chan bool) {
	writeHistory := false
	defer func() {
		if writeHistory {
			t.unregisterWriter(query)
		}
	}()
	for {
		select {
		case tweet := <-freshTweets.Data:
			writeHistory := t.registerWriter(query)
			if writeHistory {
				history.Append(tweet)
			}
			out <- tweet
		case <-stop:
			freshTweets.Stop <- true
			return
		}
	}
}

func (t *Tweets) Tweets(query string, startTime time.Time) domain.Tweets {
	out := make(chan domain.Tweet)
	stop := make(chan bool)
	tweets := domain.Tweets{
		Data: out,
		Stop: stop,
	}
	freshTweets := t.Gateway.Tweets(query)
	replayTweets := make(chan domain.Tweet)
	history := t.DataMapper.Get(query)
	go history.ReplayFrom(startTime, replayTweets)
	go func() {
		defer close(out)
		copyTweets(replayTweets, out)
		t.copyAndRecordTweets(query, history, freshTweets, out, stop)
	}()
	return tweets
}
<commit_msg>Rename<commit_after>package service

import (
	"sync"
	"time"

	"github.com/ReneGa/tweetcount-microservices/recorder/datamapper"
	"github.com/ReneGa/tweetcount-microservices/recorder/domain"
	"github.com/ReneGa/tweetcount-microservices/recorder/gateway"
)

type Tweets struct {
	sync.Mutex
	DataMapper       *datamapper.Queries
	Gateway          gateway.Tweets
	writerRegistered map[string]bool
}

func copyTweets(from chan domain.Tweet, to chan domain.Tweet) {
	for tweet := range from {
		to <- tweet
	}
}

// attemptToRegisterWriter attempts to register a history writer
// and returns a boolean indicating whether the registration was
// successful.
func (t *Tweets) attemptToRegisterWriter(query string) bool {
	t.Lock()
	if t.writerRegistered == nil {
		t.writerRegistered = map[string]bool{}
	}
	alreadyRegistered := t.writerRegistered[query]
	t.writerRegistered[query] = true
	t.Unlock()
	return !alreadyRegistered
}

func (t *Tweets) unregisterWriter(query string) {
	t.Lock()
	t.writerRegistered[query] = false
	t.Unlock()
}

func (t *Tweets) copyAndRecordTweets(query string, history datamapper.Tweets, freshTweets domain.Tweets, out chan domain.Tweet, stop chan bool) {
	writeHistory := false
	defer func() {
		if writeHistory {
			t.unregisterWriter(query)
		}
	}()
	for {
		select {
		case tweet := <-freshTweets.Data:
			writeHistory := t.attemptToRegisterWriter(query)
			if writeHistory {
				history.Append(tweet)
			}
			out <- tweet
		case <-stop:
			freshTweets.Stop <- true
			return
		}
	}
}

func (t *Tweets) Tweets(query string, startTime time.Time) domain.Tweets {
	out := make(chan domain.Tweet)
	stop := make(chan bool)
	tweets := domain.Tweets{
		Data: out,
		Stop: stop,
	}
	freshTweets := t.Gateway.Tweets(query)
	replayTweets := make(chan domain.Tweet)
	history := t.DataMapper.Get(query)
	go history.ReplayFrom(startTime, replayTweets)
	go func() {
		defer close(out)
		copyTweets(replayTweets, out)
		t.copyAndRecordTweets(query, history, freshTweets, out, stop)
	}()
	return tweets
}
<|endoftext|>


<commit_before>package collectors

import (
	"os"
	"sync"
	"time"

	"github.com/StackExchange/scollector/metadata"
	"github.com/StackExchange/scollector/opentsdb"
)

func init() {
	collectors = append(collectors, &IntervalCollector{F: puppet_disabled_linux, init: puppetInit})
}

var (
	puppetEnable bool
	puppetLock   sync.Mutex
)

const (
	puppetPath     = "/var/lib/puppet/"
	puppetDisabled = "/var/lib/puppet/state/agent_disabled.lock"
)

func puppetEnabled() (b bool) {
	puppetLock.Lock()
	b = puppetEnable
	puppetLock.Unlock()
	return
}

func puppetInit() {
	update := func() {
		_, err := os.Stat(puppetPath)
		puppetLock.Lock()
		puppetEnable = err == nil
		puppetLock.Unlock()
	}
	update()
	go func() {
		for _ = range time.Tick(time.Minute * 5) {
			update()
		}
	}()
}

func puppet_disabled_linux() opentsdb.MultiDataPoint {
	if !puppetEnabled() {
		return nil
	}
	var md opentsdb.MultiDataPoint
	disabled := 0
	if _, err := os.Stat(puppetDisabled); !os.IsNotExist(err) {
		disabled = 1
	}
	Add(&md, "puppet.disabled", disabled, nil, metadata.Unknown, metadata.None, "")
	return md
}
<commit_msg>cmd/scollector: Started work on adding the last run summary info into the puppet collector<commit_after>package collectors

import (
	"fmt"
	"io/ioutil"
	"os"
	"sync"
	"time"

	"github.com/StackExchange/scollector/metadata"
	"github.com/StackExchange/scollector/opentsdb"
	"github.com/StackExchange/slog"
	"gopkg.in/yaml.v1"
)

func init() {
	collectors = append(collectors, &IntervalCollector{F: puppet_disabled_linux, init: puppetInit})
	//TODO Currently No Init
	collectors = append(collectors, &IntervalCollector{F: puppet_run_summary_linux})
}

var (
	puppetEnable bool
	puppetLock   sync.Mutex
)

const (
	puppetPath       = "/var/lib/puppet/"
	puppetRunSummary = "./puppet.yaml"
	//puppetRunSummary = "/var/lib/puppet/state/last_run_summary.yaml"
	puppetDisabled = "/var/lib/puppet/state/agent_disabled.lock"
)

func puppetEnabled() (b bool) {
	puppetLock.Lock()
	b = puppetEnable
	puppetLock.Unlock()
	return
}

func puppetInit() {
	update := func() {
		_, err := os.Stat(puppetPath)
		puppetLock.Lock()
		puppetEnable = err == nil
		puppetLock.Unlock()
	}
	update()
	go func() {
		for _ = range time.Tick(time.Minute * 5) {
			update()
		}
	}()
}

func puppet_disabled_linux() opentsdb.MultiDataPoint {
	if !puppetEnabled() {
		return nil
	}
	var md opentsdb.MultiDataPoint
	disabled := 0
	if _, err := os.Stat(puppetDisabled); !os.IsNotExist(err) {
		disabled = 1
	}
	Add(&md, "puppet.disabled", disabled, nil, metadata.Unknown, metadata.None, "")
	return md
}

type PRSummary struct {
	Changes struct {
		Total float64 `yaml:"total"`
	} `yaml:"changes"`
	Events struct {
		Failure float64 `yaml:"failure"`
		Success float64 `yaml:"success"`
		Total   float64 `yaml:"total"`
	} `yaml:"events"`
	Resources struct {
		Changed         float64 `yaml:"changed"`
		Failed          float64 `yaml:"failed"`
		FailedToRestart float64 `yaml:"failed_to_restart"`
		OutOfSync       float64 `yaml:"out_of_sync"`
		Restarted       float64 `yaml:"restarted"`
		Scheduled       float64 `yaml:"scheduled"`
		Skipped         float64 `yaml:"skipped"`
		Total           float64 `yaml:"total"`
	} `yaml:"resources"`
	Time struct {
		Augeas           float64 `yaml:"augeas"`
		ConfigRetrieval  float64 `yaml:"config_retrieval"`
		Cron             float64 `yaml:"cron"`
		Exec             float64 `yaml:"exec"`
		File             float64 `yaml:"file"`
		Filebucket       float64 `yaml:"filebucket"`
		Group            float64 `yaml:"group"`
		IniSetting       float64 `yaml:"ini_setting"`
		LastRun          float64 `yaml:"last_run"`
		Package          float64 `yaml:"package"`
		Schedule         float64 `yaml:"schedule"`
		Service          float64 `yaml:"service"`
		SshAuthorizedKey float64 `yaml:"ssh_authorized_key"`
		Total            float64 `yaml:"total"`
		User             float64 `yaml:"user"`
		Yumrepo          float64 `yaml:"yumrepo"`
	} `yaml:"time"`
	Version struct {
		Config int64  `yaml:"config"`
		Puppet string `yaml:"puppet"`
	} `yaml:"version"`
}

func puppet_run_summary_linux() opentsdb.MultiDataPoint {
	s, err := ioutil.ReadFile(puppetRunSummary)
	if err != nil {
		slog.Errorln(err)
		return nil
	}
	var m PRSummary
	err = yaml.Unmarshal(s, &m)
	if err != nil {
		slog.Errorln(err)
		return nil
	}
	fmt.Println(m.Resources.Changed)
	var md opentsdb.MultiDataPoint
	//m.Version.Config appears to be the unix timestamp
	AddTS(&md, "puppet.run.resources", m.Version.Config, m.Resources.Changed, opentsdb.TagSet{"resource": "changed"}, metadata.Unknown, metadata.None, "")
	AddTS(&md, "puppet.run.resources", m.Version.Config, m.Resources.Failed, opentsdb.TagSet{"resource": "failed"}, metadata.Unknown, metadata.None, "")
	AddTS(&md, "puppet.run.resources", m.Version.Config, m.Resources.FailedToRestart, opentsdb.TagSet{"resource": "failed_to_restart"}, metadata.Unknown, metadata.None, "")
	AddTS(&md, "puppet.run.resources", m.Version.Config, m.Resources.OutOfSync, opentsdb.TagSet{"resource": "out_of_sync"}, metadata.Unknown, metadata.None, "")
	AddTS(&md, "puppet.run.resources", m.Version.Config, m.Resources.Restarted, opentsdb.TagSet{"resource": "restarted"}, metadata.Unknown, metadata.None, "")
	AddTS(&md, "puppet.run.resources", m.Version.Config, m.Resources.Scheduled, opentsdb.TagSet{"resource": "scheduled"}, metadata.Unknown, metadata.None, "")
	AddTS(&md, "puppet.run.resources", m.Version.Config, m.Resources.Changed, opentsdb.TagSet{"resource": "skipped"}, metadata.Unknown, metadata.None, "")
	AddTS(&md, "puppet.run.resources_total", m.Version.Config, m.Resources.Total, nil, metadata.Unknown, metadata.None, "")
	AddTS(&md, "puppet.run.changes", m.Version.Config, m.Changes, nil, metadata.Unknown, metadata.None, "")
	return md
}
<|endoftext|>


<commit_before>// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.

package redis

import (
	"reflect"
	"strings"

	"github.com/wandoulabs/codis/extern/redis-port/pkg/libs/errors"
	"github.com/wandoulabs/codis/extern/redis-port/pkg/libs/utils"
)

type HandlerFunc func(arg0 interface{}, args ...[]byte) (Resp, error)

type HandlerTable map[string]HandlerFunc

func NewHandlerTable(o interface{}) (map[string]HandlerFunc, error) {
	if o == nil {
		return nil, errors.New("handler is nil")
	}
	t := make(map[string]HandlerFunc)
	r := reflect.TypeOf(o)
	for i := 0; i < r.NumMethod(); i++ {
		m := r.Method(i)
		if m.Name[0] < 'A' || m.Name[0] > 'Z' {
			continue
		}
		n := strings.ToLower(m.Name)
		if h, err := createHandlerFunc(o, &m.Func); err != nil {
			return nil, err
		} else if _, exists := t[n]; exists {
			return nil, errors.Errorf("func.name = '%s' has already exists", m.Name)
		} else {
			t[n] = h
		}
	}
	return t, nil
}

func MustHandlerTable(o interface{}) map[string]HandlerFunc {
	t, err := NewHandlerTable(o)
	if err != nil {
		utils.ErrorPanic(err, "create redis handler map failed")
	}
	return t
}

func createHandlerFunc(o interface{}, f *reflect.Value) (HandlerFunc, error) {
	t := f.Type()
	arg0Type := reflect.TypeOf((*interface{})(nil)).Elem()
	argsType := reflect.TypeOf([][]byte{})
	if t.NumIn() != 3 || t.In(1) != arg0Type || t.In(2) != argsType {
		return nil, errors.Errorf("register with invalid func type = '%s'", t)
	}
	ret0Type := reflect.TypeOf((*Resp)(nil)).Elem()
	ret1Type := reflect.TypeOf((*error)(nil)).Elem()
	if t.NumOut() != 2 || t.Out(0) != ret0Type || t.Out(1) != ret1Type {
		return nil, errors.Errorf("register with invalid func type = '%s'", t)
	}
	return func(arg0 interface{}, args ...[]byte) (Resp, error) {
		var arg0Value reflect.Value
		if arg0 == nil {
			arg0Value = reflect.ValueOf((*interface{})(nil))
		} else {
			arg0Value = reflect.ValueOf(arg0)
		}
		var input, output []reflect.Value
		input = []reflect.Value{reflect.ValueOf(o), arg0Value, reflect.ValueOf(args)}
		if t.IsVariadic() {
			output = f.CallSlice(input)
		} else {
			output = f.Call(input)
		}
		var ret0 Resp
		var ret1 error
		if i := output[0].Interface(); i != nil {
			ret0 = i.(Resp)
		}
		if i := output[1].Interface(); i != nil {
			ret1 = i.(error)
		}
		return ret0, ret1
	}, nil
}

func ParseArgs(resp Resp) (cmd string, args [][]byte, err error) {
	var array []Resp
	if o, ok := resp.(*Array); !ok {
		return "", nil, errors.Errorf("expect array, but got type = '%s'", resp.Type())
	} else if o == nil || len(o.Value) == 0 {
		return "", nil, errors.New("request is an empty array")
	} else {
		array = o.Value
	}
	slices := make([][]byte, 0, len(array))
	for i, resp := range array {
		if o, ok := resp.(*BulkBytes); !ok {
			return "", nil, errors.Errorf("args[%d], expect bulkbytes, but got '%s'", i, resp.Type())
		} else if len(o.Value) == 0 {
			return "", nil, errors.Errorf("args[%d] is nil or empty", i)
		} else {
			slices = append(slices, o.Value)
		}
	}
	return strings.ToLower(string(slices[0])), slices[1:], nil
}
<commit_msg>redis-port, remove nil args check<commit_after>// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.

package redis

import (
	"reflect"
	"strings"

	"github.com/wandoulabs/codis/extern/redis-port/pkg/libs/errors"
	"github.com/wandoulabs/codis/extern/redis-port/pkg/libs/utils"
)

type HandlerFunc func(arg0 interface{}, args ...[]byte) (Resp, error)

type HandlerTable map[string]HandlerFunc

func NewHandlerTable(o interface{}) (map[string]HandlerFunc, error) {
	if o == nil {
		return nil, errors.New("handler is nil")
	}
	t := make(map[string]HandlerFunc)
	r := reflect.TypeOf(o)
	for i := 0; i < r.NumMethod(); i++ {
		m := r.Method(i)
		if m.Name[0] < 'A' || m.Name[0] > 'Z' {
			continue
		}
		n := strings.ToLower(m.Name)
		if h, err := createHandlerFunc(o, &m.Func); err != nil {
			return nil, err
		} else if _, exists := t[n]; exists {
			return nil, errors.Errorf("func.name = '%s' has already exists", m.Name)
		} else {
			t[n] = h
		}
	}
	return t, nil
}

func MustHandlerTable(o interface{}) map[string]HandlerFunc {
	t, err := NewHandlerTable(o)
	if err != nil {
		utils.ErrorPanic(err, "create redis handler map failed")
	}
	return t
}

func createHandlerFunc(o interface{}, f *reflect.Value) (HandlerFunc, error) {
	t := f.Type()
	arg0Type := reflect.TypeOf((*interface{})(nil)).Elem()
	argsType := reflect.TypeOf([][]byte{})
	if t.NumIn() != 3 || t.In(1) != arg0Type || t.In(2) != argsType {
		return nil, errors.Errorf("register with invalid func type = '%s'", t)
	}
	ret0Type := reflect.TypeOf((*Resp)(nil)).Elem()
	ret1Type := reflect.TypeOf((*error)(nil)).Elem()
	if t.NumOut() != 2 || t.Out(0) != ret0Type || t.Out(1) != ret1Type {
		return nil, errors.Errorf("register with invalid func type = '%s'", t)
	}
	return func(arg0 interface{}, args ...[]byte) (Resp, error) {
		var arg0Value reflect.Value
		if arg0 == nil {
			arg0Value = reflect.ValueOf((*interface{})(nil))
		} else {
			arg0Value = reflect.ValueOf(arg0)
		}
		var input, output []reflect.Value
		input = []reflect.Value{reflect.ValueOf(o), arg0Value, reflect.ValueOf(args)}
		if t.IsVariadic() {
			output = f.CallSlice(input)
		} else {
			output = f.Call(input)
		}
		var ret0 Resp
		var ret1 error
		if i := output[0].Interface(); i != nil {
			ret0 = i.(Resp)
		}
		if i := output[1].Interface(); i != nil {
			ret1 = i.(error)
		}
		return ret0, ret1
	}, nil
}

func ParseArgs(resp Resp) (cmd string, args [][]byte, err error) {
	var array []Resp
	if o, ok := resp.(*Array); !ok {
		return "", nil, errors.Errorf("expect array, but got type = '%s'", resp.Type())
	} else if o == nil || len(o.Value) == 0 {
		return "", nil, errors.New("request is an empty array")
	} else {
		array = o.Value
	}
	slices := make([][]byte, 0, len(array))
	for i, resp := range array {
		if o, ok := resp.(*BulkBytes); !ok {
			return "", nil, errors.Errorf("args[%d], expect bulkbytes, but got '%s'", i, resp.Type())
		} else if i == 0 && len(o.Value) == 0 {
			return "", nil, errors.New("command is empty")
		} else {
			slices = append(slices, o.Value)
		}
	}
	return strings.ToLower(string(slices[0])), slices[1:], nil
}
<|endoftext|>


<commit_before>package linear

import "math"

// Radian is a unit of measurement for angles: 1 radian = 180/π
type Radian float64

// NewRadian creates a radian from degrees.
func NewRadian(degrees float64) Radian {
	radians := degrees * (math.Pi / float64(180))
	return Radian(radians)
}

// Converts from radians to degrees.
func (r Radian) Degrees() float64 {
	return float64(r) * (180 / math.Pi)
}
<commit_msg>Fixed comment.<commit_after>package linear

import "math"

// Radian is a unit of measurement for angles: 1 radian = 180/π
type Radian float64

// NewRadian creates a radian from degrees.
func NewRadian(degrees float64) Radian {
	radians := degrees * (math.Pi / float64(180))
	return Radian(radians)
}

// Degrees converts from radians to degrees.
func (r Radian) Degrees() float64 {
	return float64(r) * (180 / math.Pi)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"math/big"
)

func main() {
	var output big.Int

	output = factorial(100)

	fmt.Println("Factorial of 100: ", output)
	// fmt.Println("The digit sum of ", output, "is :", digitSum(output))

} // main

func factorial(n uint8) big.Int {
	var factorial, m big.Int
	factorial.SetUint64(1)

	fmt.Println("Debug: func factorial: n", n)

	for n > 0 {
		m.SetUint64(uint64(n))
		factorial.Mul(factorial, m)
		n--
		fmt.Println("Debug: func factorial: for loop: factorial", factorial)
		fmt.Println("Debug: func factorial: for loop: n", n)
	}

	return factorial
} // factorial

func digitSum(n float64) float64 {
	var sum float64
	var digit uint8
	var pow10 uint64 = 1

	for n > 0 {
		pow10 *= 10
		digit = uint8(uint64(n) % pow10)
		sum += float64(digit)

		n /= float64(pow10)

		fmt.Println("Debug: func digitSum: for loop: n", n)
		fmt.Println("Debug: func digitSum: for loop: pow10", pow10)
		fmt.Println("Debug: func digitSum: for loop: digit", digit)
		fmt.Println("Debug: func digitSum: for loop: sum", sum)
	}

	return sum
} // digitSum
<commit_msg>Use of big.Int<commit_after>package main

import (
	"fmt"
	"math/big"
)

func main() {
	// var facto uint64
	facto := new(big.Int).SetUint64(0)
	// var digiSum uint16

	facto = factorial(100)
	fmt.Println("Factorial of 100: ", facto)

	digiSum = digitSum(facto)
	// fmt.Println("The digit sum of ", facto, "is :", digiSum)

} // main

func factorial(n uint8) *big.Int {
	// factorial := new(big.Int).SetUint64(1)
	// factorial := new(big.Int).SetUint64(uint64(n))
	// i := new(big.Int).SetUint64(uint64(n))

	// for i.Sign() > 0 {
	// 	factorial *= i
	// }

	return new(big.Int).MulRange(1, int64(n))
	// 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
} // factorial

func digitSum(n big.Int) uint16 {
	// Converting parameter 'n' from float64 type to big.Int type
	// nBigInt, _ := big.NewFloat(n).Int(nil)

	var sum uint16
	var digit uint8

	// for nBigInt.Sign() > 0 {
	// 	digit = uint8(new(big.Int).Mod(nBigInt, new(big.Int).SetUint64(10)).Uint64())
	// 	sum += uint16(digit)

	// 	nBigInt.Div(nBigInt, new(big.Int).SetUint64(10))

	for n.Sign() > 0 {
		digit = uint8(new(big.Int).Mod(nBigInt, new(big.Int).SetUint64(10)).Uint64())
		sum += uint16(digit)

		nBigInt.Div(nBigInt, new(big.Int).SetUint64(10))

		fmt.Println("Debug: func digitSum: for loop: n", nBigInt)
		fmt.Println("Debug: func digitSum: for loop: digit", digit)
		fmt.Println("Debug: func digitSum: for loop: sum", sum)
	}

	return sum
} // digitSum
<|endoftext|>


<commit_before>package manta

import (
	"encoding/binary"
)

var (
	bigEndian    = binary.BigEndian
	littleEndian = binary.LittleEndian
)

// A reader holds a buffer and performs read operations against it.
type reader struct {
	buf  []byte
	size int
	pos  int
}

// Creates a new reader object with a given buffer.
func newReader(buf []byte) *reader {
	return &reader{buf, len(buf) * 8, 0}
}

// Calculates our byte position.
func (r *reader) byte_pos() int {
	return r.pos / 8
}

// Calculates how many bits are remaining.
func (r *reader) rem_bits() int {
	return r.size - r.pos
}

// Seeks a given number of bits (may be negative).
func (r *reader) seek_bits(n int) {
	if r.pos+n >= r.size || r.pos+n < 0 {
		_panicf("seek overflow: %d bits requested, only %d remaining", n, r.size-r.pos)
	}
	r.pos += n
}

// Seeks a given number of bytes (may be negative).
func (r *reader) seek_bytes(n int) {
	r.seek_bits(n * 8)
}

// Reads a little-endian uint16.
func (r *reader) read_le_uint16() uint16 {
	return littleEndian.Uint16(r.read_bytes(2))
}

// Reads a little-endian uint32.
func (r *reader) read_le_uint32() uint32 {
	return littleEndian.Uint32(r.read_bytes(4))
}

// Reads a little-endian uint64.
func (r *reader) read_le_uint64() uint64 {
	return littleEndian.Uint64(r.read_bytes(8))
}

// Reads a big-endian uint16.
func (r *reader) read_be_uint16() uint16 {
	return bigEndian.Uint16(r.read_bytes(2))
}

// Reads a big-endian uint32.
func (r *reader) read_be_uint32() uint32 {
	return bigEndian.Uint32(r.read_bytes(4))
}

// Reads a big-endian uint64.
func (r *reader) read_be_uint64() uint64 {
	return bigEndian.Uint64(r.read_bytes(8))
}

// Reads an unsigned 32-bit varint.
func (r *reader) read_var_uint32() uint32 {
	var x uint
	var s uint
	for {
		b := uint(r.read_byte())
		x |= (b & 0x7F) << s
		s += 7
		if ((b & 0x80) == 0) || (s == 35) {
			break
		}
	}

	return uint32(x)
}

// Reads an unsigned 64-bit varint.
func (r *reader) read_var_uint64() uint64 {
	var x uint64
	var s uint
	for i := 0; ; i++ {
		b := r.read_byte()
		if b < 0x80 {
			if i > 9 || i == 9 && b > 1 {
				_panicf("read overflow: varint overflows uint64")
			}
			return x | uint64(b)<<s
		}
		x |= uint64(b&0x7f) << s
		s += 7
	}
}

// Reads a signed 64-bit varint.
func (r *reader) read_var_sint64() int64 {
	ux := r.read_var_uint64()
	x := int64(ux >> 1)
	if ux&1 != 0 {
		x = ^x
	}
	return x
}

// Reads a boolean value.
// TODO XXX: untested.
func (r *reader) read_boolean() bool {
	if r.rem_bits() < 1 {
		_panicf("read overflow: no bits left")
	}

	b := r.buf[r.pos*8]&(1<<uint(r.pos%8)) != 0
	r.pos += 1
	return b
}

// Reads the next byte (8 bits) in the buffer.
func (r *reader) read_byte() byte {
	return r.read_bytes(1)[0]
}

// Reads the given number of bytes from the buffer.
func (r *reader) read_bytes(n int) []byte {
	if r.rem_bits() < (n * 8) {
		_panicf("read overflow: %d bits requested, only %d remaining", n*8, r.size-r.pos)
	}

	// Fast path if our position is byte-aligned.
	if r.pos%8 == 0 {
		bpos := r.pos / 8
		r.pos += (n * 8)
		return r.buf[bpos : bpos+n]
	}

	// Slow path if our position isn't byte aligned.
	buf := make([]byte, n)
	for i := 0; i < n; i++ {
		buf[i] = byte(r.read_bits(8))
	}
	return buf
}

// Reads a string of a given length.
func (r *reader) read_string_n(n int) string {
	return string(r.read_bytes(n))
}

// Read bits of a given length as a uint, may or may not be byte-aligned.
func (r *reader) read_bits(n int) uint {
	if r.rem_bits() < n {
		_panicf("read overflow: %d bits requested, only %d remaining", n, r.size-r.pos)
	}

	if n > 32 {
		_panicf("invalid read: %d is greater than maximum read of 32 bits", n)
	}

	bitOffset := r.pos % 8
	nBitsToRead := bitOffset + n
	nBytesToRead := nBitsToRead / 8
	if nBitsToRead%8 != 0 {
		nBytesToRead += 1
	}

	var val uint64
	for i := 0; i < nBytesToRead; i++ {
		m := r.buf[(r.pos/8)+i]
		val += (uint64(m) << (uint64(i) * 8))
	}
	val >>= uint(bitOffset)
	val &= ((1 << uint64(n)) - 1)
	r.pos += n

	return uint(val)
}
<commit_msg>Fix boolean reads<commit_after>package manta

import (
	"encoding/binary"
)

var (
	bigEndian    = binary.BigEndian
	littleEndian = binary.LittleEndian
)

// A reader holds a buffer and performs read operations against it.
type reader struct {
	buf  []byte
	size int
	pos  int
}

// Creates a new reader object with a given buffer.
func newReader(buf []byte) *reader {
	return &reader{buf, len(buf) * 8, 0}
}

// Calculates our byte position.
func (r *reader) byte_pos() int {
	return r.pos / 8
}

// Calculates how many bits are remaining.
func (r *reader) rem_bits() int {
	return r.size - r.pos
}

// Seeks a given number of bits (may be negative).
func (r *reader) seek_bits(n int) {
	if r.pos+n >= r.size || r.pos+n < 0 {
		_panicf("seek overflow: %d bits requested, only %d remaining", n, r.size-r.pos)
	}
	r.pos += n
}

// Seeks a given number of bytes (may be negative).
func (r *reader) seek_bytes(n int) {
	r.seek_bits(n * 8)
}

// Reads a little-endian uint16.
func (r *reader) read_le_uint16() uint16 {
	return littleEndian.Uint16(r.read_bytes(2))
}

// Reads a little-endian uint32.
func (r *reader) read_le_uint32() uint32 {
	return littleEndian.Uint32(r.read_bytes(4))
}

// Reads a little-endian uint64.
func (r *reader) read_le_uint64() uint64 {
	return littleEndian.Uint64(r.read_bytes(8))
}

// Reads a big-endian uint16.
func (r *reader) read_be_uint16() uint16 {
	return bigEndian.Uint16(r.read_bytes(2))
}

// Reads a big-endian uint32.
func (r *reader) read_be_uint32() uint32 {
	return bigEndian.Uint32(r.read_bytes(4))
}

// Reads a big-endian uint64.
func (r *reader) read_be_uint64() uint64 {
	return bigEndian.Uint64(r.read_bytes(8))
}

// Reads an unsigned 32-bit varint.
func (r *reader) read_var_uint32() uint32 {
	var x uint
	var s uint
	for {
		b := uint(r.read_byte())
		x |= (b & 0x7F) << s
		s += 7
		if ((b & 0x80) == 0) || (s == 35) {
			break
		}
	}

	return uint32(x)
}

// Reads an unsigned 64-bit varint.
func (r *reader) read_var_uint64() uint64 {
	var x uint64
	var s uint
	for i := 0; ; i++ {
		b := r.read_byte()
		if b < 0x80 {
			if i > 9 || i == 9 && b > 1 {
				_panicf("read overflow: varint overflows uint64")
			}
			return x | uint64(b)<<s
		}
		x |= uint64(b&0x7f) << s
		s += 7
	}
}

// Reads a signed 64-bit varint.
func (r *reader) read_var_sint64() int64 {
	ux := r.read_var_uint64()
	x := int64(ux >> 1)
	if ux&1 != 0 {
		x = ^x
	}
	return x
}

// Reads a boolean value.
// TODO XXX: untested.
func (r *reader) read_boolean() bool {
	if r.rem_bits() < 1 {
		_panicf("read overflow: no bits left")
	}

	b := r.buf[r.pos/8]&(1<<uint(r.pos%8)) != 0
	r.pos += 1
	return b
}

// Reads the next byte (8 bits) in the buffer.
func (r *reader) read_byte() byte {
	return r.read_bytes(1)[0]
}

// Reads the given number of bytes from the buffer.
func (r *reader) read_bytes(n int) []byte {
	if r.rem_bits() < (n * 8) {
		_panicf("read overflow: %d bits requested, only %d remaining", n*8, r.size-r.pos)
	}

	// Fast path if our position is byte-aligned.
	if r.pos%8 == 0 {
		bpos := r.pos / 8
		r.pos += (n * 8)
		return r.buf[bpos : bpos+n]
	}

	// Slow path if our position isn't byte aligned.
	buf := make([]byte, n)
	for i := 0; i < n; i++ {
		buf[i] = byte(r.read_bits(8))
	}
	return buf
}

// Reads a string of a given length.
func (r *reader) read_string_n(n int) string {
	return string(r.read_bytes(n))
}

// Read bits of a given length as a uint, may or may not be byte-aligned.
func (r *reader) read_bits(n int) uint {
	if r.rem_bits() < n {
		_panicf("read overflow: %d bits requested, only %d remaining", n, r.size-r.pos)
	}

	if n > 32 {
		_panicf("invalid read: %d is greater than maximum read of 32 bits", n)
	}

	bitOffset := r.pos % 8
	nBitsToRead := bitOffset + n
	nBytesToRead := nBitsToRead / 8
	if nBitsToRead%8 != 0 {
		nBytesToRead += 1
	}

	var val uint64
	for i := 0; i < nBytesToRead; i++ {
		m := r.buf[(r.pos/8)+i]
		val += (uint64(m) << (uint64(i) * 8))
	}
	val >>= uint(bitOffset)
	val &= ((1 << uint64(n)) - 1)
	r.pos += n

	return uint(val)
}
<|endoftext|>