示例#1
0
文件: attrs.go 项目: aphistic/gomol
func getAttrHash(attr string) uint32 {
	// First try to acquire a read lock to see if we even need to hash
	// the string at all
	hashMutex.RLock()
	if hash, ok := attrHashes[attr]; ok {
		hashMutex.RUnlock()
		return hash
	}

	// We do need to hash it so release the read lock and acquire a write lock
	hashMutex.RUnlock()

	hashMutex.Lock()
	defer hashMutex.Unlock()
	if hash, ok := attrHashes[attr]; ok {
		return hash
	}

	hasher := murmur3.New32()
	hasher.Write([]byte(attr))

	hash := hasher.Sum32()
	hashAttrs[hash] = attr
	attrHashes[attr] = hash

	return hash
}
示例#2
0
文件: sender.go 项目: relops/rmq
func (s *client) send(ch *amqp.Channel, group uint64, o *Options, payload []byte) ([]byte, error) {

	id, err := s.flake.Next()
	if err != nil {
		return nil, err
	}

	groupString := fmt.Sprintf("%d", group)

	envelope := amqp.Publishing{
		MessageId:     fmt.Sprintf("%d", id),
		CorrelationId: groupString,
		Body:          payload,
		DeliveryMode:  amqp.Transient,
		Headers:       amqp.Table{timestampHeader: time.Now().UnixNano()},
	}

	if o.Persistent {
		envelope.DeliveryMode = amqp.Persistent
	}

	mandatory := false
	immediate := false

	if err := ch.Publish(o.Exchange, o.Key, mandatory, immediate, envelope); err != nil {
		return nil, fmt.Errorf("Could not publish to exchange %s: %s", o.Exchange, err)
	}

	h := murmur3.New32()
	h.Write(payload)
	sum := h.Sum(nil)

	size := float32(len(payload)) / 1024

	if len(o.Verbose) > 0 {
		log.Infof("[%d] sending %.2f kB (%x) to %s", id, size, sum, o.Key)
	} else {
		log.Infof("[%d] sending %.2f kB (%x)", id, size, sum)
	}

	return sum, nil
}
示例#3
0
func hashDigest(key string) HashKey {
	//m := md5.New()
	m := murmur3.New32()
	m.Write([]byte(key))
	return HashKey(m.Sum32())
}
示例#4
0
func doHash(in string) string {
	var h hash.Hash32 = murmur3.New32()
	h.Write([]byte(in))
	return hex.EncodeToString(h.Sum(nil))
}
示例#5
0
	DataExpires   int64  `json:"data_expires" msg:"data_expires"`
	Monitor       struct {
		TriggerLimits []float64 `json:"trigger_limits" msg:"trigger_limits"`
	} `json:"monitor" msg:"monitor"`
	EnableDetailedRecording bool        `json:"enable_detail_recording" msg:"enable_detail_recording"`
	MetaData                interface{} `json:"meta_data" msg:"meta_data"`
	Tags                    []string    `json:"tags" msg:"tags"`
	Alias                   string      `json:"alias" msg:"alias"`
	LastUpdated             string      `json:"last_updated" msg:"last_updated"`
	IdExtractorDeadline     int64       `json:"id_extractor_deadline" msg:"id_extractor_deadline"`
	SessionLifetime         int64       `bson:"session_lifetime" json:"session_lifetime"`

	firstSeenHash string
}

var murmurHasher = murmur3.New32()

func (s *SessionState) SetFirstSeenHash() {
	encoded, err := msgpack.Marshal(s)
	if err != nil {
		log.Error("Error encoding session data: ", err)
		return
	}

	s.firstSeenHash = fmt.Sprintf("%x", murmurHasher.Sum(encoded))
}

