gearman-go/worker/request.go

51 lines
1.0 KiB
Go
Raw Normal View History

2013-08-30 12:36:57 +08:00
// Copyright 2011 Xing Xing <mikespook@gmail.com>
// All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package worker
import (
2013-08-30 18:01:10 +08:00
"encoding/binary"
2013-08-30 12:36:57 +08:00
)
// Worker side job
2013-08-30 18:01:10 +08:00
type request struct {
DataType uint32
2013-08-30 12:36:57 +08:00
Data []byte
Handle, UniqueId, Fn string
}
2013-08-30 18:01:10 +08:00
func getRequest() (req *request) {
// TODO pool
return &request{}
2013-08-30 12:36:57 +08:00
}
// Encode a job to byte slice
2013-08-30 18:01:10 +08:00
func (req *request) Encode() (data []byte) {
2013-08-30 12:36:57 +08:00
var l int
2013-08-30 18:01:10 +08:00
if req.DataType == WORK_FAIL {
l = len(req.Handle)
2013-08-30 12:36:57 +08:00
} else {
2013-08-30 18:01:10 +08:00
l = len(req.Data)
if req.Handle != "" {
l += len(req.Handle) + 1
2013-08-30 12:36:57 +08:00
}
}
2013-08-30 18:01:10 +08:00
data = getBuffer(l + MIN_PACKET_LEN)
binary.BigEndian.PutUint32(data[:4], REQ)
binary.BigEndian.PutUint32(data[4:8], req.DataType)
binary.BigEndian.PutUint32(data[8:MIN_PACKET_LEN], uint32(l))
i := MIN_PACKET_LEN
if req.Handle != "" {
hi := len(req.Handle) + i
copy(data[i:hi], []byte(req.Handle))
if req.DataType != WORK_FAIL {
data[hi] = '\x00'
2013-08-30 12:36:57 +08:00
}
2013-08-30 18:01:10 +08:00
i = i + hi
2013-08-30 12:36:57 +08:00
}
2013-08-30 18:01:10 +08:00
copy(data[i:], req.Data)
2013-08-30 12:36:57 +08:00
return
}