func (sink *WebsocketSink) Run(inputChan <-chan *events.Envelope) {
	stopChan := make(chan struct{})
	sink.logger.Debugf("Websocket Sink %s: Running for streamId [%s]", sink.clientAddress, sink.streamId)
	context := truncatingbuffer.NewDefaultContext(sink.dropsondeOrigin, sink.Identifier())
	buffer := sinks.RunTruncatingBuffer(inputChan, sink.messageDrainBufferSize, context, sink.logger, stopChan)
	for {
		sink.logger.Debugf("Websocket Sink %s: Waiting for activity", sink.clientAddress)
		messageEnvelope, ok := <-buffer.GetOutputChannel()

		if !ok {
			sink.logger.Debugf("Websocket Sink %s: Closed listener channel detected. Closing websocket", sink.clientAddress)
			close(stopChan)
			return
		}
		messageBytes, err := proto.Marshal(messageEnvelope)

		if err != nil {
			sink.logger.Errorf("Websocket Sink %s: Error marshalling %s envelope from origin %s: %s", sink.clientAddress, messageEnvelope.GetEventType().String(), messageEnvelope.GetOrigin(), err.Error())
			continue
		}

		sink.logger.Debugf("Websocket Sink %s: Received %s message from %s at %d. Sending data.", sink.clientAddress, messageEnvelope.GetEventType().String(), messageEnvelope.GetOrigin(), messageEnvelope.Timestamp)
		if sink.writeTimeout != 0 {
			sink.ws.SetWriteDeadline(time.Now().Add(sink.writeTimeout))
		}
		err = sink.ws.WriteMessage(gorilla.BinaryMessage, messageBytes)
		if err != nil {
			sink.logger.Debugf("Websocket Sink %s: Error when trying to send data to sink %s. Requesting close. Err: %v", sink.clientAddress, err)
			close(stopChan)
			return
		}

		sink.logger.Debugf("Websocket Sink %s: Successfully sent data", sink.clientAddress)
	}
}
Beispiel #2
0
func (sink *WebsocketSink) Run(inputChan <-chan *events.Envelope) {
	sink.logger.Debugf("Websocket Sink %s: Running for streamId [%s]", sink.clientAddress, sink.streamId)

	buffer := sinks.RunTruncatingBuffer(inputChan, sink.wsMessageBufferSize, sink.logger, sink.dropsondeOrigin)
	for {
		sink.logger.Debugf("Websocket Sink %s: Waiting for activity", sink.clientAddress)
		messageEnvelope, ok := <-buffer.GetOutputChannel()

		droppedMessages := buffer.GetDroppedMessageCount()
		if droppedMessages != 0 {
			sink.UpdateDroppedMessageCount(droppedMessages)

		}

		if !ok {
			sink.logger.Debugf("Websocket Sink %s: Closed listener channel detected. Closing websocket", sink.clientAddress)
			return
		}

		messageBytes, err := proto.Marshal(messageEnvelope)

		if err != nil {
			sink.logger.Errorf("Websocket Sink %s: Error marshalling %s envelope from origin %s: %s", sink.clientAddress, messageEnvelope.GetEventType().String(), messageEnvelope.GetOrigin(), err.Error())
			continue
		}

		sink.logger.Debugf("Websocket Sink %s: Received %s message from %s at %d. Sending data.", sink.clientAddress, messageEnvelope.GetEventType().String(), messageEnvelope.GetOrigin(), messageEnvelope.Timestamp)
		err = sink.ws.WriteMessage(gorilla.BinaryMessage, messageBytes)
		if err != nil {
			sink.logger.Debugf("Websocket Sink %s: Error when trying to send data to sink %s. Requesting close. Err: %v", sink.clientAddress, err)
			return
		}

		sink.logger.Debugf("Websocket Sink %s: Successfully sent data", sink.clientAddress)
	}
}
Beispiel #3
0
func (s *SyslogSink) Run(inputChan <-chan *events.Envelope) {
	syslogIdentifier := s.Identifier()
	s.logger.Infof("Syslog Sink %s: Running.", syslogIdentifier)
	defer s.logger.Errorf("Syslog Sink %s: Stopped.", syslogIdentifier)

	backoffStrategy := retrystrategy.NewExponentialRetryStrategy()

	context := truncatingbuffer.NewLogAllowedContext(s.dropsondeOrigin, syslogIdentifier)
	buffer := sinks.RunTruncatingBuffer(inputChan, s.messageDrainBufferSize, context, s.logger, s.disconnectChannel)
	timer := time.NewTimer(backoffStrategy(0))
	connected := false
	defer timer.Stop()
	defer s.syslogWriter.Close()

	s.logger.Debugf("Syslog Sink %s: Starting loop. Current backoff: %v", syslogIdentifier, backoffStrategy(0))
	for {
		s.logger.Debugf("Syslog Sink %s: Waiting for activity\n", syslogIdentifier)

		select {
		case <-s.disconnectChannel:
			return
		case messageEnvelope, ok := <-buffer.GetOutputChannel():
			if !ok {
				s.logger.Debugf("Syslog Sink %s: Closed listener channel detected. Closing.\n", syslogIdentifier)
				return
			}

			numberOfTries := 0
			for {
				for !connected {
					s.logger.Debugf("Syslog Sink %s: Not connected. Trying to connect.", syslogIdentifier)
					err := s.syslogWriter.Connect()
					if err == nil {
						s.logger.Infof("Syslog Sink %s: successfully connected.", syslogIdentifier)
						connected = true
						break
					}

					sleepDuration := backoffStrategy(numberOfTries)
					errorMsg := fmt.Sprintf("Syslog Sink %s: Error when dialing out. Backing off for %v. Err: %v", syslogIdentifier, sleepDuration, err)

					s.handleSendError(errorMsg, s.appId)

					timer.Reset(sleepDuration)
					select {
					case <-s.disconnectChannel:
						return
					case <-timer.C:
					}

					numberOfTries++
				}

				err := s.sendLogMessage(messageEnvelope.GetLogMessage())
				if err == nil {
					connected = true
					break
				}

				s.logger.Debugf("Syslog Sink %s: Error when trying to send data to sink. Backing off. Err: %v\n", syslogIdentifier, err)
				connected = false
				numberOfTries++
			}
		}
	}
}
func (s *SyslogSink) Run(inputChan <-chan *events.Envelope) {
	s.logger.Infof("Syslog Sink %s: Running.", s.drainUrl)
	defer s.logger.Errorf("Syslog Sink %s: Stopped.", s.drainUrl)

	backoffStrategy := retrystrategy.NewExponentialRetryStrategy()
	filteredChan := make(chan *events.Envelope)

	go func() {
		defer close(filteredChan)

		for {
			select {
			case v, ok := <-inputChan:
				if !ok {
					return
				}

				if v.GetEventType() != events.Envelope_LogMessage {
					continue
				}

				filteredChan <- v
			case <-s.disconnectChannel:
				return
			}
		}
	}()

	buffer := sinks.RunTruncatingBuffer(filteredChan, s.messageDrainBufferSize, s.logger, s.dropsondeOrigin, s.Identifier())
	timer := time.NewTimer(backoffStrategy(0))
	connected := false
	defer timer.Stop()
	defer s.syslogWriter.Close()

	s.logger.Debugf("Syslog Sink %s: Starting loop. Current backoff: %v", s.drainUrl, backoffStrategy(0))
	for {
		s.logger.Debugf("Syslog Sink %s: Waiting for activity\n", s.drainUrl)

		select {
		case <-s.disconnectChannel:
			return
		case messageEnvelope, ok := <-buffer.GetOutputChannel():
			if !ok {
				s.logger.Debugf("Syslog Sink %s: Closed listener channel detected. Closing.\n", s.drainUrl)
				return
			}

			// Some metrics will not be filter and can get to here (i.e.: TruncatingBuffer dropped message metrics)
			if messageEnvelope.GetEventType() == events.Envelope_LogMessage {
				numberOfTries := 0
				for {
					for !connected {
						s.logger.Debugf("Syslog Sink %s: Not connected. Trying to connect.", s.drainUrl)
						err := s.syslogWriter.Connect()
						if err == nil {
							s.logger.Infof("Syslog Sink %s: successfully connected.", s.drainUrl)
							connected = true
							break
						}

						sleepDuration := backoffStrategy(numberOfTries)
						errorMsg := fmt.Sprintf("Syslog Sink %s: Error when dialing out. Backing off for %v. Err: %v", s.drainUrl, sleepDuration, err)

						s.handleSendError(errorMsg, s.appId, s.drainUrl)

						timer.Reset(sleepDuration)
						select {
						case <-s.disconnectChannel:
							return
						case <-timer.C:
						}

						numberOfTries++
					}

					err := s.sendLogMessage(messageEnvelope.GetLogMessage())
					if err == nil {
						connected = true
						break
					}

					s.logger.Debugf("Syslog Sink %s: Error when trying to send data to sink. Backing off. Err: %v\n", s.drainUrl, err)
					connected = false
					numberOfTries++
				}
			}
		}
	}
}
Beispiel #5
0
func (s *SyslogSink) Run(inputChan <-chan *events.Envelope) {
	s.Infof("Syslog Sink %s: Running.", s.drainUrl)
	defer s.Errorf("Syslog Sink %s: Stopped.", s.drainUrl)

	backoffStrategy := retrystrategy.NewExponentialRetryStrategy()
	numberOfTries := 0
	filteredChan := make(chan *events.Envelope)

	go func() {
		defer close(filteredChan)

		for {
			select {
			case v, ok := <-inputChan:
				if !ok {
					return
				}

				if v.GetEventType() != events.Envelope_LogMessage {
					continue
				}

				filteredChan <- v
			case <-s.disconnectChannel:
				return
			}
		}
	}()

	buffer := sinks.RunTruncatingBuffer(filteredChan, 100, s.Logger, s.dropsondeOrigin)
	timer := time.NewTimer(backoffStrategy(numberOfTries))
	connected := false
	defer timer.Stop()
	defer s.syslogWriter.Close()
	for {
		s.Debugf(starting_loop_debug, s.drainUrl, backoffStrategy(numberOfTries))
		timer.Reset(backoffStrategy(numberOfTries))
		select {
		case <-s.disconnectChannel:
			return
		case <-timer.C:
		}

		if !connected {
			s.Debugf(dialing_debug_string, s.drainUrl)
			err := s.syslogWriter.Connect()
			if err != nil {
				numberOfTries++
				errorMsg := fmt.Sprintf(dial_error_debug_string, s.drainUrl, backoffStrategy(numberOfTries), err)

				s.handleSendError(errorMsg, s.appId, s.drainUrl)
				continue
			}

			s.Infof("Syslog Sink %s: successfully connected.", s.drainUrl)
			connected = true
		}

		s.Debugf("Syslog Sink %s: Waiting for activity\n", s.drainUrl)

		select {
		case <-s.disconnectChannel:
			return
		case messageEnvelope, ok := <-buffer.GetOutputChannel():
			droppedMessages := buffer.GetDroppedMessageCount()
			if droppedMessages != 0 {
				s.UpdateDroppedMessageCount(droppedMessages)

			}

			if !ok {
				s.Debugf("Syslog Sink %s: Closed listener channel detected. Closing.\n", s.drainUrl)
				return
			}
			s.Debugf("Syslog Sink:Run: Received %s message from %s at %d. Sending data.", messageEnvelope.GetEventType().String(), messageEnvelope.GetOrigin(), messageEnvelope.Timestamp)

			connected = s.sendMessage(messageEnvelope)
			if connected {
				numberOfTries = 0
			} else {
				numberOfTries++
			}
		}
	}
}