Skip to content

Commit

Permalink
rename: this >> w
Browse files Browse the repository at this point in the history
  • Loading branch information
edwingeng committed Feb 1, 2018
1 parent b84d65b commit ed4ec96
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 81 deletions.
32 changes: 16 additions & 16 deletions callback/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,46 @@ func NewWUID(tag string, logger Logger, opts ...Option) *WUID {
}

// Next returns the next unique number.
func (this *WUID) Next() uint64 {
return this.w.Next()
func (w *WUID) Next() uint64 {
return w.w.Next()
}

// LoadH24WithCallback calls cb to get a number, and then sets it as the high 24 bits of the unique
// numbers that Next generates.
// The number returned by cb should look like 0x000123, not 0x0001230000000000.
func (this *WUID) LoadH24WithCallback(cb func() (uint64, error)) error {
func (w *WUID) LoadH24WithCallback(cb func() (uint64, error)) error {
if cb == nil {
return errors.New("cb cannot be nil. tag: " + this.w.Tag)
return errors.New("cb cannot be nil. tag: " + w.w.Tag)
}

h24, err := cb()
if err != nil {
return err
}

if err = this.w.VerifyH24(h24); err != nil {
if err = w.w.VerifyH24(h24); err != nil {
return err
}
if this.w.Section == 0 {
if h24 == atomic.LoadUint64(&this.w.N)>>40 {
return fmt.Errorf("the h24 should be a different value other than %d. tag: %s", h24, this.w.Tag)
if w.w.Section == 0 {
if h24 == atomic.LoadUint64(&w.w.N)>>40 {
return fmt.Errorf("the h24 should be a different value other than %d. tag: %s", h24, w.w.Tag)
}
} else {
if h24 == (atomic.LoadUint64(&this.w.N)>>40)&0x0FFFFF {
return fmt.Errorf("the h20 should be a different value other than %d. tag: %s", h24, this.w.Tag)
if h24 == (atomic.LoadUint64(&w.w.N)>>40)&0x0FFFFF {
return fmt.Errorf("the h20 should be a different value other than %d. tag: %s", h24, w.w.Tag)
}
}

this.w.Reset(uint64(h24) << 40)
w.w.Reset(uint64(h24) << 40)

this.w.Lock()
defer this.w.Unlock()
w.w.Lock()
defer w.w.Unlock()

if this.w.Renew != nil {
if w.w.Renew != nil {
return nil
}
this.w.Renew = func() error {
return this.LoadH24WithCallback(cb)
w.w.Renew = func() error {
return w.LoadH24WithCallback(cb)
}

return nil
Expand Down
38 changes: 19 additions & 19 deletions internal/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,54 +29,54 @@ func NewWUID(tag string, logger Logger, opts ...Option) *WUID {
return w
}

func (this *WUID) Next() uint64 {
x := atomic.AddUint64(&this.N, 1)
func (w *WUID) Next() uint64 {
x := atomic.AddUint64(&w.N, 1)
if x&0xFFFFFFFFFF >= CriticalValue && x&RenewInterval == 0 {
this.Lock()
renew := this.Renew
this.Unlock()
w.Lock()
renew := w.Renew
w.Unlock()

go func() {
defer func() {
if r := recover(); r != nil && this.Logger != nil {
this.Logger.Warn(fmt.Sprintf("[wuid] panic. tag: %s, reason: %+v", this.Tag, r))
if r := recover(); r != nil && w.Logger != nil {
w.Logger.Warn(fmt.Sprintf("[wuid] panic. tag: %s, reason: %+v", w.Tag, r))
}
}()

err := renew()
if this.Logger == nil {
if w.Logger == nil {
return
}
if err != nil {
this.Logger.Warn(fmt.Sprintf("[wuid] renew failed. tag: %s, reason: %s", this.Tag, err.Error()))
w.Logger.Warn(fmt.Sprintf("[wuid] renew failed. tag: %s, reason: %s", w.Tag, err.Error()))
} else {
this.Logger.Info(fmt.Sprintf("[wuid] renew succeeded. tag: %s", this.Tag))
w.Logger.Info(fmt.Sprintf("[wuid] renew succeeded. tag: %s", w.Tag))
}
}()
}
return x
}

func (this *WUID) Reset(n uint64) {
if this.Section == 0 {
atomic.StoreUint64(&this.N, n)
func (w *WUID) Reset(n uint64) {
if w.Section == 0 {
atomic.StoreUint64(&w.N, n)
} else {
atomic.StoreUint64(&this.N, n&0x0FFFFFFFFFFFFFFF|uint64(this.Section)<<60)
atomic.StoreUint64(&w.N, n&0x0FFFFFFFFFFFFFFF|uint64(w.Section)<<60)
}
}

func (this *WUID) VerifyH24(h24 uint64) error {
func (w *WUID) VerifyH24(h24 uint64) error {
if h24 == 0 {
return errors.New("the h24 should not be 0. tag: " + this.Tag)
return errors.New("the h24 should not be 0. tag: " + w.Tag)
}

if this.Section == 0 {
if w.Section == 0 {
if h24 > 0xFFFFFF {
return errors.New("the h24 should not exceed 0xFFFFFF. tag: " + this.Tag)
return errors.New("the h24 should not exceed 0xFFFFFF. tag: " + w.Tag)
}
} else {
if h24 > 0x0FFFFF {
return errors.New("the h20 should not exceed 0x0FFFFF. tag: " + this.Tag)
return errors.New("the h20 should not exceed 0x0FFFFF. tag: " + w.Tag)
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/wuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ type simpleLogger struct {
numWarn int
}

func (this *simpleLogger) Info(args ...interface{}) {
func (w *simpleLogger) Info(args ...interface{}) {
str := "INFO\t"
str += fmt.Sprint(args...)
log.Println(str)
this.numInfo++
w.numInfo++
}

func (this *simpleLogger) Warn(args ...interface{}) {
func (w *simpleLogger) Warn(args ...interface{}) {
str := "WARN\t"
str += fmt.Sprint(args...)
log.Println(str)
this.numWarn++
w.numWarn++
}

func TestWUID_Next_Renew(t *testing.T) {
Expand Down
32 changes: 16 additions & 16 deletions mongo/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,30 @@ func NewWUID(tag string, logger Logger, opts ...Option) *WUID {
}

// Next returns the next unique number.
func (this *WUID) Next() uint64 {
return this.w.Next()
func (w *WUID) Next() uint64 {
return w.w.Next()
}

// LoadH24FromMongo adds 1 to a specific number in your MongoDB, fetches the new value,
// and then sets it as the high 24 bits of the unique numbers that Next generates.
func (this *WUID) LoadH24FromMongo(addr, user, pass, dbName, coll, docID string) error {
return this.LoadH24FromMongoWithTimeout(addr, user, pass, dbName, coll, docID, 3*time.Second)
func (w *WUID) LoadH24FromMongo(addr, user, pass, dbName, coll, docID string) error {
return w.LoadH24FromMongoWithTimeout(addr, user, pass, dbName, coll, docID, 3*time.Second)
}

// LoadH24FromMongoWithTimeout adds 1 to a specific number in your MongoDB, fetches the new value,
// and then sets it as the high 24 bits of the unique numbers that Next generates.
func (this *WUID) LoadH24FromMongoWithTimeout(addr, user, pass, dbName, coll, docID string, dialTimeout time.Duration) error {
func (w *WUID) LoadH24FromMongoWithTimeout(addr, user, pass, dbName, coll, docID string, dialTimeout time.Duration) error {
if len(addr) == 0 {
return errors.New("addr cannot be empty. tag: " + this.w.Tag)
return errors.New("addr cannot be empty. tag: " + w.w.Tag)
}
if len(dbName) == 0 {
return errors.New("dbName cannot be empty. tag: " + this.w.Tag)
return errors.New("dbName cannot be empty. tag: " + w.w.Tag)
}
if len(coll) == 0 {
return errors.New("coll cannot be empty. tag: " + this.w.Tag)
return errors.New("coll cannot be empty. tag: " + w.w.Tag)
}
if len(docID) == 0 {
return errors.New("docID cannot be empty. tag: " + this.w.Tag)
return errors.New("docID cannot be empty. tag: " + w.w.Tag)
}

var url = "mongodb://" + addr + "/" + coll
Expand All @@ -89,20 +89,20 @@ func (this *WUID) LoadH24FromMongoWithTimeout(addr, user, pass, dbName, coll, do
if err != nil {
return err
}
if err = this.w.VerifyH24(uint64(m["n"].(int))); err != nil {
if err = w.w.VerifyH24(uint64(m["n"].(int))); err != nil {
return err
}

this.w.Reset(uint64(m["n"].(int)) << 40)
w.w.Reset(uint64(m["n"].(int)) << 40)

this.w.Lock()
defer this.w.Unlock()
w.w.Lock()
defer w.w.Unlock()

if this.w.Renew != nil {
if w.w.Renew != nil {
return nil
}
this.w.Renew = func() error {
return this.LoadH24FromMongo(addr, user, pass, dbName, coll, docID)
w.w.Renew = func() error {
return w.LoadH24FromMongo(addr, user, pass, dbName, coll, docID)
}

return nil
Expand Down
28 changes: 14 additions & 14 deletions mysql/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ func NewWUID(tag string, logger Logger, opts ...Option) *WUID {
}

// Next returns the next unique number.
func (this *WUID) Next() uint64 {
return this.w.Next()
func (w *WUID) Next() uint64 {
return w.w.Next()
}

// LoadH24FromMysql adds 1 to a specific number in your MySQL, fetches the new value, and then
// sets it as the high 24 bits of the unique numbers that Next generates.
func (this *WUID) LoadH24FromMysql(addr, user, pass, dbName, table string) error {
func (w *WUID) LoadH24FromMysql(addr, user, pass, dbName, table string) error {
if len(addr) == 0 {
return errors.New("addr cannot be empty. tag: " + this.w.Tag)
return errors.New("addr cannot be empty. tag: " + w.w.Tag)
}
if len(user) == 0 {
return errors.New("user cannot be empty. tag: " + this.w.Tag)
return errors.New("user cannot be empty. tag: " + w.w.Tag)
}
if len(dbName) == 0 {
return errors.New("dbName cannot be empty. tag: " + this.w.Tag)
return errors.New("dbName cannot be empty. tag: " + w.w.Tag)
}
if len(table) == 0 {
return errors.New("table cannot be empty. tag: " + this.w.Tag)
return errors.New("table cannot be empty. tag: " + w.w.Tag)
}

var dsn string
Expand All @@ -81,20 +81,20 @@ func (this *WUID) LoadH24FromMysql(addr, user, pass, dbName, table string) error
if err != nil {
return err
}
if err = this.w.VerifyH24(uint64(lastInsertedID)); err != nil {
if err = w.w.VerifyH24(uint64(lastInsertedID)); err != nil {
return err
}

this.w.Reset(uint64(lastInsertedID) << 40)
w.w.Reset(uint64(lastInsertedID) << 40)

this.w.Lock()
defer this.w.Unlock()
w.w.Lock()
defer w.w.Unlock()

if this.w.Renew != nil {
if w.w.Renew != nil {
return nil
}
this.w.Renew = func() error {
return this.LoadH24FromMysql(addr, user, pass, dbName, table)
w.w.Renew = func() error {
return w.LoadH24FromMysql(addr, user, pass, dbName, table)
}

return nil
Expand Down
24 changes: 12 additions & 12 deletions redis/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ func NewWUID(tag string, logger Logger, opts ...Option) *WUID {
}

// Next returns the next unique number.
func (this *WUID) Next() uint64 {
return this.w.Next()
func (w *WUID) Next() uint64 {
return w.w.Next()
}

// LoadH24FromRedis adds 1 to a specific number in your Redis, fetches the new value, and then
// sets it as the high 24 bits of the unique numbers that Next generates.
func (this *WUID) LoadH24FromRedis(addr, pass, key string) error {
func (w *WUID) LoadH24FromRedis(addr, pass, key string) error {
if len(addr) == 0 {
return errors.New("addr cannot be empty. tag: " + this.w.Tag)
return errors.New("addr cannot be empty. tag: " + w.w.Tag)
}
if len(key) == 0 {
return errors.New("key cannot be empty. tag: " + this.w.Tag)
return errors.New("key cannot be empty. tag: " + w.w.Tag)
}

client := redis.NewClient(&redis.Options{
Expand All @@ -62,20 +62,20 @@ func (this *WUID) LoadH24FromRedis(addr, pass, key string) error {
if err != nil {
return err
}
if err = this.w.VerifyH24(uint64(h24)); err != nil {
if err = w.w.VerifyH24(uint64(h24)); err != nil {
return err
}

this.w.Reset(uint64(h24) << 40)
w.w.Reset(uint64(h24) << 40)

this.w.Lock()
defer this.w.Unlock()
w.w.Lock()
defer w.w.Unlock()

if this.w.Renew != nil {
if w.w.Renew != nil {
return nil
}
this.w.Renew = func() error {
return this.LoadH24FromRedis(addr, pass, key)
w.w.Renew = func() error {
return w.LoadH24FromRedis(addr, pass, key)
}

return nil
Expand Down

0 comments on commit ed4ec96

Please sign in to comment.