-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added process limit, so your pc doesn't turn into an aviation e…
…ngine
- Loading branch information
Showing
7 changed files
with
187 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package thumbnails | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"runtime" | ||
"strconv" | ||
"sync" | ||
) | ||
|
||
/* Controls the no of concurrent processes used to generate thumbnails */ | ||
type GuardProcs struct { | ||
g chan struct{} | ||
max int | ||
} | ||
|
||
func NewGuardProcs() GuardProcs { | ||
|
||
maxProcs := runtime.NumCPU() | ||
maxProcsEnv := os.Getenv("AVRP_MAX_PROCS") | ||
if maxProcsEnv != "" { | ||
mp, err := strconv.Atoi(maxProcsEnv) | ||
if err == nil { | ||
maxProcs = mp | ||
} | ||
} | ||
|
||
log.Printf("Using %v threads for thumbnail generation\n", maxProcs) | ||
|
||
return GuardProcs{ | ||
g: make(chan struct{}, maxProcs), | ||
max: maxProcs, | ||
} | ||
|
||
} | ||
|
||
func (p *GuardProcs) MaxProcs() int { | ||
return p.max | ||
} | ||
|
||
func (p *GuardProcs) In() { | ||
p.g <- struct{}{} | ||
} | ||
|
||
func (p *GuardProcs) Out() { | ||
<-p.g | ||
} | ||
|
||
type GuardFile struct { | ||
locks map[string]*sync.Mutex | ||
} | ||
|
||
func NewGuardFile() GuardFile { | ||
return GuardFile{ | ||
locks: make(map[string]*sync.Mutex), | ||
} | ||
} | ||
|
||
func (p *GuardFile) Lock(key string) { | ||
l, exists := p.locks[key] | ||
if exists { | ||
l.Lock() | ||
} else { | ||
p.locks[key] = &sync.Mutex{} | ||
p.locks[key].Lock() | ||
} | ||
} | ||
|
||
func (p *GuardFile) Unlock(key string) { | ||
l, exists := p.locks[key] | ||
if exists { | ||
l.Unlock() | ||
} else { | ||
log.Printf("ERR: %s not found in GuardFile\n", key) | ||
} | ||
} |
Oops, something went wrong.