parallelize the calls

This commit is contained in:
2023-05-31 01:27:22 -06:00
parent 2004ad0f44
commit 40f6ed5da8
2 changed files with 50 additions and 19 deletions

41
main.go
View File

@ -10,6 +10,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"time"
)
@ -93,18 +94,30 @@ func getCategories() ([]Transaction, error) {
return []Transaction{}, fmt.Errorf("error reading csv: %w", err)
}
waitGroup := sync.WaitGroup{}
transactionChan := make(chan Transaction)
var finalTransactions []Transaction
count := 0
for _, line := range transactions {
//if count > 100 {
// break
//}
fmt.Printf("Transaction Count: %v\n", len(transactions))
for _, transaction := range transactions {
if count != 0 && count%100 == 0 {
fmt.Printf("At %v waiting...\n", count)
time.Sleep(60 * time.Second)
fmt.Println("starting... next 100")
}
waitGroup.Add(1)
go func(line Transaction, wg *sync.WaitGroup, transactionChan chan Transaction, count int) {
defer wg.Done()
fmt.Println(line)
transaction, err := parseTransaction(line.TransactionName)
if err != nil {
// continue if parsing error
fmt.Println(err)
fmt.Println("done with error")
return
}
fmt.Println("done parsing")
//fmt.Printf("%+v\n", transaction)
combinedTransaction := Transaction{
Category: transaction.Category,
@ -113,9 +126,21 @@ func getCategories() ([]Transaction, error) {
TransactionName: line.TransactionName,
IsoDate: line.IsoDate,
}
finalTransactions = append(finalTransactions, combinedTransaction)
transactionChan <- combinedTransaction
fmt.Println("done end of function")
}(transaction, &waitGroup, transactionChan, count)
count++
}
// Start a goroutine to receive values from the transactionChan
go func() {
for combinedTransaction := range transactionChan {
finalTransactions = append(finalTransactions, combinedTransaction)
}
}()
fmt.Println(count)
fmt.Println("waiting...")
waitGroup.Wait()
close(transactionChan)
jsonTransactions, err := json.Marshal(finalTransactions)
if err != nil {
@ -201,6 +226,12 @@ Expected output format:
)
if err != nil {
// if 429, wait and try again
if strings.Contains(err.Error(), "429") {
fmt.Println("429 error, waiting 5 seconds...")
time.Sleep(5 * time.Second)
continue
}
return Transaction{}, fmt.Errorf("ChatCompletion request error: %w", err)
}
fmt.Println(resp.Choices[0].Message.Content)

File diff suppressed because one or more lines are too long