예제 #1
0
func stopQueue(qu chan message) {
	close(qu)
	for msg := range qu { // clear queue and send fail signal
		op.SigFailed(msg.context.Signal, nil)
	}

}
예제 #2
0
func (mh *testMessageHandler) acknowledgeMessage(m message) {
	if mh.response == CompletedResponse {
		op.SigCompleted(m.context.Signal)
	} else {
		op.SigFailed(m.context.Signal, nil)
	}
}
예제 #3
0
func (c *console) PublishEvent(
	s op.Signaler,
	opts outputs.Options,
	event common.MapStr,
) error {
	var jsonEvent []byte
	var err error

	if c.config.Pretty {
		jsonEvent, err = json.MarshalIndent(event, "", "  ")
	} else {
		jsonEvent, err = json.Marshal(event)
	}
	if err != nil {
		logp.Err("Fail to convert the event to JSON (%v): %#v", err, event)
		op.SigCompleted(s)
		return err
	}

	if err = writeBuffer(jsonEvent); err != nil {
		goto fail
	}
	if err = writeBuffer([]byte{'\n'}); err != nil {
		goto fail
	}

	op.SigCompleted(s)
	return nil
fail:
	if opts.Guaranteed {
		logp.Critical("Unable to publish events to console: %v", err)
	}
	op.SigFailed(s, err)
	return err
}
예제 #4
0
파일: single.go 프로젝트: YaSuenag/hsbeat
// publish is used to publish events using the configured protocol client.
// It provides general error handling and back off support used on failed
// send attempts. To be used by PublishEvent and PublishEvents.
// The send callback will try to progress sending traffic and returns kind of
// progress made in ok or resetFail. If ok is set to true, send finished
// processing events. If ok is false but resetFail is set, send was partially
// successful. If send was partially successful, the fail counter is reset thus up
// to maxAttempts send attempts without any progress might be executed.
func (s *Mode) publish(
	signaler op.Signaler,
	opts outputs.Options,
	send func() (ok bool, resetFail bool),
) error {
	fails := 0
	var err error

	guaranteed := opts.Guaranteed || s.maxAttempts == 0
	for !s.closed && (guaranteed || fails < s.maxAttempts) {

		ok := false
		resetFail := false

		if err := s.connect(); err != nil {
			logp.Err("Connecting error publishing events (retrying): %s", err)
			goto sendFail
		}

		ok, resetFail = send()
		if !ok {
			s.closeClient()
			goto sendFail
		}

		debugf("send completed")
		s.backoff.Reset()
		op.SigCompleted(signaler)
		return nil

	sendFail:
		debugf("send fail")

		fails++
		if resetFail {
			debugf("reset fails")
			s.backoff.Reset()
			fails = 0
		}
		s.backoff.Wait()

		if !guaranteed && (s.maxAttempts > 0 && fails == s.maxAttempts) {
			// max number of attempts reached
			debugf("max number of attempts reached")
			break
		}
	}

	debugf("messages dropped")
	mode.Dropped(1)
	op.SigFailed(signaler, err)
	return nil
}
예제 #5
0
func send(qu, bulkQu chan message, m message) {
	var ch chan message
	if m.event != nil {
		ch = qu
	} else {
		ch = bulkQu
	}

	var done <-chan struct{}
	if m.client != nil {
		done = m.client.canceler.Done()
	}

	select {
	case <-done: // blocks if nil
		// client closed -> signal drop
		// XXX: send Cancel or Fail signal?
		op.SigFailed(m.context.Signal, ErrClientClosed)

	case ch <- m:
		messagesInWorkerQueues.Add(1)
	}
}
예제 #6
0
파일: context.go 프로젝트: ChongFeng/beats
// dropping is called when a message is dropped. It updates the
// relevant counters and sends a failed signal.
func dropping(msg eventsMessage) {
	debugf("messages dropped")
	mode.Dropped(1)
	op.SigFailed(msg.signaler, nil)
}
예제 #7
0
func (m *LoadBalancerMode) onMessage(
	backoff *backoff,
	client ProtocolClient,
	msg eventsMessage,
) (bool, error) {
	done := false
	if msg.event != nil {
		err := client.PublishEvent(msg.event)
		done = !backoff.WaitOnError(err)
		if err != nil {
			if msg.attemptsLeft > 0 {
				msg.attemptsLeft--
			}
			m.onFail(msg, err)
			return done, err
		}
	} else {
		events := msg.events
		total := len(events)

		for len(events) > 0 {
			var err error

			events, err = client.PublishEvents(events)
			done = !backoff.WaitOnError(err)
			if done && err != nil {
				op.SigFailed(msg.signaler, err)
				return done, err
			}

			if err != nil {
				if msg.attemptsLeft > 0 {
					msg.attemptsLeft--
				}

				// reset attempt count if subset of messages has been processed
				if len(events) < total && msg.attemptsLeft >= 0 {
					debug("reset fails")
					msg.attemptsLeft = m.maxAttempts
				}

				if err != ErrTempBulkFailure {
					// retry non-published subset of events in batch
					msg.events = events
					m.onFail(msg, err)
					return done, err
				}

				if m.maxAttempts > 0 && msg.attemptsLeft == 0 {
					// no more attempts left => drop
					dropping(msg)
					return done, err
				}

				// reset total count for temporary failure loop
				total = len(events)
			}
		}
	}

	op.SigCompleted(msg.signaler)
	return done, nil
}