some refactoring

This commit is contained in:
Sangeet Kumar 2024-10-03 14:04:17 +05:30
parent b4c9386577
commit 1e2975ad0f
4 changed files with 14 additions and 18 deletions

View File

@ -7,6 +7,7 @@ import (
type Client struct {
es *elasticsearch.Client
config *Config
ch chan Data
}
func NewClient(config *Config) (*Client, error) {
@ -30,11 +31,14 @@ func NewClient(config *Config) (*Client, error) {
}
func (c *Client) Open() error {
// create a buffer channel
ch := make(chan Data, 1)
for index, from := range c.config.Indexes {
NewWorker(c.es, index, from, c.config.BatchSize, ch)
// Open a connection with ElasticSearch
// create a buffer channel
c.ch = make(chan Data, 1)
for _, index := range c.config.Indexes {
NewWorker(c, index)
}
return nil

View File

@ -9,8 +9,8 @@ import (
func main() {
config := &elasticstream.Config{
Host: "http://localhost:9200",
Indexes: map[string]int{"users": 0, "students": 0, "teachers": 0},
Host: "http://test.urantiacloud.com:9200",
Indexes: []string{"IndexA", "IndexB", "IndexC"},
BatchSize: 10,
}

View File

@ -3,6 +3,6 @@ package elasticstream
type Config struct {
Host string
// map of index name and position from where data is to be read.
Indexes map[string]int
Indexes []string
BatchSize int
}

View File

@ -1,29 +1,21 @@
package elasticstream
import (
"github.com/elastic/go-elasticsearch/v8"
// "github.com/elastic/go-elasticsearch/v8"
)
type Worker struct {
client *elasticsearch.Client
client *Client
index string // name of the indexes worker pulls data from
from int // from where to start read data
size int // batch size
buffer chan Data
}
func NewWorker(client *elasticsearch.Client, index string, from, size int, buffer chan Data) *Worker {
func NewWorker(client *Client, index string) {
w := &Worker{
client: client,
index: index,
from: from,
size: size,
buffer: buffer,
}
go w.start()
return w
}
func (w *Worker) start() {