mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	add workerpool to util
This commit is contained in:
		
							parent
							
								
									b9987b4fdc
								
							
						
					
					
						commit
						7a9ad0c8cc
					
				
					 1 changed files with 29 additions and 0 deletions
				
			
		
							
								
								
									
										29
									
								
								src/util/workerpool.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/util/workerpool.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,29 @@
 | 
			
		|||
package util
 | 
			
		||||
 | 
			
		||||
import "runtime"
 | 
			
		||||
 | 
			
		||||
var workerPool chan func()
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	maxProcs := runtime.GOMAXPROCS(0)
 | 
			
		||||
	if maxProcs < 1 {
 | 
			
		||||
		maxProcs = 1
 | 
			
		||||
	}
 | 
			
		||||
	workerPool = make(chan func(), maxProcs)
 | 
			
		||||
	for idx := 0; idx < maxProcs; idx++ {
 | 
			
		||||
		go func() {
 | 
			
		||||
			for f := range workerPool {
 | 
			
		||||
				f()
 | 
			
		||||
			}
 | 
			
		||||
		}()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// WorkerGo submits a job to a pool of GOMAXPROCS worker goroutines.
 | 
			
		||||
// This is meant for short non-blocking functions f() where you could just go f(),
 | 
			
		||||
// but you want some kind of backpressure to prevent spawning endless goroutines.
 | 
			
		||||
// WorkerGo returns as soon as the function is queued to run, not when it finishes.
 | 
			
		||||
// In Yggdrasil, these workers are used for certain cryptographic operations.
 | 
			
		||||
func WorkerGo(f func()) {
 | 
			
		||||
	workerPool <- f
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue