Skip to content

Commit

Permalink
Merge pull request #697 from batazor/optimization_write_to_store
Browse files Browse the repository at this point in the history
Banch package
  • Loading branch information
batazor authored Aug 7, 2020
2 parents 100346d + 684393b commit b2d2c62
Show file tree
Hide file tree
Showing 27 changed files with 731 additions and 559 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.*
docs
github.com
initialState
*.yml
*.yaml
*.md
Expand Down
29 changes: 21 additions & 8 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,47 @@

### Select: postgres, mongo, redis, dgraph, sqlite, leveldb, badger, ram, rethinkdb
### Default: ram
STORE_TYPE="ram"
STORE_TYPE="mongo"

### Badger
### RAM ----------------------------------------------------------------------------------------------------------------
### Select (enum): MODE_SINGLE_WRITE, MODE_BATCH_WRITE
### Default: 0
STORE_RAM_MODE_WRITE=0

### Badger -------------------------------------------------------------------------------------------------------------
### Default: /tmp/links.badger
STORE_BADGER_PATH="/tmp/links.badger"

### Dgraph
### Dgraph -------------------------------------------------------------------------------------------------------------
### Default: localhost:9080
STORE_DGRAPH_URI="localhost:9080"

### LevelDB
### LevelDB ------------------------------------------------------------------------------------------------------------
### Default: /tmp/links.db
STORE_LEVELDB_PATH="/tmp/links.db"

### MongoDB
### MongoDB ------------------------------------------------------------------------------------------------------------
### Docs: https://docs.mongodb.com/manual/reference/connection-string/
### Default: mongodb://localhost:27017
STORE_MONGODB_URI="mongodb://localhost:27017/shortlink"

### Postgres
### Select (enum): MODE_SINGLE_WRITE, MODE_BATCH_WRITE
### Default: 0
STORE_MONGODB_MODE_WRITE=0

### Postgres -----------------------------------------------------------------------------------------------------------
### Default: postgres://shortlink:shortlink@localhost:5435/shortlink
STORE_POSTGRES_URI="postgres://shortlink:shortlink@localhost:5435/shortlink?sslmode=disable"

### Redis
### Select (enum): MODE_SINGLE_WRITE, MODE_BATCH_WRITE
### Default: 0
STORE_POSTGRES_MODE_WRITE=0

### Redis --------------------------------------------------------------------------------------------------------------
### Default: localhost:6379
STORE_REDIS_URI="localhost:6379"

### SQLite
### SQLite -------------------------------------------------------------------------------------------------------------
### Default: /tmp/links.sqlite
STORE_SQLITE_PATH="/tmp/links.sqlite"

Expand Down
335 changes: 0 additions & 335 deletions bindata.go

This file was deleted.

6 changes: 0 additions & 6 deletions dist/CHANGELOG.md

This file was deleted.

100 changes: 0 additions & 100 deletions dist/config.yaml

This file was deleted.

