Skip to content

Commit

Permalink
Add thread pool
Browse files Browse the repository at this point in the history
Signed-off-by: Joeky <joeky5888@gmail.com>
  • Loading branch information
joeky888 committed Mar 17, 2023
1 parent 0dfadc3 commit 47a90ee
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"time"
)

var poolGuard chan struct{}

const MaxGoroutine = 512

func init() {
poolGuard = make(chan struct{}, MaxGoroutine)
}

func handleTunneling(w http.ResponseWriter, r *http.Request) {
destConn, err := net.DialTimeout("tcp", r.Host, 10*time.Second)
if err != nil {
Expand Down Expand Up @@ -59,10 +67,20 @@ func copyHeader(dst, src http.Header) {
}
}

func acquireRequestPool() {
poolGuard <- struct{}{} // Would block if guard channel is full
}

func releaseRequestPool() {
<-poolGuard
}

func main() {
server := &http.Server{
Addr: ":13002",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
acquireRequestPool()
defer releaseRequestPool()
if r.Method == http.MethodConnect {
handleTunneling(w, r)
} else {
Expand Down

0 comments on commit 47a90ee

Please sign in to comment.