forked from yuxh/gearman-go
		
	merge from paulmach
This commit is contained in:
		
						commit
						764dcf5f99
					
				@ -20,7 +20,7 @@ var (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
    IdGen = NewObjectId()
 | 
						IdGen = NewAutoIncId()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Status handler
 | 
					// Status handler
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										32
									
								
								client/id.go
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								client/id.go
									
									
									
									
									
								
							@ -1,35 +1,29 @@
 | 
				
			|||||||
package client
 | 
					package client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
    "strconv"
 | 
						"strconv"
 | 
				
			||||||
    "labix.org/v2/mgo/bson"
 | 
						"sync/atomic"
 | 
				
			||||||
    "github.com/mikespook/golib/autoinc"
 | 
						"time"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type IdGenerator interface {
 | 
					type IdGenerator interface {
 | 
				
			||||||
    Id() string
 | 
						Id() string
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ObjectId
 | 
					 | 
				
			||||||
type objectId struct {}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (id *objectId) Id() string {
 | 
					 | 
				
			||||||
    return bson.NewObjectId().Hex()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func NewObjectId() IdGenerator {
 | 
					 | 
				
			||||||
    return &objectId{}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AutoIncId
 | 
					// AutoIncId
 | 
				
			||||||
type autoincId struct {
 | 
					type autoincId struct {
 | 
				
			||||||
    *autoinc.AutoInc
 | 
						value int64
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (id *autoincId) Id() string {
 | 
					func (ai *autoincId) Id() string {
 | 
				
			||||||
    return strconv.Itoa(id.AutoInc.Id())
 | 
						next := atomic.AddInt64(&ai.value, 1)
 | 
				
			||||||
 | 
						return strconv.FormatInt(next, 10)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewAutoIncId() IdGenerator {
 | 
					func NewAutoIncId() IdGenerator {
 | 
				
			||||||
    return &autoincId{autoinc.New(1, 1)}
 | 
						// we'll consider the nano fraction of a second at startup unique
 | 
				
			||||||
 | 
						// and count up from there.
 | 
				
			||||||
 | 
						return &autoincId{
 | 
				
			||||||
 | 
							value: int64(time.Now().Nanosecond()) << 32,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										18
									
								
								client/id_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								client/id_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					package client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAutoInc(t *testing.T) {
 | 
				
			||||||
 | 
						ai := NewAutoIncId()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						previous := ai.Id()
 | 
				
			||||||
 | 
						for i := 0; i < 10; i++ {
 | 
				
			||||||
 | 
							id := ai.Id()
 | 
				
			||||||
 | 
							if id == previous {
 | 
				
			||||||
 | 
								t.Errorf("Id not unique, previous and current %s", id)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							previous = id
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user