Exemplo n.º 1
0
// returns an operation filter which uses a consistent hash to determine
// if the operation will be accepted for processing. can be used to distribute work.
// name:	the name of the worker creating this filter. e.g. "Harry"
// workers:	a slice of strings representing the available worker names
func ConsistentHashFilter(name string, workers []string) (gtm.OpFilter, error) {
	if len(workers) == 0 {
		return nil, EmptyWorkers
	}
	found := false
	ring := hashring.New(workers)
	for _, worker := range workers {
		if worker == name {
			found = true
		}
	}
	if !found {
		return nil, WorkerMissing
	}
	return func(op *gtm.Op) bool {
		if op.Id != nil {
			var idStr string
			switch op.Id.(type) {
			case bson.ObjectId:
				idStr = op.Id.(bson.ObjectId).Hex()
			default:
				idStr = fmt.Sprintf("%v", op.Id)
			}
			who, ok := ring.GetNode(idStr)
			if ok {
				return name == who
			} else {
				return false
			}
		} else {
			return true
		}
	}, nil
}
Exemplo n.º 2
0
func NewTcpCli(bsCb BaseCliCb, enableHash bool, maxOutgo int, outGoBuf int, heartBeatSecond int) *TcpCli {
	tcp := &TcpCli{
		cb:         bsCb,
		enableHash: enableHash,
	}
	if tcp.enableHash {
		tcp.hashRing = hashring.New([]string{})
	}
	tcp.initData(maxOutgo, outGoBuf, heartBeatSecond, false)
	return tcp
}