Skip to content

Commit

Permalink
* error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shell909090 committed Feb 15, 2017
1 parent 52c3275 commit 182d584
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
40 changes: 32 additions & 8 deletions backend/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const (

type Backends struct {
*HttpBackend
fb *FileBackend
Interval int
Timeout int
fb *FileBackend
Interval int
Timeout int
MaxRowLimit int32

running bool
ticker *time.Ticker
Expand All @@ -38,6 +39,7 @@ func NewBackends(cfg *BackendConfig, name string) (bs *Backends, err error) {
ch_write: make(chan []byte, 16),

rewriter_running: false,
MaxRowLimit: int32(cfg.MaxRowLimit),
}
bs.fb, err = NewFileBackend(name)
if err != nil {
Expand Down Expand Up @@ -114,8 +116,12 @@ func (bs *Backends) WriteBuffer(p []byte) {
}
}

if bs.ch_timer == nil {
bs.ch_timer = time.After(time.Millisecond * time.Duration(bs.Interval))
switch {
case bs.write_counter >= bs.MaxRowLimit:
bs.Flush()
case bs.ch_timer == nil:
bs.ch_timer = time.After(
time.Millisecond * time.Duration(bs.Interval))
}

return
Expand Down Expand Up @@ -148,8 +154,17 @@ func (bs *Backends) Flush() {
// maybe blocked here, run in another goroutine
if bs.HttpBackend.IsActive() {
err = bs.HttpBackend.WriteCompressed(p)
if err == nil {
switch err {
case nil:
return
case ErrBadRequest:
log.Printf("bad request, drop all data.")
return
case ErrNotFound:
log.Printf("bad backend, drop all data.")
return
default:
log.Printf("unknown error %s, maybe overloaded.", err)
}
log.Printf("write http error: %s\n", err)
}
Expand Down Expand Up @@ -199,8 +214,17 @@ func (bs *Backends) Rewrite() (err error) {
}

err = bs.HttpBackend.WriteCompressed(p)
if err != nil {
log.Printf("rewrite http error: %s\n", err)

switch err {
case nil:
case ErrBadRequest:
log.Printf("bad request, drop all data.")
err = nil
case ErrNotFound:
log.Printf("bad backend, drop all data.")
err = nil
default:
log.Printf("unknown error %s, maybe overloaded.", err)

err = bs.fb.RollbackMeta()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type BackendConfig struct {
Interval int
Timeout int
TimeoutQuery int
MaxRowLimit int
}

type RedisConfigSource struct {
Expand Down Expand Up @@ -149,6 +150,9 @@ func (rcs *RedisConfigSource) LoadConfigFromRedis(name string) (cfg *BackendConf
if cfg.TimeoutQuery == 0 {
cfg.TimeoutQuery = 60000
}
if cfg.MaxRowLimit == 0 {
cfg.MaxRowLimit = 10000
}
return
}

Expand Down

0 comments on commit 182d584

Please sign in to comment.