Пример #1
0
func (cons *Socket) readFromConnection(conn net.Conn) {
	defer func() {
		conn.Close()
		cons.WorkerDone()
	}()

	conn.SetDeadline(time.Time{})
	buffer := shared.NewBufferedReader(socketBufferGrowSize, cons.flags, cons.offset, cons.delimiter)

	for !cons.quit {
		err := buffer.ReadAll(conn, cons.Enqueue)

		// Handle errors
		if err != nil && err != io.EOF {
			if cons.clientDisconnected(err) {
				return // ### return, connection closed ###
			}

			Log.Error.Print("Socket read failed: ", err)
			continue // ### continue, keep open, try again ###
		}

		// Send ack if everything was ok
		if cons.acknowledge != "" {
			fmt.Fprint(conn, cons.acknowledge)
		}
	}
}
Пример #2
0
func (cons *Socket) processConnection(conn net.Conn) {
	cons.AddWorker()
	defer cons.WorkerDone()
	defer conn.Close()

	buffer := shared.NewBufferedReader(socketBufferGrowSize, cons.flags, cons.offset, cons.delimiter)

	for cons.IsActive() && !cons.IsFuseBurned() {
		conn.SetReadDeadline(time.Now().Add(cons.readTimeout))
		err := buffer.ReadAll(conn, cons.Enqueue)
		if err == nil {
			if err = cons.sendAck(conn, true); err == nil {
				continue // ### continue, all is well ###
			}
		}

		// Silently exit on disconnect/close
		if !cons.IsActive() || shared.IsDisconnectedError(err) {
			return // ### return, connection closed ###
		}

		// Ignore timeout related errors
		if netErr, isNetErr := err.(net.Error); isNetErr && netErr.Timeout() {
			continue // ### return, ignore timeouts ###
		}

		Log.Error.Print("Socket transfer failed: ", err)
		cons.sendAck(conn, false)

		// Parser errors do not drop the connection
		if err != shared.BufferDataInvalid {
			return // ### return, close connections ###
		}
	}
}
Пример #3
0
func (cons *File) read() {
	defer cons.close()

	sendFunction := cons.Enqueue
	if cons.offsetFileName != "" {
		sendFunction = cons.enqueueAndPersist
	}

	buffer := shared.NewBufferedReader(fileBufferGrowSize, 0, 0, cons.delimiter)
	printFileOpenError := true

	for cons.state != fileStateDone {

		// Initialize the seek state if requested
		// Try to read the remains of the file first
		if cons.state == fileStateOpen {
			if cons.file != nil {
				buffer.ReadAll(cons.file, sendFunction)
			}
			cons.initFile()
			buffer.Reset(uint64(cons.seekOffset))
		}

		// Try to open the file to read from
		if cons.state == fileStateRead && cons.file == nil {
			file, err := os.OpenFile(cons.realFileName(), os.O_RDONLY, 0666)

			switch {
			case err != nil:
				if printFileOpenError {
					Log.Error.Print("File open error - ", err)
					printFileOpenError = false
				}
				time.Sleep(3 * time.Second)
				continue // ### continue, retry ###

			default:
				cons.file = file
				cons.seekOffset, _ = cons.file.Seek(cons.seekOffset, cons.seek)
				printFileOpenError = true
			}
		}

		// Try to read from the file
		if cons.state == fileStateRead && cons.file != nil {
			err := buffer.ReadAll(cons.file, sendFunction)

			switch {
			case err == nil: // ok
			case err == io.EOF:
				runtime.Gosched()
			case cons.state == fileStateRead:
				Log.Error.Print("Error reading file - ", err)
				cons.file.Close()
				cons.file = nil
			}
		}
	}
}
Пример #4
0
// Configure initializes this producer with values from a plugin config.
func (prod *Proxy) Configure(conf core.PluginConfig) error {
	err := prod.ProducerBase.Configure(conf)
	if err != nil {
		return err
	}
	prod.SetStopCallback(prod.close)

	prod.bufferSizeKB = conf.GetInt("ConnectionBufferSizeKB", 1<<10) // 1 MB
	prod.address, prod.protocol = shared.ParseAddress(conf.GetString("Address", ":5880"))
	if prod.protocol == "udp" {
		return fmt.Errorf("Proxy does not support UDP")
	}

	prod.timeout = time.Duration(conf.GetInt("TimeoutSec", 1)) * time.Second

	delimiter := shared.Unescape(conf.GetString("Delimiter", "\n"))
	offset := conf.GetInt("Offset", 0)
	flags := shared.BufferedReaderFlagEverything // pass all messages as-is

	partitioner := strings.ToLower(conf.GetString("Partitioner", "delimiter"))
	switch partitioner {
	case "binary_be":
		flags |= shared.BufferedReaderFlagBigEndian
		fallthrough

	case "binary", "binary_le":
		switch conf.GetInt("Size", 4) {
		case 1:
			flags |= shared.BufferedReaderFlagMLE8
		case 2:
			flags |= shared.BufferedReaderFlagMLE16
		case 4:
			flags |= shared.BufferedReaderFlagMLE32
		case 8:
			flags |= shared.BufferedReaderFlagMLE64
		default:
			return fmt.Errorf("Size only supports the value 1,2,4 and 8")
		}

	case "fixed":
		flags |= shared.BufferedReaderFlagMLEFixed
		offset = conf.GetInt("Size", 1)

	case "ascii":
		flags |= shared.BufferedReaderFlagMLE

	case "delimiter":
		// Nothing to add

	default:
		return fmt.Errorf("Unknown partitioner: %s", partitioner)
	}

	prod.reader = shared.NewBufferedReader(prod.bufferSizeKB, flags, offset, delimiter)
	return nil
}
Пример #5
0
func (client *proxyClient) read() {
	buffer := shared.NewBufferedReader(proxyClientBufferGrowSize, client.proxy.flags, client.proxy.offset, client.proxy.delimiter)

	for !client.proxy.quit && client.connected {
		err := buffer.ReadAll(client.conn, client.sendMessage)

		// Handle read errors
		if err != nil && err != io.EOF {
			if client.hasDisconnected(err) {
				return // ### return, connection closed ###
			}
			Log.Error.Print("Proxy read failed: ", err)
		}
	}
}
Пример #6
0
func (cons *Console) readStdIn() {
	buffer := shared.NewBufferedReader(consoleBufferGrowSize, 0, 0, "\n")

	for {
		err := buffer.ReadAll(os.Stdin, cons.Enqueue)
		switch err {
		case io.EOF:
			if cons.autoexit {
				proc, _ := os.FindProcess(os.Getpid())
				proc.Signal(os.Interrupt)
			}

		case nil:
			// ignore
		default:
			Log.Error.Print("Error reading stdin: ", err)
		}
	}
}
Пример #7
0
func (cons *Console) readStdIn() {
	buffer := shared.NewBufferedReader(consoleBufferGrowSize, 0, 0, "\n")

	for cons.IsActive() {
		err := buffer.ReadAll(os.Stdin, cons.Enqueue)
		cons.WaitOnFuse()
		switch err {
		case io.EOF:
			if cons.autoexit {
				// TODO: Hack
				Log.Debug.Print("Console triggered exit.")
				proc, _ := os.FindProcess(os.Getpid())
				proc.Signal(os.Interrupt)
			}

		case nil:
			// ignore
		default:
			Log.Error.Print("Error reading stdin: ", err)
		}
	}
}