Skip to content

Commit

Permalink
output logs to stderr if no logger is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
edwingeng committed Feb 10, 2018
1 parent 6caa022 commit db105b5
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 32 deletions.
3 changes: 2 additions & 1 deletion callback/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func (ego *WUID) LoadH24WithCallback(cb func() (uint64, error)) error {
}
}

ego.w.Reset(uint64(h24) << 40)
ego.w.Reset(h24 << 40)
ego.w.Logger.Info(fmt.Sprintf("[wuid] new h24: %d", h24))

ego.w.Lock()
defer ego.w.Unlock()
Expand Down
17 changes: 12 additions & 5 deletions callback/wuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ import (
"testing"
)

type simpleLogger struct{}

func (ego *simpleLogger) Info(args ...interface{}) {}
func (ego *simpleLogger) Warn(args ...interface{}) {}

var sl = &simpleLogger{}

func TestWUID_LoadH24WithCallback_Error(t *testing.T) {
var err error
g := NewWUID("default", nil)
g := NewWUID("default", sl)
err = g.LoadH24WithCallback(nil)
if err == nil {
t.Fatal("LoadH24WithCallback should fail when cb is nil")
Expand All @@ -39,7 +46,7 @@ func TestWUID_LoadH24WithCallback(t *testing.T) {
return atomic.AddUint64(&h24, 1), nil
}

g := NewWUID("default", nil)
g := NewWUID("default", sl)
for i := 0; i < 1000; i++ {
err := g.LoadH24WithCallback(cb)
if err != nil {
Expand All @@ -61,7 +68,7 @@ func TestWUID_LoadH24WithCallback_Section(t *testing.T) {
return atomic.AddUint64(&h24, 1), nil
}

g := NewWUID("default", nil, WithSection(1))
g := NewWUID("default", sl, WithSection(1))
for i := 0; i < 1000; i++ {
err := g.LoadH24WithCallback(cb)
if err != nil {
Expand All @@ -82,13 +89,13 @@ func TestWUID_LoadH24WithCallback_Same(t *testing.T) {
return 100, nil
}

g1 := NewWUID("default", nil)
g1 := NewWUID("default", sl)
_ = g1.LoadH24WithCallback(cb)
if err := g1.LoadH24WithCallback(cb); err == nil {
t.Fatal("LoadH24WithCallback should return an error")
}

g2 := NewWUID("default", nil, WithSection(1))
g2 := NewWUID("default", sl, WithSection(1))
_ = g2.LoadH24WithCallback(cb)
if err := g2.LoadH24WithCallback(cb); err == nil {
t.Fatal("LoadH24WithCallback should return an error")
Expand Down
24 changes: 18 additions & 6 deletions internal/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"errors"
"fmt"
"log"
"sync"
"sync/atomic"
)
Expand All @@ -28,7 +29,12 @@ type WUID struct {

// NewWUID is for internal use only.
func NewWUID(tag string, logger Logger, opts ...Option) *WUID {
w := &WUID{Tag: tag, Logger: logger}
w := &WUID{Tag: tag}
if logger != nil {
w.Logger = logger
} else {
w.Logger = defaultLogger{}
}
for _, opt := range opts {
opt(w)
}
Expand All @@ -44,16 +50,12 @@ func (ego *WUID) Next() uint64 {
if x&0xFFFFFFFFFF >= CriticalValue && x&RenewInterval == 0 {
go func() {
defer func() {
if r := recover(); r != nil && ego.Logger != nil {
if r := recover(); r != nil {
ego.Logger.Warn(fmt.Sprintf("[wuid] panic, renew failed. tag: %s, reason: %+v", ego.Tag, r))
}
}()

err := ego.RenewNow()

if ego.Logger == nil {
return
}
if err != nil {
ego.Logger.Warn(fmt.Sprintf("[wuid] renew failed. tag: %s, reason: %s", ego.Tag, err.Error()))
} else {
Expand Down Expand Up @@ -107,6 +109,16 @@ type Logger interface {
Warn(args ...interface{})
}

type defaultLogger struct{}

func (l defaultLogger) Info(args ...interface{}) {
log.Println(args...)
}

func (l defaultLogger) Warn(args ...interface{}) {
log.Println(args...)
}

// Option is for internal use only.
type Option func(*WUID)

Expand Down
7 changes: 5 additions & 2 deletions mongo/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package wuid

import (
"errors"
"fmt"
"time"

"github.com/edwingeng/wuid/internal"
Expand Down Expand Up @@ -89,11 +90,13 @@ func (ego *WUID) LoadH24FromMongoWithTimeout(addr, user, pass, dbName, coll, doc
if err != nil {
return err
}
if err = ego.w.VerifyH24(uint64(m["n"].(int))); err != nil {
h24 := uint64(m["n"].(int))
if err = ego.w.VerifyH24(h24); err != nil {
return err
}

ego.w.Reset(uint64(m["n"].(int)) << 40)
ego.w.Reset(h24 << 40)
ego.w.Logger.Info(fmt.Sprintf("[wuid] new h24: %d", h24))

ego.w.Lock()
defer ego.w.Unlock()
Expand Down
17 changes: 12 additions & 5 deletions mongo/wuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ import (
"github.com/edwingeng/wuid/internal"
)

type simpleLogger struct{}

func (ego *simpleLogger) Info(args ...interface{}) {}
func (ego *simpleLogger) Warn(args ...interface{}) {}

var sl = &simpleLogger{}

func getMongoConfig() (string, string, string, string, string, string) {
return "127.0.0.1:27017", "", "", "test", "foo", "wuid"
}

func TestWUID_LoadH24FromMongo(t *testing.T) {
var nextValue uint64
g := NewWUID("default", nil)
g := NewWUID("default", sl)
for i := 0; i < 1000; i++ {
err := g.LoadH24FromMongo(getMongoConfig())
if err != nil {
Expand All @@ -38,7 +45,7 @@ func TestWUID_LoadH24FromMongo(t *testing.T) {
}

func TestWUID_LoadH24FromMongo_Error(t *testing.T) {
g := NewWUID("default", nil)
g := NewWUID("default", sl)
addr, user, pass, dbName, coll, docID := getMongoConfig()

if g.LoadH24FromMongo("", user, pass, dbName, coll, docID) == nil {
Expand All @@ -61,7 +68,7 @@ func TestWUID_LoadH24FromMongo_Error(t *testing.T) {

func TestWUID_LoadH24FromMongo_UserPass(t *testing.T) {
var err error
g := NewWUID("default", nil)
g := NewWUID("default", sl)
addr, _, _, dbName, coll, docID := getMongoConfig()
err = g.LoadH24FromMongo(addr, "wuid", "abc123", dbName, coll, docID)
if err != nil {
Expand All @@ -78,7 +85,7 @@ func TestWUID_LoadH24FromMongo_UserPass(t *testing.T) {
}

func TestWUID_Next_Renew(t *testing.T) {
g := NewWUID("default", nil)
g := NewWUID("default", sl)
err := g.LoadH24FromMongo(getMongoConfig())
if err != nil {
t.Fatal(err)
Expand All @@ -103,7 +110,7 @@ func TestWUID_Next_Renew(t *testing.T) {
}

func TestWithSection(t *testing.T) {
g := NewWUID("default", nil, WithSection(15))
g := NewWUID("default", sl, WithSection(15))
err := g.LoadH24FromMongo(getMongoConfig())
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 4 additions & 2 deletions mysql/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ func (ego *WUID) LoadH24FromMysql(addr, user, pass, dbName, table string) error
if err != nil {
return err
}
if err = ego.w.VerifyH24(uint64(lastInsertedID)); err != nil {
h24 := uint64(lastInsertedID)
if err = ego.w.VerifyH24(h24); err != nil {
return err
}

ego.w.Reset(uint64(lastInsertedID) << 40)
ego.w.Reset(h24 << 40)
ego.w.Logger.Info(fmt.Sprintf("[wuid] new h24: %d", h24))

ego.w.Lock()
defer ego.w.Unlock()
Expand Down
17 changes: 12 additions & 5 deletions mysql/wuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ import (
"github.com/edwingeng/wuid/internal"
)

type simpleLogger struct{}

func (ego *simpleLogger) Info(args ...interface{}) {}
func (ego *simpleLogger) Warn(args ...interface{}) {}

var sl = &simpleLogger{}

func getMysqlConfig() (string, string, string, string, string) {
return "127.0.0.1:3306", "root", "", "test", "wuid"
}

func TestWUID_LoadH24FromMysql(t *testing.T) {
var nextValue uint64
g := NewWUID("default", nil)
g := NewWUID("default", sl)
for i := 0; i < 1000; i++ {
err := g.LoadH24FromMysql(getMysqlConfig())
if err != nil {
Expand All @@ -38,7 +45,7 @@ func TestWUID_LoadH24FromMysql(t *testing.T) {
}

func TestWUID_LoadH24FromMysql_Error(t *testing.T) {
g := NewWUID("default", nil)
g := NewWUID("default", sl)
addr, user, pass, dbName, table := getMysqlConfig()

if g.LoadH24FromMysql("", user, pass, dbName, table) == nil {
Expand All @@ -61,7 +68,7 @@ func TestWUID_LoadH24FromMysql_Error(t *testing.T) {

func TestWUID_LoadH24FromMysql_UserPass(t *testing.T) {
var err error
g := NewWUID("default", nil)
g := NewWUID("default", sl)
addr, _, _, dbName, table := getMysqlConfig()
err = g.LoadH24FromMysql(addr, "wuid", "abc123", dbName, table)
if err != nil {
Expand All @@ -78,7 +85,7 @@ func TestWUID_LoadH24FromMysql_UserPass(t *testing.T) {
}

func TestWUID_Next_Renew(t *testing.T) {
g := NewWUID("default", nil)
g := NewWUID("default", sl)
err := g.LoadH24FromMysql(getMysqlConfig())
if err != nil {
t.Fatal(err)
Expand All @@ -103,7 +110,7 @@ func TestWUID_Next_Renew(t *testing.T) {
}

func TestWithSection(t *testing.T) {
g := NewWUID("default", nil, WithSection(15))
g := NewWUID("default", sl, WithSection(15))
err := g.LoadH24FromMysql(getMysqlConfig())
if err != nil {
t.Fatal(err)
Expand Down
9 changes: 6 additions & 3 deletions redis/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package wuid

import (
"errors"
"fmt"

"github.com/edwingeng/wuid/internal"
"github.com/go-redis/redis"
Expand Down Expand Up @@ -58,15 +59,17 @@ func (ego *WUID) LoadH24FromRedis(addr, pass, key string) error {
})
defer client.Close()

h24, err := client.Incr(key).Result()
n, err := client.Incr(key).Result()
if err != nil {
return err
}
if err = ego.w.VerifyH24(uint64(h24)); err != nil {
h24 := uint64(n)
if err = ego.w.VerifyH24(h24); err != nil {
return err
}

ego.w.Reset(uint64(h24) << 40)
ego.w.Reset(h24 << 40)
ego.w.Logger.Info(fmt.Sprintf("[wuid] new h24: %d", h24))

ego.w.Lock()
defer ego.w.Unlock()
Expand Down
13 changes: 10 additions & 3 deletions redis/wuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (
"github.com/go-redis/redis"
)

type simpleLogger struct{}

func (ego *simpleLogger) Info(args ...interface{}) {}
func (ego *simpleLogger) Warn(args ...interface{}) {}

var sl = &simpleLogger{}

func getRedisConfig() (string, string, string) {
return "127.0.0.1:6379", "", "wuid"
}
Expand All @@ -30,7 +37,7 @@ func TestWUID_LoadH24FromRedis(t *testing.T) {
t.Fatal(err)
}

g := NewWUID("default", nil)
g := NewWUID("default", sl)
for i := 0; i < 1000; i++ {
err = g.LoadH24FromRedis(getRedisConfig())
if err != nil {
Expand All @@ -47,7 +54,7 @@ func TestWUID_LoadH24FromRedis(t *testing.T) {
}

func TestWUID_LoadH24FromRedis_Error(t *testing.T) {
g := NewWUID("default", nil)
g := NewWUID("default", sl)
addr, pass, key := getRedisConfig()

if g.LoadH24FromRedis("", pass, key) == nil {
Expand Down Expand Up @@ -88,7 +95,7 @@ func TestWUID_Next_Renew(t *testing.T) {
}

func TestWithSection(t *testing.T) {
g := NewWUID("default", nil, WithSection(15))
g := NewWUID("default", sl, WithSection(15))
err := g.LoadH24FromRedis(getRedisConfig())
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit db105b5

Please sign in to comment.