工厂功能在五郎



我有一个类似于下面所示的服务对象,它通过HTTP公开:

type ComputeService struct {
}
func (svc ComputeService) Compute(userType string, data Data) (Result, error) {
// rate limit based on userType (both check and increment counter)
// if not rate limited, compute and return result
}

现在,如您所见,需要根据userType进行速率限制。限速器本身的实现根据userType而变化。我正在考虑使用userType解决速率限制器的 2 种方法。

// First approach: for each request new ComputeService object is
// created after resolving RateLimiter using userType in the handler
// itself
type ComputeService struct {
RateLimiter RateLimiter
}
// Second approach: ComputeService object is initialized during startup
// and only RateLimiter is resolved with every Compute() call by calling
// the provider function
type ComputeService struct {
RateLimiterProvider func(userType string) RateLimiter
}

两者都是可测试的。哪一个更可取?我倾向于第二种方法,因为使用它意味着处理程序将是纯粹的读取请求,委托给服务,写出响应,而在第一种方法中,处理程序将包含解决速率限制器实现的额外步骤。

如果您使用的是像 Dargo 这样的 DI 系统,则可以使用其提供程序注入在运行时动态选择实现。

在这种情况下,您的服务将如下所示:

import "github.com/jwells131313/dargo/ioc"
type RateLimiter interface {
}
type UserType1RateLimiter struct {
}
type UserType2RateLimiter struct {
}
type ComputeService struct {
RateLimiterProvider ioc.Provider `inject:"RateLimiter"`
}
func (svc ComputeService) Compute(userType string, data Data) (Result, error) {
// rate limit based on userType (both check and increment counter)
// if not rate limited, compute and return result
raw, err := svc.RateLimiterProvider.QualifiedBy(userType).Get()
if err != nil {
return nil, err
}
limiter := raw.(RateLimiter)
//...
}  

以下是将这些绑定到服务定位器中的方式:

func initializer() {
serviceLocator, _ = ioc.CreateAndBind("ExampleLocator", func(binder ioc.Binder) error {
binder.Bind("RateLimiter", UserType1RateLimiter{}).QualifiedBy("UserType1")
binder.Bind("RateLimiter", UserType2RateLimiter{}).QualifiedBy("UserType2")
binder.Bind("ComputeService", ComputeService{})
return nil
})
}

这仅适用于您使用 Dargo 之类的东西,但它在您的情况下仍然可能有用。

如果您不使用 Dargo,在我看来这是一个意见问题,尽管我个人会选择第二种方法

最新更新