func (s *SessionState) GetHash() string {
	encoded, err := msgpack.Marshal(s)
	if err != nil {
		log.Error("Error encoding session data: ", err)
示例#6
0
文件: receiver.go 项目: relops/rmq
func handle(deliveries <-chan amqp.Delivery, opts *Options, tag string, signal chan error, cancelSubscription chan bool) {

	// TODO This could consume a lot of memory if not LRU'ed
	entropy := make(map[string]hash.Hash)

	for {
		select {
		case <-cancelSubscription:
			log.Infof("subscription cancelled by server")
			return
		default:
			for d := range deliveries {

				now := time.Now().UnixNano()

				if opts.Interval > 0 {
					time.Sleep(time.Duration(opts.Interval) * time.Millisecond)
				}

				if err := d.Ack(false); err != nil {
					signal <- err
				}

				h := murmur3.New32()
				h.Write(d.Body)
				sum := h.Sum(nil)

				e, ok := entropy[d.CorrelationId]
				if !ok {
					e = murmur3.New32()
				}

				e.Write(sum)
				ent := e.Sum(nil)
				entropy[d.CorrelationId] = e

				size := float32(len(d.Body)) / 1024

				var latency float64
				ts, hasLatency := d.Headers[timestampHeader]
				if hasLatency {
					then, ok := ts.(int64)
					if !ok {
						signal <- fmt.Errorf("Invalid nanos timestamp header: %+v", ts)
					}

					latency = float64((now - then)) / (1000 * 1000)
					if latency < 0 {
						log.Warnf("[%s] %s negative latency: %f (ms); sent at %d (nanos), received at %d (nanos)", tag, d.MessageId, latency, then, now)
					}
				}

				if opts.Entropy {
					label := shortLabel(d.CorrelationId)
					if hasLatency {
						log.Infof("[%s] %s receiving %.2f kB (%x) @ %.2f ms [%s, %x]", tag, d.MessageId, size, sum, latency, label, ent)
					} else {
						log.Infof("[%s] %s receiving %.2f kB (%x) [%s, %x]", tag, d.MessageId, size, sum, label, ent)
					}

				} else {
					if hasLatency {
						log.Infof("[%s] %s receiving %.2f kB (%x) @ %.2f ms", tag, d.MessageId, size, sum, latency)
					} else {
						log.Infof("[%s] %s receiving %.2f kB (%x)", tag, d.MessageId, size, sum)
					}

				}
			}
		}
	}

	log.Info("receiver exiting")
	signal <- nil
}
示例#7
0
文件: sender.go 项目: relops/rmq
func StartSender(s *client, signal chan error, opts *Options, wg *sync.WaitGroup) {

	ch, err := s.openChannel()
	if err != nil {
		signal <- err
		return
	}

	group, err := s.flake.Next()
	if err != nil {
		signal <- err
		return
	}

	h := murmur3.New32()

	if len(opts.Args.MessageBody) > 0 {

		m := make(map[string]string)
		for _, kv := range opts.Args.MessageBody {
			s := strings.SplitN(kv, "=", 2)
			if len(s) == 1 {
				m[s[0]] = ""
			} else {
				m[s[0]] = s[1]
			}
		}
		encoded, err := json.Marshal(m)
		if err != nil {
			signal <- err
			return
		}

		sum, err := s.send(ch, group, opts, encoded)
		if err != nil {
			signal <- err
			return
		}

		h.Write(sum)

	} else {

		r := rand.New(rand.NewSource(time.Now().UnixNano()))

		for i := 0; i < opts.Count; i++ {
			sizeInKb := opts.Size * 1024
			size := int(sizeInKb)
			if opts.StdDev > 0 {
				dev := float64(opts.StdDev)
				s := r.NormFloat64()*dev*100 + sizeInKb
				size = int(s)
			}

			if size == 0 {
				size++
			}

			buf := make([]byte, size)
			_, err = randbo.New().Read(buf)
			if err != nil {
				signal <- err
				return
			}

			sum, err := s.send(ch, group, opts, buf)
			if err != nil {
				signal <- err
				return
			}

			h.Write(sum)

			time.Sleep(time.Duration(opts.Interval) * time.Millisecond)
		}
	}

	if opts.Entropy {
		log.Infof("[%d] sender entropy (%x)", group, h.Sum(nil))
	}

	wg.Done()
	signal <- nil
}