@@ -11,6 +11,7 @@ import (
1111 "math/rand"
1212 "net"
1313 "os"
14+ "sync"
1415 "sync/atomic"
1516 "syscall"
1617 "time"
@@ -371,12 +372,16 @@ type asnCounts struct {
371372}
372373
373374type connStats struct {
375+ m sync.RWMutex
374376 epochStart time.Time
375377 statCounts
376378 geoIPMap map [uint ]* asnCounts
377379}
378380
379381func (c * connStats ) PrintAndReset (logger * log.Logger ) {
382+ c .m .Lock () // protect both read for print and write for reset.
383+ defer c .m .Unlock ()
384+
380385 // prevent div by 0 if thread starvation happens
381386 var epochDur float64 = math .Max (float64 (time .Since (c .epochStart ).Milliseconds ()), 1 )
382387
@@ -443,10 +448,16 @@ func (c *connStats) PrintAndReset(logger *log.Logger) {
443448 )
444449 }
445450
446- c .Reset ()
451+ c .reset ()
447452}
448453
449454func (c * connStats ) Reset () {
455+ c .m .Lock ()
456+ defer c .m .Unlock ()
457+ c .reset ()
458+ }
459+
460+ func (c * connStats ) reset () {
450461 atomic .StoreInt64 (& c .numFound , 0 )
451462 atomic .StoreInt64 (& c .numErr , 0 )
452463 atomic .StoreInt64 (& c .numTimeout , 0 )
@@ -483,6 +494,8 @@ func (c *connStats) addCreated(asn uint, cc string) {
483494
484495 // GeoIP tracking
485496 if isValidCC (cc ) {
497+ c .m .Lock ()
498+ defer c .m .Unlock ()
486499 if _ , ok := c .geoIPMap [asn ]; ! ok {
487500 // We haven't seen asn before, so add it to the map
488501 c .geoIPMap [asn ] = & asnCounts {}
@@ -501,6 +514,8 @@ func (c *connStats) createdToDiscard(asn uint, cc string) {
501514
502515 // GeoIP tracking
503516 if isValidCC (cc ) {
517+ c .m .Lock ()
518+ defer c .m .Unlock ()
504519 if _ , ok := c .geoIPMap [asn ]; ! ok {
505520 // We haven't seen asn before, so add it to the map
506521 c .geoIPMap [asn ] = & asnCounts {}
@@ -522,6 +537,8 @@ func (c *connStats) createdToCheck(asn uint, cc string) {
522537
523538 // GeoIP tracking
524539 if isValidCC (cc ) {
540+ c .m .Lock ()
541+ defer c .m .Unlock ()
525542 if _ , ok := c .geoIPMap [asn ]; ! ok {
526543 // We haven't seen asn before, so add it to the map
527544 c .geoIPMap [asn ] = & asnCounts {}
@@ -543,6 +560,8 @@ func (c *connStats) createdToReset(asn uint, cc string) {
543560
544561 // GeoIP tracking
545562 if isValidCC (cc ) {
563+ c .m .Lock ()
564+ defer c .m .Unlock ()
546565 if _ , ok := c .geoIPMap [asn ]; ! ok {
547566 // We haven't seen asn before, so add it to the map
548567 c .geoIPMap [asn ] = & asnCounts {}
@@ -564,6 +583,8 @@ func (c *connStats) createdToTimeout(asn uint, cc string) {
564583
565584 // GeoIP tracking
566585 if isValidCC (cc ) {
586+ c .m .Lock ()
587+ defer c .m .Unlock ()
567588 if _ , ok := c .geoIPMap [asn ]; ! ok {
568589 // We haven't seen asn before, so add it to the map
569590 c .geoIPMap [asn ] = & asnCounts {}
@@ -585,6 +606,8 @@ func (c *connStats) createdToError(asn uint, cc string) {
585606
586607 // GeoIP tracking
587608 if isValidCC (cc ) {
609+ c .m .Lock ()
610+ defer c .m .Unlock ()
588611 if _ , ok := c .geoIPMap [asn ]; ! ok {
589612 // We haven't seen asn before, so add it to the map
590613 c .geoIPMap [asn ] = & asnCounts {}
@@ -606,6 +629,8 @@ func (c *connStats) readToCheck(asn uint, cc string) {
606629
607630 // GeoIP tracking
608631 if isValidCC (cc ) {
632+ c .m .Lock ()
633+ defer c .m .Unlock ()
609634 if _ , ok := c .geoIPMap [asn ]; ! ok {
610635 // We haven't seen asn before, so add it to the map
611636 c .geoIPMap [asn ] = & asnCounts {}
@@ -627,6 +652,8 @@ func (c *connStats) readToTimeout(asn uint, cc string) {
627652
628653 // GeoIP tracking
629654 if isValidCC (cc ) {
655+ c .m .Lock ()
656+ defer c .m .Unlock ()
630657 if _ , ok := c .geoIPMap [asn ]; ! ok {
631658 // We haven't seen asn before, so add it to the map
632659 c .geoIPMap [asn ] = & asnCounts {}
@@ -648,6 +675,8 @@ func (c *connStats) readToReset(asn uint, cc string) {
648675
649676 // GeoIP tracking
650677 if isValidCC (cc ) {
678+ c .m .Lock ()
679+ defer c .m .Unlock ()
651680 if _ , ok := c .geoIPMap [asn ]; ! ok {
652681 // We haven't seen asn before, so add it to the map
653682 c .geoIPMap [asn ] = & asnCounts {}
@@ -669,6 +698,8 @@ func (c *connStats) readToError(asn uint, cc string) {
669698
670699 // GeoIP tracking
671700 if isValidCC (cc ) {
701+ c .m .Lock ()
702+ defer c .m .Unlock ()
672703 if _ , ok := c .geoIPMap [asn ]; ! ok {
673704 // We haven't seen asn before, so add it to the map
674705 c .geoIPMap [asn ] = & asnCounts {}
@@ -690,6 +721,8 @@ func (c *connStats) checkToCreated(asn uint, cc string) {
690721
691722 // GeoIP tracking
692723 if isValidCC (cc ) {
724+ c .m .Lock ()
725+ defer c .m .Unlock ()
693726 if _ , ok := c .geoIPMap [asn ]; ! ok {
694727 // We haven't seen asn before, so add it to the map
695728 c .geoIPMap [asn ] = & asnCounts {}
@@ -711,6 +744,8 @@ func (c *connStats) checkToRead(asn uint, cc string) {
711744
712745 // GeoIP tracking
713746 if isValidCC (cc ) {
747+ c .m .Lock ()
748+ defer c .m .Unlock ()
714749 if _ , ok := c .geoIPMap [asn ]; ! ok {
715750 // We haven't seen asn before, so add it to the map
716751 c .geoIPMap [asn ] = & asnCounts {}
@@ -732,6 +767,8 @@ func (c *connStats) checkToFound(asn uint, cc string) {
732767
733768 // GeoIP tracking
734769 if isValidCC (cc ) {
770+ c .m .Lock ()
771+ defer c .m .Unlock ()
735772 if _ , ok := c .geoIPMap [asn ]; ! ok {
736773 // We haven't seen asn before, so add it to the map
737774 c .geoIPMap [asn ] = & asnCounts {}
@@ -753,6 +790,8 @@ func (c *connStats) checkToError(asn uint, cc string) {
753790
754791 // GeoIP tracking
755792 if isValidCC (cc ) {
793+ c .m .Lock ()
794+ defer c .m .Unlock ()
756795 if _ , ok := c .geoIPMap [asn ]; ! ok {
757796 // We haven't seen asn before, so add it to the map
758797 c .geoIPMap [asn ] = & asnCounts {}
@@ -774,6 +813,8 @@ func (c *connStats) checkToDiscard(asn uint, cc string) {
774813
775814 // GeoIP tracking
776815 if isValidCC (cc ) {
816+ c .m .Lock ()
817+ defer c .m .Unlock ()
777818 if _ , ok := c .geoIPMap [asn ]; ! ok {
778819 // We haven't seen asn before, so add it to the map
779820 c .geoIPMap [asn ] = & asnCounts {}
@@ -795,6 +836,8 @@ func (c *connStats) discardToReset(asn uint, cc string) {
795836
796837 // GeoIP tracking
797838 if isValidCC (cc ) {
839+ c .m .Lock ()
840+ defer c .m .Unlock ()
798841 if _ , ok := c .geoIPMap [asn ]; ! ok {
799842 // We haven't seen asn before, so add it to the map
800843 c .geoIPMap [asn ] = & asnCounts {}
@@ -816,6 +859,8 @@ func (c *connStats) discardToTimeout(asn uint, cc string) {
816859
817860 // GeoIP tracking
818861 if isValidCC (cc ) {
862+ c .m .Lock ()
863+ defer c .m .Unlock ()
819864 if _ , ok := c .geoIPMap [asn ]; ! ok {
820865 // We haven't seen asn before, so add it to the map
821866 c .geoIPMap [asn ] = & asnCounts {}
@@ -837,6 +882,8 @@ func (c *connStats) discardToError(asn uint, cc string) {
837882
838883 // GeoIP tracking
839884 if isValidCC (cc ) {
885+ c .m .Lock ()
886+ defer c .m .Unlock ()
840887 if _ , ok := c .geoIPMap [asn ]; ! ok {
841888 // We haven't seen asn before, so add it to the map
842889 c .geoIPMap [asn ] = & asnCounts {}
@@ -858,6 +905,8 @@ func (c *connStats) discardToClose(asn uint, cc string) {
858905
859906 // GeoIP tracking
860907 if isValidCC (cc ) {
908+ c .m .Lock ()
909+ defer c .m .Unlock ()
861910 if _ , ok := c .geoIPMap [asn ]; ! ok {
862911 // We haven't seen asn before, so add it to the map
863912 c .geoIPMap [asn ] = & asnCounts {}
0 commit comments