Exemplo n.º 1
0
func (w *WriteClient) write(data []byte) error {
	msgCount := atomic.AddUint64(&w.msgCount, 1)

	md := model.NewMetaData(w.id, msgCount, model.NewUUID()) //TODO  transactionid should be supplied
	entry := model.NewLogEntry(md, data)
	request := model.NewWriteRequest(entry)

	conn := w.connectionPool.Connection()
	if _, err := conn.Write(encoder.EncodePayload(request)); err != nil {
		return err
	}

	return nil
}
Exemplo n.º 2
0
//NewWriteClient creates a new WriteClient instance
func NewWriteClient(servers []string) *WriteClient {
	client := &WriteClient{
		id:             model.NewUUID(),
		wChan:          make(chan []byte),
		quitChan:       make(chan bool),
		connectionPool: NewRoundRobinConnectionPool(servers),
	}

	go func(w *WriteClient) {
		for {
			select {
			case data := <-w.wChan:
				if err := w.write(data); err != nil {
					log.Fatal(err)
				}
			case <-w.quitChan:
				w.close()
				return
			}
		}
	}(client)

	return client
}