Skip to content

Commit

Permalink
rename: ego >> this
Browse files Browse the repository at this point in the history
  • Loading branch information
edwingeng committed Mar 28, 2018
1 parent d8c6c63 commit b3cd303
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 102 deletions.
38 changes: 19 additions & 19 deletions callback/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,55 +39,55 @@ func NewWUID(tag string, logger Logger, opts ...Option) *WUID {
}

// Next returns the next unique number.
func (ego *WUID) Next() uint64 {
return ego.w.Next()
func (this *WUID) Next() uint64 {
return this.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 (ego *WUID) LoadH24WithCallback(cb func() (uint64, error)) error {
func (this *WUID) LoadH24WithCallback(cb func() (uint64, error)) error {
if cb == nil {
return errors.New("cb cannot be nil. tag: " + ego.w.Tag)
return errors.New("cb cannot be nil. tag: " + this.w.Tag)
}

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

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

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

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

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

return nil
}

// RenewNow reacquires the high 24 bits from your data store immediately
func (ego *WUID) RenewNow() error {
return ego.w.RenewNow()
func (this *WUID) RenewNow() error {
return this.w.RenewNow()
}

// Option should never be used directly.
Expand Down
4 changes: 2 additions & 2 deletions callback/wuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

type simpleLogger struct{}

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

var sl = &simpleLogger{}

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

// Next is for internal use only.
func (ego *WUID) Next() uint64 {
x := atomic.AddUint64(&ego.N, 1)
func (this *WUID) Next() uint64 {
x := atomic.AddUint64(&this.N, 1)
if x&0xFFFFFFFFFF >= DangerLine {
panic(errors.New("<wuid> the low 40 bits are about to run out"))
}
if x&0xFFFFFFFFFF >= CriticalValue && x&RenewInterval == 0 {
go func() {
defer func() {
if r := recover(); r != nil {
ego.Logger.Warn(fmt.Sprintf("<wuid> panic, renew failed. tag: %s, reason: %+v", ego.Tag, r))
this.Logger.Warn(fmt.Sprintf("<wuid> panic, renew failed. tag: %s, reason: %+v", this.Tag, r))
}
}()

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

// RenewNow reacquires the high 24 bits from your data store immediately
func (ego *WUID) RenewNow() error {
ego.Lock()
renew := ego.Renew
ego.Unlock()
func (this *WUID) RenewNow() error {
this.Lock()
renew := this.Renew
this.Unlock()

return renew()
}

// Reset is for internal use only.
func (ego *WUID) Reset(n uint64) {
if ego.Section == 0 {
atomic.StoreUint64(&ego.N, n)
func (this *WUID) Reset(n uint64) {
if this.Section == 0 {
atomic.StoreUint64(&this.N, n)
} else {
atomic.StoreUint64(&ego.N, n&0x0FFFFFFFFFFFFFFF|uint64(ego.Section)<<60)
atomic.StoreUint64(&this.N, n&0x0FFFFFFFFFFFFFFF|uint64(this.Section)<<60)
}
}

// VerifyH24 is for internal use only.
func (ego *WUID) VerifyH24(h24 uint64) error {
func (this *WUID) VerifyH24(h24 uint64) error {
if h24 == 0 {
return errors.New("the h24 should not be 0. tag: " + ego.Tag)
return errors.New("the h24 should not be 0. tag: " + this.Tag)
}

if ego.Section == 0 {
if this.Section == 0 {
if h24 > 0xFFFFFF {
return errors.New("the h24 should not exceed 0xFFFFFF. tag: " + ego.Tag)
return errors.New("the h24 should not exceed 0xFFFFFF. tag: " + this.Tag)
}
} else {
if h24 > 0x0FFFFF {
return errors.New("the h20 should not exceed 0x0FFFFF. tag: " + ego.Tag)
return errors.New("the h20 should not exceed 0x0FFFFF. tag: " + this.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 (ego *simpleLogger) Info(args ...interface{}) {
func (this *simpleLogger) Info(args ...interface{}) {
str := "INFO\t"
str += fmt.Sprint(args...)
log.Println(str)
ego.numInfo++
this.numInfo++
}

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

func TestWUID_Next_Panic(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ go tool vet -all -shadow=true $dirs
errcheck github.com/edwingeng/wuid/... \
| ag -v '[ \t]*defer'

golint $dirs
golint $dirs | ag -v 'receiver name should be a reflection of its identity'
38 changes: 19 additions & 19 deletions mongo/wuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ func NewWUID(tag string, logger Logger, opts ...Option) *WUID {
}

// Next returns the next unique number.
func (ego *WUID) Next() uint64 {
return ego.w.Next()
func (this *WUID) Next() uint64 {
return this.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 (ego *WUID) LoadH24FromMongo(addr, user, pass, dbName, coll, docID string) error {
return ego.LoadH24FromMongoWithTimeout(addr, user, pass, dbName, coll, docID, 3*time.Second)
func (this *WUID) LoadH24FromMongo(addr, user, pass, dbName, coll, docID string) error {
return this.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 (ego *WUID) LoadH24FromMongoWithTimeout(addr, user, pass, dbName, coll, docID string, dialTimeout time.Duration) error {
func (this *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: " + ego.w.Tag)
return errors.New("addr cannot be empty. tag: " + this.w.Tag)
}
if len(dbName) == 0 {
return errors.New("dbName cannot be empty. tag: " + ego.w.Tag)
return errors.New("dbName cannot be empty. tag: " + this.w.Tag)
}
if len(coll) == 0 {
return errors.New("coll cannot be empty. tag: " + ego.w.Tag)
return errors.New("coll cannot be empty. tag: " + this.w.Tag)
}
if len(docID) == 0 {
return errors.New("docID cannot be empty. tag: " + ego.w.Tag)
return errors.New("docID cannot be empty. tag: " + this.w.Tag)
}

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

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

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

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

return nil
}

// RenewNow reacquires the high 24 bits from your data store immediately
func (ego *WUID) RenewNow() error {
return ego.w.RenewNow()
func (this *WUID) RenewNow() error {
return this.w.RenewNow()
}

// Option should never be used directly.
Expand Down
4 changes: 2 additions & 2 deletions mongo/wuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

type simpleLogger struct{}

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

var sl = &simpleLogger{}

Expand Down
34 changes: 17 additions & 17 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 (ego *WUID) Next() uint64 {
return ego.w.Next()
func (this *WUID) Next() uint64 {
return this.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 (ego *WUID) LoadH24FromMysql(addr, user, pass, dbName, table string) error {
func (this *WUID) LoadH24FromMysql(addr, user, pass, dbName, table string) error {
if len(addr) == 0 {
return errors.New("addr cannot be empty. tag: " + ego.w.Tag)
return errors.New("addr cannot be empty. tag: " + this.w.Tag)
}
if len(user) == 0 {
return errors.New("user cannot be empty. tag: " + ego.w.Tag)
return errors.New("user cannot be empty. tag: " + this.w.Tag)
}
if len(dbName) == 0 {
return errors.New("dbName cannot be empty. tag: " + ego.w.Tag)
return errors.New("dbName cannot be empty. tag: " + this.w.Tag)
}
if len(table) == 0 {
return errors.New("table cannot be empty. tag: " + ego.w.Tag)
return errors.New("table cannot be empty. tag: " + this.w.Tag)
}

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

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

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

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

return nil
}

// RenewNow reacquires the high 24 bits from your data store immediately
func (ego *WUID) RenewNow() error {
return ego.w.RenewNow()
func (this *WUID) RenewNow() error {
return this.w.RenewNow()
}

// Option should never be used directly.
Expand Down
4 changes: 2 additions & 2 deletions mysql/wuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

type simpleLogger struct{}

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

var sl = &simpleLogger{}

Expand Down
Loading

0 comments on commit b3cd303

Please sign in to comment.