package pgproto3
import (
)
const cancelRequestCode = 80877102
type CancelRequest struct {
ProcessID uint32
SecretKey []byte
}
func (*CancelRequest) () {}
func ( *CancelRequest) ( []byte) error {
if len() < 12 {
return errors.New("cancel request too short")
}
if len() > 264 {
return errors.New("cancel request too long")
}
:= binary.BigEndian.Uint32()
if != cancelRequestCode {
return errors.New("bad cancel request code")
}
.ProcessID = binary.BigEndian.Uint32([4:])
.SecretKey = make([]byte, len()-8)
copy(.SecretKey, [8:])
return nil
}
func ( *CancelRequest) ( []byte) ([]byte, error) {
if len(.SecretKey) > 256 {
return nil, errors.New("secret key too long")
}
:= int32(12 + len(.SecretKey))
= pgio.AppendInt32(, )
= pgio.AppendInt32(, cancelRequestCode)
= pgio.AppendUint32(, .ProcessID)
= append(, .SecretKey...)
return , nil
}
func ( CancelRequest) () ([]byte, error) {
return json.Marshal(struct {
string
uint32
string
}{
: "CancelRequest",
: .ProcessID,
: hex.EncodeToString(.SecretKey),
})
}
func ( *CancelRequest) ( []byte) error {
var struct {
uint32
string
}
if := json.Unmarshal(, &); != nil {
return
}
.ProcessID = .
, := hex.DecodeString(.)
if != nil {
return
}
.SecretKey =
return nil
}