github.com/igggame/nebulas-go@v2.1.0+incompatible/net/recved_message.go (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package net
    20  
    21  import (
    22  	"fmt"
    23  	"sync"
    24  
    25  	"github.com/nebulasio/go-nebulas/util/logging"
    26  	"github.com/sirupsen/logrus"
    27  	"github.com/willf/bloom"
    28  )
    29  
    30  const (
    31  	// according to https://krisives.github.io/bloom-calculator/
    32  	// Count (n) = 100000, Error (p) = 0.001
    33  	maxCountOfRecvMessageInBloomFiler = 1000000
    34  	bloomFilterOfRecvMessageArgM      = 14377588
    35  	bloomFilterOfRecvMessageArgK      = 10
    36  )
    37  
    38  var (
    39  	bloomFilterOfRecvMessage        = bloom.New(bloomFilterOfRecvMessageArgM, bloomFilterOfRecvMessageArgK)
    40  	bloomFilterMutex                sync.Mutex
    41  	countOfRecvMessageInBloomFilter = 0
    42  )
    43  
    44  // RecordKey add key to bloom filter.
    45  func RecordKey(key string) {
    46  	bloomFilterMutex.Lock()
    47  	defer bloomFilterMutex.Unlock()
    48  
    49  	countOfRecvMessageInBloomFilter++
    50  	if countOfRecvMessageInBloomFilter > maxCountOfRecvMessageInBloomFiler {
    51  		// reset.
    52  		logging.VLog().WithFields(logrus.Fields{
    53  			"countOfRecvMessageInBloomFilter": countOfRecvMessageInBloomFilter,
    54  		}).Debug("reset bloom filter.")
    55  		countOfRecvMessageInBloomFilter = 0
    56  		bloomFilterOfRecvMessage = bloom.New(bloomFilterOfRecvMessageArgM, bloomFilterOfRecvMessageArgK)
    57  	}
    58  
    59  	bloomFilterOfRecvMessage.AddString(key)
    60  }
    61  
    62  // HasKey use bloom filter to check if the key exists quickly
    63  func HasKey(key string) bool {
    64  	bloomFilterMutex.Lock()
    65  	defer bloomFilterMutex.Unlock()
    66  
    67  	return bloomFilterOfRecvMessage.TestString(key)
    68  }
    69  
    70  // RecordRecvMessage records received message
    71  func RecordRecvMessage(s *Stream, hash uint32) {
    72  	RecordKey(fmt.Sprintf("%s-%d", s.pid, hash))
    73  }
    74  
    75  // HasRecvMessage check if the received message exists before
    76  func HasRecvMessage(s *Stream, hash uint32) bool {
    77  	return HasKey(fmt.Sprintf("%s-%d", s.pid, hash))
    78  }