2018-08-29 12:04:23 +00:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2018-09-17 07:20:46 +00:00
|
|
|
"sync/atomic"
|
2018-08-29 12:04:23 +00:00
|
|
|
|
|
|
|
"git.loafle.net/overflow_scanner/probe/discovery/types"
|
2018-09-13 13:05:05 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2018-08-29 12:04:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ofDiscoveryRequest struct {
|
2018-09-13 13:05:05 +00:00
|
|
|
requestID string
|
2018-08-29 12:04:23 +00:00
|
|
|
requesterID string
|
|
|
|
requestType types.DiscoveryRequestType
|
|
|
|
params []interface{}
|
|
|
|
|
2018-09-17 07:20:46 +00:00
|
|
|
canceled atomic.Value
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
dequeue chan bool
|
|
|
|
timeout bool
|
2018-08-29 12:04:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 13:05:05 +00:00
|
|
|
func (dr *ofDiscoveryRequest) RequestID() string {
|
|
|
|
return dr.requestID
|
|
|
|
}
|
|
|
|
|
2018-08-29 12:04:23 +00:00
|
|
|
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) release() {
|
|
|
|
releaseDiscoveryRequest(dr)
|
|
|
|
}
|
|
|
|
|
|
|
|
var discoveryRequestPool sync.Pool
|
|
|
|
|
2018-08-30 12:56:32 +00:00
|
|
|
func retainDiscoveryRequest(requesterID string, requestType types.DiscoveryRequestType, params ...interface{}) *ofDiscoveryRequest {
|
2018-08-29 12:04:23 +00:00
|
|
|
v := discoveryRequestPool.Get()
|
|
|
|
var dr *ofDiscoveryRequest
|
|
|
|
if v == nil {
|
|
|
|
dr = &ofDiscoveryRequest{}
|
|
|
|
} else {
|
|
|
|
dr = v.(*ofDiscoveryRequest)
|
|
|
|
}
|
|
|
|
|
2018-09-13 13:05:05 +00:00
|
|
|
dr.requestID = uuid.NewV4().String()
|
2018-08-29 12:04:23 +00:00
|
|
|
dr.requesterID = requesterID
|
|
|
|
dr.requestType = requestType
|
|
|
|
dr.params = params
|
2018-09-17 07:20:46 +00:00
|
|
|
dr.canceled.Store(false)
|
2018-08-29 12:04:23 +00:00
|
|
|
dr.dequeue = make(chan bool, 1)
|
|
|
|
dr.timeout = false
|
|
|
|
|
|
|
|
return dr
|
|
|
|
}
|
|
|
|
|
|
|
|
func releaseDiscoveryRequest(dr *ofDiscoveryRequest) {
|
2018-09-13 13:05:05 +00:00
|
|
|
dr.requestID = ""
|
2018-08-29 12:04:23 +00:00
|
|
|
dr.requesterID = ""
|
|
|
|
dr.requestType = types.DiscoveryRequestTypeNone
|
|
|
|
dr.params = nil
|
|
|
|
close(dr.dequeue)
|
|
|
|
dr.dequeue = nil
|
|
|
|
dr.timeout = false
|
|
|
|
|
|
|
|
discoveryRequestPool.Put(dr)
|
|
|
|
}
|