20 changes: 12 additions & 8 deletions docs/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,33 @@ File generated by cli. DO NOT EDIT.
| "BOT_TELEGRAM_WEBHOOK" | YOUR_WEBHOOK_URL_HERE | Your webhook URL |
| "BOT_TELEGRAM_CHAT_ID" | 123 | Your chat ID |
| "BOT_TELEGRAM_DEBUG_MODE" | false | Debug mode |
| "LOG_LEVEL" | logger.INFO_LEVEL | Log level. Select 0-4 (Fatal->Debug) |
| "LOG_TIME_FORMAT" | time.RFC3339Nano | Log time format (golang time format) |
| "TRACER_SERVICE_NAME" | ShortLink | |
| "TRACER_URI" | localhost:6831 | |
| "MQ_ENABLED" | false | |
| "LOG_LEVEL" | logger.INFO_LEVEL | |
| "LOG_TIME_FORMAT" | time.RFC3339Nano | |
| "TRACER_SERVICE_NAME" | ShortLink | Service Name |
| "TRACER_URI" | localhost:6831 | Tracing addr:host |
| "MQ_ENABLED" | false | Enabled MQ-service |
| "SENTRY_DSN" | __DSN__ | key for sentry |
| "MQ_TYPE" | kafka | Select: kafka, nats |
| "LOG_LEVEL" | logger.INFO_LEVEL | |
| "LOG_TIME_FORMAT" | time.RFC3339Nano | |
| "TRACER_SERVICE_NAME" | ShortLink | |
| "TRACER_URI" | localhost:6831 | |
| "MQ_ENABLED" | false | |
| "SENTRY_DSN" | __DSN__ | |
| "MQ_TYPE" | rabbitmq | Select: kafka, rabbitmq, nats |
| "MQ_KAFKA_URI" | localhost:9092 | Kafka URI |
| "MQ_KAFKA_CONSUMER_GROUP" | shortlink | Kafka consumer group |
| "MQ_RABBIT_URI" | amqp://localhost:5672 | RabbitMQ URI |
| "STORE_TYPE" | ram | Select: postgres, mongo, mysql, redis, dgraph, sqlite, leveldb, badger, ram, scylla, cassandra |
| "STORE_BADGER_PATH" | /tmp/links.badger | Badger path to file |
| "STORE_CASSANDRA_URI" | localhost:9042 | Cassandra URI |
| "STORE_DGRAPH_URI" | localhost:9080 | DGRAPH URI |
| "STORE_LEVELDB_PATH" | /tmp/links.db | LevelDB path to file |
| "STORE_MONGODB_URI" | mongodb://localhost:27017 | MongoDB URI |
| "STORE_MONGODB_URI" | mongodb://localhost:27017/shortlink | MongoDB URI |
| "STORE_MYSQL_URI" | shortlink:shortlink@(localhost:3306)/shortlink?parseTime=true | MySQL URI |
| "STORE_POSTGRES_URI" | postgres://shortlink:shortlink@localhost:5435/shortlink?pool_max_conns=10 | Postgres URI |
| "STORE_POSTGRES_URI" | postgres://shortlink:shortlink@localhost:5435/shortlink?sslmode=disable | Postgres URI |
| "STORE_RAM_MODE_WRITE" | options.MODE_SINGLE_WRITE | mode write to store |
| "STORE_REDIS_URI" | localhost:6379 | Redis URI |
| "STORE_RETHINKDB_URI" | localhost:28015 | RethinkDB URI |
| "STORE_SCYLLA_URI" | localhost:9042 | Scylla URI |
| "STORE_SQLITE_PATH" | /tmp/links.sqlite | SQLite URI |
| "API_TYPE" | http-chi | Select: http-chi, gRPC-web, graphql, cloudevents, go-kit |
Expand Down
2 changes: 1 addition & 1 deletion docs/shortctl.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ shortctl [flags]
--o string Output file path (default "./docs/env.md")
```

###### Auto generated by spf13/cobra on 17-Jun-2020
###### Auto generated by spf13/cobra on 4-Aug-2020
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ github.com/jackc/puddle v1.1.1 h1:PJAw7H/9hoWC4Kf3J8iNmL1SwA6E8vfsLqBiL+F6CtI=
github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
github.com/jcmturner/gofork v1.0.0 h1:J7uCkflzTEhUZ64xqKnkDxq3kzc96ajM1Gli5ktUem8=
github.com/jcmturner/gofork v1.0.0/go.mod h1:MK8+TM0La+2rjBD4jE12Kj1pCCxK7d2LK/UM3ncEo0o=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
Expand Down Expand Up @@ -888,6 +889,7 @@ go.opencensus.io v0.22.2 h1:75k/FF0Q2YM8QYo07VPddOLBslDt1MZOdEslOHvmzAs=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4 h1:LYy1Hy3MJdrCdMwwzxA/dRok4ejH+RwNGbuoD9fCjto=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down Expand Up @@ -956,6 +958,7 @@ golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNT
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
golang.org/x/lint v0.0.0-20200130185559-910be7a94367 h1:0IiAsCRByjO2QjX7ZPkw5oU9x+n1YqRL802rjC0c3Aw=
golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
Expand Down Expand Up @@ -1007,6 +1010,7 @@ golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU=
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand All @@ -1023,6 +1027,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -1151,6 +1156,7 @@ golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200626171337-aa94e735be7f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200713011307-fd294ab11aed/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200725200936-102e7d357031 h1:VtIxiVHWPhnny2ZTi4f9/2diZKqyLaq3FUTuud5+khA=
golang.org/x/tools v0.0.0-20200725200936-102e7d357031/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
68 changes: 68 additions & 0 deletions internal/batch/batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package batch

import (
"context"
"time"
)

// TODO: add config for as timeout, retries, etc...
// New - create a new batch
func New(ctx context.Context, cb func([]*Item) interface{}) (*Config, error) {
cnf := Config{
cb: cb,
Interval: time.Millisecond * 100,
}

// run background job
go cnf.run(ctx)

return &cnf, nil
}

func (c *Config) Push(item interface{}) (chan interface{}, error) {
// create new item
el := NewItem(item)

c.mx.Lock()
c.items = append(c.items, el)
c.mx.Unlock()

return el.CB, nil
}

// run - starts a loop flushing at the Interval
func (c *Config) run(ctx context.Context) {
ticker := time.NewTicker(c.Interval)

for {
select {
case <-ctx.Done():
for key := range c.items {
c.items[key].CB <- "ctx close"
}

break
case <-ticker.C:
c.mx.Lock()

// skip if items empty
if len(c.items) > 0 {
// apply func for all items
c.cb(c.items)

// clear items
c.items = []*Item{}
}

c.mx.Unlock()
}
}
}

func NewItem(item interface{}) *Item {
cb := make(chan interface{})
return &Item{
CB: cb,
Item: item,
}
}
61 changes: 61 additions & 0 deletions internal/batch/batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package batch

import (
"context"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

//func TestMain(m *testing.M) {
// goleak.VerifyTestMain(m)
//}

func TestNew(t *testing.T) {
t.Run("Create new a batch", func(t *testing.T) {
// Add events
wg := sync.WaitGroup{}

ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
aggrCB := func(args []*Item) interface{} {
// Get string
for _, item := range args {
//time.Sleep(time.Second * 2) // Emulate long work

item.CB <- item.Item.(string)
}

return nil
}

b, err := New(ctx, aggrCB)
assert.Nil(t, err)

request := []string{"A", "B", "C", "D"}
for key := range request {
wg.Add(1)
res, err := b.Push(request[key])
assert.Nil(t, err)
go func(key int) {
assert.Equal(t, <-res, request[key])
wg.Done()
}(key)
}

time.Sleep(time.Second * 1)
cancel()
for key := range request {
wg.Add(1)
res, err := b.Push(request[key])
assert.Nil(t, err)
go func() {
assert.Equal(t, <-res, "ctx close")
wg.Done()
}()
}
wg.Wait()
})
}
Loading

0 comments on commit b2d2c62

Please sign in to comment.