github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/cmd/geph-bridge/e2enat_writer.go (about)

     1  package main
     2  
     3  import (
     4  	"runtime"
     5  	"sync"
     6  )
     7  
     8  var bufPool = &sync.Pool{
     9  	New: func() interface{} {
    10  		return make([]byte, 2048)
    11  	},
    12  }
    13  
    14  func malloc(n int) []byte {
    15  	return bufPool.Get().([]byte)[:n]
    16  }
    17  
    18  func free(bts []byte) {
    19  	bufPool.Put(bts[:2048])
    20  }
    21  
    22  // This gives us ample buffer space to deal with CPU spikes and avoid packet loss.
    23  var e2ejobs = make(chan func(), 1000)
    24  
    25  func maybeDoJob(f func()) {
    26  	select {
    27  	case e2ejobs <- f:
    28  	default:
    29  	}
    30  	//f()
    31  }
    32  
    33  func init() {
    34  	for i := 0; i < runtime.GOMAXPROCS(0); i++ {
    35  		//log.Println("spawning worker thread", workerID)
    36  		go func() {
    37  			for i := 0; ; i++ {
    38  				(<-e2ejobs)()
    39  			}
    40  		}()
    41  	}
    42  }