Esempio n. 1
0
// SCHEME://123.3.45.0:3456/2345/R1122334455667788
func ParseAddr(s string) (*Addr, error) {
	u, err := url.Parse(s)
	if err != nil {
		return nil, err
	}
	if u.Scheme != n.Scheme {
		return nil, errors.NewError("worker address URL scheme mismatch")
	}
	// Net address
	naddr, err := ParseNetAddr(u.Host)
	if err != nil {
		return nil, err
	}
	// Parse path
	parts := strings.Split(u.Path, "/")
	if len(parts) != 3 {
		return nil, errors.NewError(fmt.Sprintf("parse path: %#v", parts))
	}
	if parts[0] != "" {
		return nil, errors.NewError("must start with slash")
	}
	// PID
	pid, err := strconv.Atoi(parts[1])
	if err != nil {
		return nil, err
	}
	// Worker ID
	id, err := n.ParseWorkerID(parts[2])
	if err != nil {
		return nil, err
	}
	return &Addr{ID: id, PID: pid, TCP: naddr.(*net.TCPAddr)}, nil
}
Esempio n. 2
0
// RuntimeProfile exposes the Go runtime profiling framework of this worker
func (s *Acid) RuntimeProfile(name string, debug int) ([]byte, error) {
	prof := pprof.Lookup(name)
	if prof == nil {
		return nil, errors.NewError("no such profile")
	}
	var w bytes.Buffer
	if err := prof.WriteTo(&w, debug); err != nil {
		return nil, err
	}
	return w.Bytes(), nil
}
Esempio n. 3
0
func (d *Dialer) auth(addr n.Addr, conn *blend.Conn) error {
	defer conn.Close()
	if err := conn.Write(&HelloMsg{
		SourceAddr: d.dialback,
		TargetAddr: addr,
	}); err != nil {
		return err
	}
	msg, err := conn.Read()
	if err != nil {
		return err
	}
	switch q := msg.(type) {
	case *WelcomeMsg:
		return nil
	case *RejectMsg:
		return errors.NewError("dial rejected by remote (%s)", errors.Unpack(q.Err))
	}
	return errors.NewError("unknown welcome response")
}
Esempio n. 4
0
func (s *Acid) CPUProfile(duration time.Duration) ([]byte, error) {
	if duration > time.Hour {
		return nil, errors.NewError("cpu profile duration exceeds 1 hour")
	}
	var w bytes.Buffer
	if err := pprof.StartCPUProfile(&w); err != nil {
		return nil, err
	}
	log.Printf("cpu profiling for %d sec", duration/1e9)
	time.Sleep(duration)
	pprof.StopCPUProfile()
	return w.Bytes(), nil
}
Esempio n. 5
0
func (r *Runtime) Spawn(host worker.Host, anchor []string, fn circuit.Func, in ...interface{}) (retrn []interface{}, addr n.Addr, err error) {

	// Catch all errors
	defer func() {
		if p := recover(); p != nil {
			retrn, addr = nil, nil

			var w bytes.Buffer
			pprof.Lookup("goroutine").WriteTo(&w, 2)
			err = errors.NewError(fmt.Sprintf("spawn panic: %#v\nstack:\n%s", p, string(w.Bytes())))
		}
	}()

	addr, err = worker.Spawn(host, anchor...)
	if err != nil {
		return nil, nil, err
	}

	return r.remoteGo(addr, fn, in...), addr, nil
}
Esempio n. 6
0
func (l *Listener) handshake(conn *blend.Conn) (sourceAddr *Addr, err error) {
	if conn == nil {
		return nil, errors.NewError("listener off")
	}
	defer conn.Close()
	//
	var msg interface{}
	msg, err = conn.Read()
	if err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			conn.Write(&RejectMsg{err})
		} else {
			err = conn.Write(&WelcomeMsg{})
		}
	}()
	hello, ok := msg.(*HelloMsg)
	if !ok {
		log.Println("rejecting", conn.RemoteAddr().String(), "unknown hello message type")
		return nil, errors.NewError("rejecting unknown hello type")
	}
	// Accept user connections
	da, ok := hello.SourceAddr.(*Addr)
	if !ok {
		log.Println("rejecting", conn.RemoteAddr().String(), "unknown source address type")
		return nil, errors.NewError("rejecting unknown source address type")
	}
	reverseAddr(da, conn.RemoteAddr())
	la, ok := hello.TargetAddr.(*Addr)
	if !ok {
		log.Println("rejecting ", conn.RemoteAddr().String(), "unknown target address type")
		return nil, errors.NewError("rejecting unknown target address type")
	}
	if la.WorkerID() != l.addr.WorkerID() {
		log.Println("rejecting", conn.RemoteAddr().String(), "due to worker identity mismatch")
		return nil, errors.NewError("rejecting worker identity mismatch, looks for %s, got %s", la.WorkerID(), l.addr.WorkerID())
	}
	if la.PID != os.Getpid() {
		log.Println("rejecting", conn.RemoteAddr().String(), "due to worker PID mismatch")
		return nil, errors.NewError("rejecting worker PID mismatch, looks for %d, got %d", la.PID, os.Getpid())
	}
	return da, nil
}
Esempio n. 7
0
//   2013 Petar Maymounkov <*****@*****.**>

// Package anchorfs exposes the programming interface for accessing the anchor file system
package anchorfs

import (
	"path"
	"strings"
	"time"

	"github.com/gocircuit/circuit/use/errors"
	"github.com/gocircuit/circuit/use/n"
)

var (
	ErrName     = errors.NewError("anchor name")
	ErrNotFound = errors.NewError("not found")
)

// System represents an anchor file system
type System interface {
	OpenFile(string) (File, error)
	OpenDir(string) (Dir, error)

	// Created returns the anchors created by this worker
	Created() []string
}

// Rev is the sequentially-increasing revision number of a file system object
type Rev int32
Esempio n. 8
0
	nblocked int
}

func newSniffer(name string) *Sniffer {
	s := &Sniffer{
		name:    name,
		unblock: make(chan struct{}),
	}
	runtime.SetFinalizer(s, func(x *Sniffer) {
		x.warn("•", "Finalizer")
		x.Close()
	})
	return s
}

var errWait = errors.NewError("wait")

func (s *Sniffer) Printf(format string, arg ...interface{}) {
	log.Printf("▒ %s ▒ %s", s.name, fmt.Sprintf(format, arg...))
}

func (s *Sniffer) report(attr string, p []byte) {
	s.Printf("%s⟩\n“%s”", attr, string(p))
}

func (s *Sniffer) warn(attr, msg string) {
	s.Printf("%s⟫ “%s”", attr, msg)
}

func (s *Sniffer) Read(p []byte) (n int, err error) {
	for {
Esempio n. 9
0
func fileRecover(pe *error) {
	if p := recover(); p != nil {
		*pe = errors.NewError("server died")
	}
}
Esempio n. 10
0
//
// Authors:
//   2013 Petar Maymounkov <*****@*****.**>

package n

import (
	"fmt"
	"hash/fnv"
	"math/rand"
	"strconv"

	"github.com/gocircuit/circuit/use/errors"
)

var ErrParse = errors.NewError("parse")

// WorkerID represents the identity of a circuit worker process.
type WorkerID string

// String returns a cononical string representation of this worker ID.
func (r WorkerID) String() string {
	return string(r)
}

// ChooseWorkerID returns a random worker ID.
func ChooseWorkerID() WorkerID {
	return Int64WorkerID(rand.Int63())
}

func Int64WorkerID(src int64) WorkerID {