Class AsyncService

Nested Relationships

Nested Types

Class Documentation

class AsyncService

Effectively a threadpool, providing an API to take a translation request of a source-text, paramaterized by TranslationModel to be used for translation.

Configurability on optional items for the Response corresponding to a request is provisioned through ResponseOptions.

Public Functions

AsyncService(const AsyncService::Config &config)

Construct an AsyncService with configuration loaded from Options.

Expects positive integer value for cpu-threads. Additionally requires options which configure AggregateBatchingPool.

Ptr<TranslationModel> createCompatibleModel(const TranslationModel::Config &config)

Create a TranslationModel compatible with this instance of Service.

Internally assigns how many replicas of backend needed based on worker threads set. See TranslationModel for documentation on other params.

void translate(std::shared_ptr<TranslationModel> translationModel, std::string &&source, CallbackType callback, const ResponseOptions &options = ResponseOptions())

With the supplied TranslationModel, translate an input.

A Response is constructed with optional items set/unset indicated via ResponseOptions. Upon completion translation of the input, the client supplied callback is triggered with the constructed Response. Concurrent-calls to this function are safe.

Parameters
  • [in] translationModel: TranslationModel to use for the request.

  • [in] source: rvalue reference of the string to be translated. This is available as-is to the client later in the Response corresponding to this call along with the translated-text and meta-data.

  • [in] callback: A callback function provided by the client which accepts an rvalue of a Response.

  • [in] responseOptions: Options indicating whether or not to include some member in the Response, also specify any additional configurable parameters.

void pivot(std::shared_ptr<TranslationModel> first, std::shared_ptr<TranslationModel> second, std::string &&source, CallbackType clientCallback, const ResponseOptions &options = ResponseOptions())

With the supplied two translation models, translate using first and then the second generating a response as if it were translated from first’s source language to second’s target langauge.

Requires first’s target to be second’s source to work correctly - effectively implementing pivoting translation via an intermediate language.

Parameters
  • [in] first: TranslationModel capable of translating from source language to pivot language.

  • [in] second: TranslationModel capable of translating between pivot and target language.

  • [move]: source: The source text to be translated

  • [in] clientCallback: The callback to be called with the constructed Response. Expects the callback to consume the Response.

  • [in] options: Options indicating whether or not to include optional members in response and pass additional configurations. See ResponseOptions.

void clear()

Clears all pending requests.

~AsyncService()

Thread joins and proper shutdown are required to be handled explicitly.

If you do not want to wait, call clear() before destructor.

TranslationCache::Stats cacheStats()
struct Config

Public Members

size_t numWorkers = {1}

How many worker translation threads to spawn.

size_t cacheSize = {0}

Size in History items to be stored in the cache.

Loosely corresponds to sentences to cache in the real world. A value of 0 means no caching.

Logger::Config logger

Public Static Functions

template<class App>
static void addOptions(App &app, Config &config)