probe/discovery/discovery-request.go

71 lines
1.6 KiB
Go
Raw Normal View History

2018-08-29 12:04:23 +00:00
package discovery
import (
"sync"
"git.loafle.net/overflow_scanner/probe/discovery/types"
)
type ofDiscoveryRequest struct {
requesterID string
requestType types.DiscoveryRequestType
params []interface{}
messageChan chan<- types.DiscoveryMessage
dequeue chan bool
timeout bool
}
func (dr *ofDiscoveryRequest) RequesterID() string {
return dr.requesterID
}
func (dr *ofDiscoveryRequest) RequestType() types.DiscoveryRequestType {
return dr.requestType
}
func (dr *ofDiscoveryRequest) Params() []interface{} {
return dr.params
}
func (dr *ofDiscoveryRequest) SendMessage(messageType types.DiscoveryMessageType, data interface{}, err error) {
dr.messageChan <- retainDiscoveryMessage(dr, messageType, data, err)
}
func (dr *ofDiscoveryRequest) release() {
releaseDiscoveryRequest(dr)
}
var discoveryRequestPool sync.Pool
func retainDiscoveryRequest(messageChan chan<- types.DiscoveryMessage, requesterID string, requestType types.DiscoveryRequestType, params ...interface{}) *ofDiscoveryRequest {
v := discoveryRequestPool.Get()
var dr *ofDiscoveryRequest
if v == nil {
dr = &ofDiscoveryRequest{}
} else {
dr = v.(*ofDiscoveryRequest)
}
dr.requesterID = requesterID
dr.requestType = requestType
dr.params = params
dr.messageChan = messageChan
dr.dequeue = make(chan bool, 1)
dr.timeout = false
return dr
}
func releaseDiscoveryRequest(dr *ofDiscoveryRequest) {
dr.requesterID = ""
dr.requestType = types.DiscoveryRequestTypeNone
dr.params = nil
dr.messageChan = nil
close(dr.dequeue)
dr.dequeue = nil
dr.timeout = false
discoveryRequestPool.Put(dr)
}