예제 #1
0
func (stream *stream) readFrom(sock *fullDuplex) {
	for {
		// TODO: don't always chunk off exactly 1024 bytes
		buf := make([]byte, 1024)
		n, err := sock.Read(buf)
		state.RecordRead(n)
		if err != nil {
			if err != io.EOF {
				log.Debugf("readFrom %s: %s", stream.l, err)
			}
			sock.CloseRead()
			stream.emptyPacket()
			return
		}
		packet := packet{
			time.After(stream.conn.link.Delay()),
			buf[:n],
		}

		select {
		case stream.in <- packet:
		case <-stream.conn.kill:
			stream.closeLater(sock)
			return
		}
	}
}
예제 #2
0
func (stream *stream) writeTo(sock *fullDuplex) {
	for {
		var buf []byte
		select {
		case <-stream.conn.kill:
			stream.closeLater(sock)
			return
		case buf = <-stream.out:
		}
		// This is the special "close" notification
		if buf == nil {
			sock.CloseWrite()
			return
		}

		n, err := sock.Write(buf)
		state.RecordWrite(n)
		log.Debugf("Wrote %s %d bytes: %#v", stream.l, n, string(buf))
		if err != nil || n != len(buf) {
			log.Printf("writeTo %s: %s", stream.l, err)
			break
		}
	}

	// Close the other end of the connection
	time.Sleep(stream.conn.link.Delay())
	stream.other(sock).CloseRead()
}
예제 #3
0
func NewRand(name string) *rand.Rand {
	source := fmt.Sprintf("%s-%d", name, Seed())
	image := sha1.Sum([]byte(source))
	b := bytes.NewReader(image[:])
	n, err := realrand.Int(b, big.NewInt(math.MaxInt64))
	if err != nil {
		log.Fatalf("Bug in seed derivation: %s", err)
	}
	log.Debugf("Seed for %v (source %v) is %v", name, source, n.Int64())
	return rand.New(rand.NewSource(n.Int64()))
}
예제 #4
0
func (h *Harness) issueQuery(reqid int, node *agent.Agent, query string) bool {
	log.Debugf("[harness] Making request to %v: %#v", node, query)

	b := strings.NewReader(query)
	url := node.ConnectionString + "/sql"

	start := time.Now()
	resp, err := unixClient.Post(url, "application/octet-stream", b)
	end := time.Now()

	if err != nil {
		log.Printf("[harness] Sleeping 100ms after request error from %s (in response to %#v): %s", node, query, err)
		return false
	}

	body, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	if err != nil {
		log.Printf("[harness] Error reading body from %v (in response to %#v): %s", node, query, err)
	}

	if resp.StatusCode != 200 {
		log.Printf("[harness] Sleeping 100ms after HTTP %d status code from %s (in response to %#v): %s", resp.StatusCode, node, query, body)
		return false
	}

	log.Debugf("[harness] Received response to %v (%#v): %s", node, query, body)

	h.result <- &result{
		reqid: reqid,
		node:  node,
		query: query,
		start: start,
		end:   end,
		resp:  body,
	}
	return true
}
예제 #5
0
func (d *Director) randomPartition(rng *rand.Rand) []*network.Link {
	count := state.NodeCount()

	// If there are fewer than three nodes, netsplits are pretty
	// boring
	if count < 3 {
		return []*network.Link{d.net.Links()[0]}
	}
	perm := rng.Perm(count)
	splitPoint := rng.Intn(count-2) + 1
	split := make([]uint, 0)
	for i := 0; i < splitPoint; i++ {
		split = append(split, uint(perm[i]))
	}
	log.Debugf("Made a netsplit: %v", split)
	return d.net.FindPerimeter(split)
}