feat: implement context handling with timeout and goroutine management
This commit is contained in:
@@ -1,38 +1,26 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"context"
|
||||||
"sipro-mps/internal/ym/products"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// package main
|
|
||||||
//
|
|
||||||
// import (
|
|
||||||
//
|
|
||||||
// "context"
|
|
||||||
// "fmt"
|
|
||||||
// "sipro-mps/internal/wb/products/mapping/generated"
|
|
||||||
// "strings"
|
|
||||||
//
|
|
||||||
// pb "sipro-mps/api/generated/v1/wb/products"
|
|
||||||
// "sipro-mps/pkg/api/wb/client"
|
|
||||||
//
|
|
||||||
// "github.com/deliveryhero/pipeline/v2"
|
|
||||||
//
|
|
||||||
// )
|
|
||||||
//
|
|
||||||
// func main() {
|
|
||||||
//
|
|
||||||
// input := "1,2,3,4,5"
|
|
||||||
//
|
|
||||||
// for out := range pipeline.Process(context.Background(), apply, pipeline.Emit(input)) {
|
|
||||||
// for j := range out {
|
|
||||||
// fmt.Printf("process: %s\n", out[j])
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rs := products.GetCategoriesError("{\"status\":\"ERROR\",\"errors\":[{\"code\":\"BAD_REQUEST\",\"message\":\"Following categories are not leaf categories: 91735\"}]}")
|
ctx, cacnel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
fmt.Println(rs)
|
defer cacnel()
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
wg.Done()
|
||||||
|
println("goroutine exit")
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
println("context done")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
wg.Add(1)
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"sipro-mps/internal/redis"
|
"sipro-mps/internal/redis"
|
||||||
"sipro-mps/pkg/utils"
|
"sipro-mps/pkg/utils"
|
||||||
"time"
|
"time"
|
||||||
@@ -69,6 +70,9 @@ type RateLimitTransport struct {
|
|||||||
func (t *RateLimitTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
func (t *RateLimitTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
ctx := req.Context()
|
ctx := req.Context()
|
||||||
|
|
||||||
|
req.Header.Set("X-Client-Secret", os.Getenv("WB_SECRET_TOKEN"))
|
||||||
|
req.Header.Set("User-Agent", "wbas_seller.denco.store3547")
|
||||||
|
|
||||||
tokenString := req.Header.Get("Authorization")
|
tokenString := req.Header.Get("Authorization")
|
||||||
authData := utils.NewWbAuthData(tokenString)
|
authData := utils.NewWbAuthData(tokenString)
|
||||||
authDataBytes, err := json.Marshal(authData)
|
authDataBytes, err := json.Marshal(authData)
|
||||||
|
|||||||
Reference in New Issue
Block a user