- 在 sqrt_test.go 中添加了 fastSqr1 测试函数,用于测试事件驱动模型 - 新增了 Event 和 Uint32AsyncEvent 类型用于测试 - 更新了 go.work、go.mod 和
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package cart
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/badu/bus"
|
|
"github.com/badu/bus/test_scenarios/factory-request-reply/events"
|
|
"github.com/badu/bus/test_scenarios/factory-request-reply/inventory"
|
|
"github.com/badu/bus/test_scenarios/factory-request-reply/prices"
|
|
)
|
|
|
|
type ServiceImpl struct {
|
|
sb *strings.Builder
|
|
}
|
|
|
|
func NewService(sb *strings.Builder) ServiceImpl {
|
|
result := ServiceImpl{sb: sb}
|
|
return result
|
|
}
|
|
|
|
func (s *ServiceImpl) AddProductToCart(ctx context.Context, productID string) error {
|
|
inventoryClientRequest := events.NewInventoryGRPCClientRequestEvent()
|
|
bus.Pub(inventoryClientRequest)
|
|
inventoryClientRequest.WaitReply()
|
|
|
|
pricesClientRequest := events.NewPricesGRPCClientRequestEvent()
|
|
bus.Pub(pricesClientRequest)
|
|
pricesClientRequest.WaitReply()
|
|
|
|
defer inventoryClientRequest.Conn.Close() // close GRPC connection when done
|
|
stockResponse, err := inventoryClientRequest.Client.GetStockForProduct(ctx, &inventory.ProductIDRequest{ID: productID})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer pricesClientRequest.Conn.Close() // close GRPC connection when done
|
|
priceResponse, err := pricesClientRequest.Client.GetPricesForProduct(ctx, &prices.ProductIDRequest{ID: productID})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s.sb.WriteString(fmt.Sprintf("stock %0.2fpcs @ price %0.2f$\n", stockResponse.Stock, priceResponse.Price))
|
|
|
|
return nil
|
|
}
|