Move fetching response from channel to request() method
Also parse error on bad status in request method. This pattern was scattered all over the place.
This commit is contained in:
parent
338b59a53a
commit
47db3dbbbc
@ -140,17 +140,11 @@ func (l *Libvirt) {{.Name}}({{range $ix, $arg := .Args}}{{if $ix}}, {{end}}{{.Na
|
||||
return
|
||||
}
|
||||
{{end}}
|
||||
var resp <-chan response
|
||||
resp, err = l.request({{.Num}}, constants.Program, &buf)
|
||||
{{if .RetStruct}} var r response{{end}}
|
||||
{{if .RetStruct}}r{{else}}_{{end}}, err = l.request({{.Num}}, constants.Program, &buf)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
r := <-resp
|
||||
if r.Status != StatusOK {
|
||||
err = decodeError(r.Payload)
|
||||
return
|
||||
}
|
||||
{{if .RetStruct}}
|
||||
// Return value unmarshaling
|
||||
rdr := bytes.NewReader(r.Payload)
|
||||
|
3900
libvirt.gen.go
3900
libvirt.gen.go
File diff suppressed because it is too large
Load Diff
19
libvirt.go
19
libvirt.go
@ -152,19 +152,12 @@ func (l *Libvirt) Events(dom string) (<-chan DomainEvent, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := l.request(constants.QEMUConnectDomainMonitorEventRegister, constants.ProgramQEMU, &buf)
|
||||
res, err := l.request(constants.QEMUConnectDomainMonitorEventRegister, constants.ProgramQEMU, &buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := <-resp
|
||||
if res.Status != StatusOK {
|
||||
err = decodeError(res.Payload)
|
||||
if err == ErrUnsupported {
|
||||
return nil, ErrEventsNotSupported
|
||||
}
|
||||
|
||||
return nil, decodeError(res.Payload)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dec := xdr.NewDecoder(bytes.NewReader(res.Payload))
|
||||
@ -250,17 +243,11 @@ func (l *Libvirt) Run(dom string, cmd []byte) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := l.request(constants.QEMUDomainMonitor, constants.ProgramQEMU, &buf)
|
||||
res, err := l.request(constants.QEMUDomainMonitor, constants.ProgramQEMU, &buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
res := <-resp
|
||||
// check for libvirt errors
|
||||
if res.Status != StatusOK {
|
||||
return nil, decodeError(res.Payload)
|
||||
}
|
||||
|
||||
// check for QEMU process errors
|
||||
if err = getQEMUError(res); err != nil {
|
||||
return nil, err
|
||||
|
51
rpc.go
51
rpc.go
@ -131,41 +131,22 @@ func (l *Libvirt) connect() error {
|
||||
|
||||
// libvirt requires that we call auth-list prior to connecting,
|
||||
// event when no authentication is used.
|
||||
resp, err := l.request(constants.ProcAuthList, constants.Program, &buf)
|
||||
_, err = l.request(constants.ProcAuthList, constants.Program, &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r := <-resp
|
||||
if r.Status != StatusOK {
|
||||
return decodeError(r.Payload)
|
||||
}
|
||||
|
||||
resp, err = l.request(constants.ProcConnectOpen, constants.Program, &buf)
|
||||
_, err = l.request(constants.ProcConnectOpen, constants.Program, &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r = <-resp
|
||||
if r.Status != StatusOK {
|
||||
return decodeError(r.Payload)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Libvirt) disconnect() error {
|
||||
resp, err := l.request(constants.ProcConnectClose, constants.Program, nil)
|
||||
if err != nil {
|
||||
_, err := l.request(constants.ProcConnectClose, constants.Program, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
r := <-resp
|
||||
if r.Status != StatusOK {
|
||||
return decodeError(r.Payload)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// listen processes incoming data and routes
|
||||
@ -281,16 +262,11 @@ func (l *Libvirt) removeStream(id uint32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := l.request(constants.QEMUConnectDomainMonitorEventDeregister, constants.ProgramQEMU, &buf)
|
||||
_, err = l.request(constants.QEMUConnectDomainMonitorEventDeregister, constants.ProgramQEMU, &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res := <-resp
|
||||
if res.Status != StatusOK {
|
||||
return decodeError(res.Payload)
|
||||
}
|
||||
|
||||
l.em.Lock()
|
||||
delete(l.events, id)
|
||||
l.em.Unlock()
|
||||
@ -314,9 +290,9 @@ func (l *Libvirt) deregister(id uint32) {
|
||||
}
|
||||
|
||||
// request performs a libvirt RPC request.
|
||||
// The returned channel is used by the caller to receive the asynchronous
|
||||
// call response. The channel is closed once a response has been sent.
|
||||
func (l *Libvirt) request(proc uint32, program uint32, payload *bytes.Buffer) (<-chan response, error) {
|
||||
// returns response returned by server.
|
||||
// if response is not OK, decodes error from it and returns it.
|
||||
func (l *Libvirt) request(proc uint32, program uint32, payload *bytes.Buffer) (response, error) {
|
||||
serial := l.serial()
|
||||
c := make(chan response)
|
||||
|
||||
@ -344,22 +320,27 @@ func (l *Libvirt) request(proc uint32, program uint32, payload *bytes.Buffer) (<
|
||||
defer l.mu.Unlock()
|
||||
err := binary.Write(l.w, binary.BigEndian, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return response{}, err
|
||||
}
|
||||
|
||||
// write payload
|
||||
if payload != nil {
|
||||
err = binary.Write(l.w, binary.BigEndian, payload.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return response{}, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := l.w.Flush(); err != nil {
|
||||
return nil, err
|
||||
return response{}, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
resp := <-c
|
||||
if resp.Status != StatusOK {
|
||||
return resp, decodeError(resp.Payload)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// encode XDR encodes the provided data.
|
||||
|
Loading…
Reference in New Issue
Block a user