<commit_before>// An example of implementing an Fast AGI server in Go
//
// A request formed like the following:
// agi(agi://127.0.0.1/playback?file=foo)
// plays back file 'foo' to the user
//
// Copyright (C) 2013 - 2014, <NAME> <<EMAIL>>
// This program is free software, distributed under the terms of
// the BSD 3-Clause License. See the LICENSE file
// at the top of the source tree.

package main

import (
	"bufio"
	"flag"
	"github.com/zaf/agi"
	"log"
	"net"
	"net/url"
	"os"
	"os/signal"
	"runtime"
	"sync"
)

var (
	debug     = flag.Bool("debug", false, "Print debug information on stderr")
	listen    = flag.String("listen", "127.0.0.1", "Listening address")
	port      = flag.String("port", "4573", "Listening server port")
	listeners = flag.Int("pool", 4, "Pool size of Listeners")
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	shutdown := false

	addr := net.JoinHostPort(*listen, *port)
	log.Printf("Starting FastAGI server on %v\n", addr)
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatalln(err)
	}
	defer listener.Close()
	wg := new(sync.WaitGroup)
	for i := 0; i < *listeners; i++ {
		go func() {
			for !shutdown {
				conn, err := listener.Accept()
				if err != nil {
					log.Println(err)
					continue
				}
				if *debug {
					log.Printf("Connected: %v <-> %v\n", conn.LocalAddr(), conn.RemoteAddr())
				}
				wg.Add(1)
				go agiConnHandle(conn, wg)
			}
		}()
	}
	signal := <-c
	log.Printf("Received %v, Waiting for remaining sessions to end and exit.\n", signal)
	shutdown = true
	wg.Wait()
}

func agiConnHandle(client net.Conn, wg *sync.WaitGroup) {
	//Create a new AGI session
	rw := bufio.NewReadWriter(bufio.NewReader(client), bufio.NewWriter(client))
	myAgi, err := agi.Init(rw)
	defer func() {
		if *debug {
			log.Printf("Closing connection from %v", client.RemoteAddr())
		}<commit_msg>Fixed a race condition in fagi server example.<commit_after>// An example of implementing an Fast AGI server in Go
//
// A request formed like the following:
// agi(agi://127.0.0.1/playback?file=foo)
// plays back file 'foo' to the user
//
// Copyright (C) 2013 - 2014, <NAME> <<EMAIL>>
// This program is free software, distributed under the terms of
// the BSD 3-Clause License. See the LICENSE file
// at the top of the source tree.

package main

import (
	"bufio"
	"flag"
	"github.com/zaf/agi"
	"log"
	"net"
	"net/url"
	"os"
	"os/signal"
	"runtime"
	"sync"
	"sync/atomic"
)

var (
	debug     = flag.Bool("debug", false, "Print debug information on stderr")
	listen    = flag.String("listen", "127.0.0.1", "Listening address")
	port      = flag.String("port", "4573", "Listening server port")
	listeners = flag.Int("pool", 4, "Pool size of Listeners")
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	var shutdown int32

	addr := net.JoinHostPort(*listen, *port)
	log.Printf("Starting FastAGI server on %v\n", addr)
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatalln(err)
	}
	defer listener.Close()
	wg := new(sync.WaitGroup)
	for i := 0; i < *listeners; i++ {
		go func() {
			for atomic.LoadInt32(&shutdown) == 0 {
				conn, err := listener.Accept()
				if err != nil {
					log.Println(err)
					continue
				}
				if *debug {
					log.Printf("Connected: %v <-> %v\n", conn.LocalAddr(), conn.RemoteAddr())
				}
				wg.Add(1)
				go agiConnHandle(conn, wg)
			}
		}()
	}
	signal := <-c
	log.Printf("Received %v, Waiting for remaining sessions to end and exit.\n", signal)
	atomic.StoreInt32(&shutdown, 1)
	wg.Wait()
}

func agiConnHandle(client net.Conn, wg *sync.WaitGroup) {
	//Create a new AGI session
	rw := bufio.NewReadWriter(bufio.NewReader(client), bufio.NewWriter(client))
	myAgi, err := agi.Init(rw)
	defer func() {
		if *debug {
			log.Printf("Closing connection from %v", client.RemoteAddr())
		}
{'fraction_non_alphanumeric': 0.16219635627530365, 'fraction_numerical': 0.016700404858299597, 'mean_word_length': 3.3873473917869035, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1522004', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1455, 'n_words': 408}