agent: remove userEventEnc type

This commit is contained in:
Armon Dadgar 2014-08-28 10:56:30 -07:00
parent 2d03146d3b
commit 93c17db1b2
2 changed files with 21 additions and 33 deletions

View File

@ -54,7 +54,7 @@ type Agent struct {
// using eventIndex as the next index to insert into. This // using eventIndex as the next index to insert into. This
// is guarded by eventLock. When an insert happens, the // is guarded by eventLock. When an insert happens, the
// eventNotify group is notified. // eventNotify group is notified.
eventBuf []*userEventEnc eventBuf []*UserEvent
eventIndex int eventIndex int
eventLock sync.RWMutex eventLock sync.RWMutex
eventNotify consul.NotifyGroup eventNotify consul.NotifyGroup
@ -102,7 +102,7 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
checkMonitors: make(map[string]*CheckMonitor), checkMonitors: make(map[string]*CheckMonitor),
checkTTLs: make(map[string]*CheckTTL), checkTTLs: make(map[string]*CheckTTL),
eventCh: make(chan serf.UserEvent, 1024), eventCh: make(chan serf.UserEvent, 1024),
eventBuf: make([]*userEventEnc, 256), eventBuf: make([]*UserEvent, 256),
shutdownCh: make(chan struct{}), shutdownCh: make(chan struct{}),
} }

View File

@ -14,37 +14,32 @@ const (
) )
// UserEventParam is used to parameterize a user event // UserEventParam is used to parameterize a user event
type UserEventParam struct { type UserEvent struct {
// ID of the user event. Automatically generated.
ID string
// Name of the event // Name of the event
Name string Name string `codec:"n"`
// Optional payload // Optional payload
Payload []byte Payload []byte `codec:"p,omitempty"`
// NodeFilter is a regular expression to filter on nodes // NodeFilter is a regular expression to filter on nodes
NodeFilter string NodeFilter string `codec:"nf,omitempty"`
// ServiceFilter is a regular expression to filter on services // ServiceFilter is a regular expression to filter on services
ServiceFilter string ServiceFilter string `codec:"sf,omitempty"`
// TagFilter is a regular expression to filter on tags of a service, // TagFilter is a regular expression to filter on tags of a service,
// must be provided with ServiceFilter // must be provided with ServiceFilter
TagFilter string TagFilter string `codec:"tf,omitempty"`
}
// userEventEnc is the encoded version // Version of the user event. Automatically generated.
type userEventEnc struct { Version int `codec:"v"`
Version int `codec:"v"`
ID string
Name string `codec:"n"`
Payload []byte `codec:"p,omitempty"`
NodeFilter string `codec:"nf,omitempty"`
ServiceFilter string `codec:"sf,omitempty"`
TagFilter string `codec:"tf,omitempty"`
} }
// validateUserEventParams is used to sanity check the inputs // validateUserEventParams is used to sanity check the inputs
func validateUserEventParams(params *UserEventParam) error { func validateUserEventParams(params *UserEvent) error {
// Validate the inputs // Validate the inputs
if params.Name == "" { if params.Name == "" {
return fmt.Errorf("User event missing name") return fmt.Errorf("User event missing name")
@ -71,23 +66,16 @@ func validateUserEventParams(params *UserEventParam) error {
} }
// UserEvent is used to fire an event via the Serf layer on the LAN // UserEvent is used to fire an event via the Serf layer on the LAN
func (a *Agent) UserEvent(params *UserEventParam) error { func (a *Agent) UserEvent(params *UserEvent) error {
// Validate the params // Validate the params
if err := validateUserEventParams(params); err != nil { if err := validateUserEventParams(params); err != nil {
return err return err
} }
// Format message // Format message
msg := userEventEnc{ params.ID = generateUUID()
Version: userEventMaxVersion, params.Version = userEventMaxVersion
ID: generateUUID(), payload, err := encodeUserEvent(&params)
Name: params.Name,
Payload: params.Payload,
NodeFilter: params.NodeFilter,
ServiceFilter: params.ServiceFilter,
TagFilter: params.TagFilter,
}
payload, err := encodeUserEvent(&msg)
if err != nil { if err != nil {
return fmt.Errorf("UserEvent encoding failed: %v", err) return fmt.Errorf("UserEvent encoding failed: %v", err)
} }
@ -104,7 +92,7 @@ func (a *Agent) handleEvents() {
select { select {
case e := <-a.eventCh: case e := <-a.eventCh:
// Decode the event // Decode the event
msg := new(userEventEnc) msg := new(UserEvent)
if err := decodeUserEvent(e.Payload, msg); err != nil { if err := decodeUserEvent(e.Payload, msg); err != nil {
a.logger.Printf("[ERR] agent: Failed to decode event: %v", err) a.logger.Printf("[ERR] agent: Failed to decode event: %v", err)
continue continue
@ -125,7 +113,7 @@ func (a *Agent) handleEvents() {
} }
// shouldProcessUserEvent checks if an event makes it through our filters // shouldProcessUserEvent checks if an event makes it through our filters
func (a *Agent) shouldProcessUserEvent(msg *userEventEnc) bool { func (a *Agent) shouldProcessUserEvent(msg *UserEvent) bool {
// Check the version // Check the version
if msg.Version > userEventMaxVersion { if msg.Version > userEventMaxVersion {
a.logger.Printf("[WARN] agent: Event version %d may have unsupported features (%s)", a.logger.Printf("[WARN] agent: Event version %d may have unsupported features (%s)",
@ -197,7 +185,7 @@ func (a *Agent) shouldProcessUserEvent(msg *userEventEnc) bool {
} }
// ingestUserEvent is used to process an event that passes filtering // ingestUserEvent is used to process an event that passes filtering
func (a *Agent) ingestUserEvent(msg *userEventEnc) { func (a *Agent) ingestUserEvent(msg *UserEvent) {
a.eventLock.Lock() a.eventLock.Lock()
defer func() { defer func() {
a.eventLock.Unlock() a.eventLock.Unlock()