Ejemplo n.º 1
0
func hlp_out(t *testing.T, res gomem.MCResponse, exp string) {
	var out bytes.Buffer
	err := text.WriteMCResponse(&res, &out)
	if err != nil {
		t.Error(err)
		return
	}

	if !bytes.Equal(out.Bytes(), []byte(exp)) {
		t.Errorf("\nexpected:\n%+v\ngot:\n%+v", exp, out.String())
	}
}
Ejemplo n.º 2
0
func writeback(in <-chan *gomem.MCResponse, out_ io.Writer) {
	out := bufio.NewWriter(out_)
	mx := new(sync.Mutex)

	for res := range in {
		if res.Opaque != 0xffffffff {
			// binary protocol
			var b []byte
			quiet := res.Opcode.IsQuiet()
			if res.Opcode == gomem.GET && res.Opaque == 0 && res.Status == gomem.KEY_ENOENT {
				b = not_found
			} else {
				b = res.Bytes()
			}
			resP.Put(res)

			mx.Lock()
			out.Write(b)

			// "The getq command is both mum on cache miss and quiet,
			// holding its response until a non-quiet command is issued."
			if !quiet {
				// This allows us to do Bytes() and Flush() in
				// parallel
				go func() {
					out.Flush()
					mx.Unlock()
				}()
			} else {
				mx.Unlock()
			}
			continue
		}

		// we've got a text protocol client
		err := text.WriteMCResponse(res, out)
		_ = err // TODO
		resP.Put(res)
	}
}