Async Decorator Pattern

1122·2021년 6월 21일
0

Unity

목록 보기
1/2

목표


분석하기

  • As a Filter in ASP.NET Core, Middleware in node.js (Express) and React, WSGI in Python, and MagicOnion
  • 메서드는 비동기적으로 외부에서 내부로 호출
  • async/await를 활용하여 callback으로는 불가능한 디자인을 달성
  • UnityWebRequest 확장 기능
    1. Logging
    2. Mocking
    3. Timeout
    4. Processing request header before request
    5. Processing response header after request
    6. Exception handling based on status code
    7. UI handling after error (pop-ups, retries, scene transition)
  • 공통인터페이스
public interface IAsyncDecorator
{
    UniTask<ResponseContext> SendAsync(RequestContext context, CancellationToken cancellationToken, Func<RequestContext, CancellationToken, UniTask<ResponseContext>> next);
}
  • SetupHeaderDecorator
public class SetupHeaderDecorator : IAsyncDecorator
{
    public async UniTask<ResponseContext> SendAsync(RequestContext context, CancellationToken cancellationToken, Func<RequestContext, CancellationToken, UniTask<ResponseContext>> next)
    {
        //Request 호출 전
        context.RequestHeaders["x-app-timestamp"] = context.Timestamp.ToString();
        context.RequestHeaders["x-user-id"] = UserProfile.Id;
        context.RequestHeaders["x-access-token"] = UserProfile.Token;
 
        //Request 호출
        var respsonse = await next(context, cancellationToken); // call next decorator
 
        //Request 호출 후
        var nextToken = respsonse.ResponseHeaders["token"];
        UserProfile.Token = nextToken;
 
        return respsonse;
    }
}
  • LoggingDecorator - Request 전후 로그 출력, Exception 로그 출력

  • MockDecorator - 테스트 및 서버 측 구현이 준비 되기전 클라이언트 더미 패킷 테스트

  • AppendTokenDecorator - 토큰이 유효하지 않을 때 토큰을 얻고, 패킷 다시 전달

  • QueueRequestDecorator - 순차 처리 강제(Queue)

  • 사용하기

    // create decorated client(store to field)
    var client = new NetworkClient("http://localhost", TimeSpan.FromSeconds(10),
        new QueueRequestDecorator(),
        new LoggingDecorator(),
        new AppendTokenDecorator(),
        new SetupHeaderDecorator());
     
    // for example, call like this
    var result = await client.PostAsync("/User/Register", new { Id = 100 });

    QueueRequest -> Logging -> AppendToken -> SetupHeader -> NetworkClient -> SetupHeader -> AppendToken -> Logging -> QueueRequest

  • IAsyncDecorator, RequestContext, ResponseContext, NetworkClient

  • 핵심 프로세스 - IAsyncDecorator들의 호출

    UniTask<ResponseContext> InvokeRecursive(RequestContext context, CancellationToken cancellationToken)
    {
        context.decoratorIndex++;
        return decorators[context.decoratorIndex].SendAsync(context, cancellationToken, InvokeRecursive);
    }
  • ReturnToTitleDecorator


선수학습


정리

  • 비동기 코드의 전/후 코드 조립의 간편화 및 모듈화
profile
1122

0개의 댓글