如何在异步方法中管理互斥锁

本文关键字:管理 异步方法 | 更新日期: 2023-09-27 17:50:56

我已经将我的旧HttpHandler (.ashx) TwitterFeed代码移植到WebAPI应用程序。代码的核心使用了优秀的Linq2Twitter包(https://linqtotwitter.codeplex.com/)。移植的一部分涉及将该组件从版本2升级到版本3,版本3现在提供了许多异步方法调用——这对我来说是新的。下面是基本的控制器:

public async Task<IEnumerable<Status>> 
GetTweets(int count, bool includeRetweets, bool excludeReplies)
{
   var auth = new SingleUserAuthorizer
   {
      CredentialStore = new SingleUserInMemoryCredentialStore
      {
         ConsumerKey       = ConfigurationManager.AppSettings["twitterConsumerKey"],
         ConsumerSecret    = ConfigurationManager.AppSettings["twitterConsumerKeySecret"],
         AccessToken       = ConfigurationManager.AppSettings["twitterAccessToken"],
         AccessTokenSecret = ConfigurationManager.AppSettings["twitterAccessTokenSecret"]
      }
   };
   var ctx = new TwitterContext(auth);
   var tweets =
      await
      (from tweet in ctx.Status
         where (
            (tweet.Type == StatusType.Home)
            && (tweet.ExcludeReplies == excludeReplies)
            && (tweet.IncludeMyRetweet == includeRetweets)
            && (tweet.Count == count)
         )
      select tweet)
      .ToListAsync();
   return tweets;
}

这工作得很好,但是以前,我缓存了结果以避免"过度调用"Twitter API。在这里,我遇到了一个问题(更多的是由于我对异步协议缺乏理解,而不是我怀疑的其他任何事情)。

总的来说,我想做的是首先检查缓存,如果数据不存在,然后重新填充缓存并将数据返回给调用者(网页)。下面是我对代码

的尝试
public class TwitterController : ApiController {
   private const string CacheKey = "TwitterFeed";
   public async Task<IEnumerable<Status>>
   GetTweets(int count, bool includeRetweets, bool excludeReplies)
   {
      var context = System.Web.HttpContext.Current;
      var tweets = await GetTweetData(context, count, includeRetweets, excludeReplies);
      return tweets;
   }
   private async Task<IEnumerable<Status>>
   GetTweetData(HttpContext context, int count, bool includeRetweets, bool excludeReplies)
   {
      var cache = context.Cache;
      Mutex mutex = null;
      bool iOwnMutex = false;
      IEnumerable<Status> data = (IEnumerable<Status>)cache[CacheKey];
      // Start check to see if available on cache
      if (data == null)
      {
         try
         {
            // Lock base on resource key
            mutex = new Mutex(true, CacheKey);
            // Wait until it is safe to enter (someone else might already be
            // doing this), but also add 30 seconds max.
            iOwnMutex = mutex.WaitOne(30000);
            // Now let's see if some one else has added it...
            data = (IEnumerable<Status>)cache[CacheKey];
            // They did, so send it...
            if (data != null)
            {
               return data;
            }
            if (iOwnMutex)
            {
               // Still not there, so now is the time to look for it!
               data = await CallTwitterApi(count, includeRetweets, excludeReplies);
               cache.Remove(CacheKey);
               cache.Add(CacheKey, data, null, GetTwitterExpiryDate(),
                  TimeSpan.Zero, CacheItemPriority.Normal, null);
            }
         }
         finally
         {
            // Release the Mutex.
            if ((mutex != null) && (iOwnMutex))
            {
               // The following line throws the error:
               // Object synchronization method was called from an
               // unsynchronized block of code.
               mutex.ReleaseMutex();
            }
         }
      }
      return data;
   }
   private DateTime GetTwitterExpiryDate()
   {
      string szExpiry = ConfigurationManager.AppSettings["twitterCacheExpiry"];
      int expiry = Int32.Parse(szExpiry);
      return DateTime.Now.AddMinutes(expiry);
   }
   private async Task<IEnumerable<Status>>
   CallTwitterApi(int count, bool includeRetweets, bool excludeReplies)
   {
      var auth = new SingleUserAuthorizer
      {
         CredentialStore = new SingleUserInMemoryCredentialStore
         {
            ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
            ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerKeySecret"],
            AccessToken = ConfigurationManager.AppSettings["twitterAccessToken"],
            AccessTokenSecret = ConfigurationManager.AppSettings["twitterAccessTokenSecret"]
         }
      };
      var ctx = new TwitterContext(auth);
      var tweets =
         await
         (from tweet in ctx.Status
          where (
             (tweet.Type == StatusType.Home)
             && (tweet.ExcludeReplies == excludeReplies)
             && (tweet.IncludeMyRetweet == includeRetweets)
             && (tweet.Count == count)
             && (tweet.RetweetCount < 1)
          )
          select tweet)
         .ToListAsync();
      return tweets;
   }
}

问题发生在释放互斥锁的最后代码块中(尽管我担心GetTweetData()方法的整体模式和方法):

if ((mutex != null) && (iOwnMutex))
{
    // The following line throws the error:
    // Object synchronization method was called from an
    // unsynchronized block of code.
    mutex.ReleaseMutex();
}

如果我注释掉这行,代码工作正常,但是(我假设)我应该释放创建它的互斥锁。从我所发现的,这个问题与线程在创建和释放互斥锁之间的变化有关。

由于我缺乏异步编码的一般知识,我不确定a)我使用的模式是否可行,b)如果可行,我如何解决这个问题。

如何在异步方法中管理互斥锁

使用这样的互斥锁是行不通的。首先,Mutex是线程仿射的,所以它不能与async代码一起使用。

我注意到的其他问题:

  • Cache是线程安全的,所以它不应该需要互斥锁(或任何其他保护)。异步方法应该遵循基于任务的异步模式。
关于缓存有一个主要技巧:当您只有内存缓存时,缓存任务而不是结果数据。另一方面,我不得不怀疑HttpContext.Cache是否是最好的缓存使用,但我将保留它,因为你的问题更多的是关于异步代码如何改变缓存模式。

所以,我建议这样写:

private const string CacheKey = "TwitterFeed";
public Task<IEnumerable<Status>> GetTweetsAsync(int count, bool includeRetweets, bool excludeReplies)
{
  var context = System.Web.HttpContext.Current;
  return GetTweetDataAsync(context, count, includeRetweets, excludeReplies);
}
private Task<IEnumerable<Status>> GetTweetDataAsync(HttpContext context, int count, bool includeRetweets, bool excludeReplies)
{
  var cache = context.Cache;
  Task<IEnumerable<Status>> data = cache[CacheKey] as Task<IEnumerable<Status>>;
  if (data != null)
    return data;
  data = CallTwitterApiAsync(count, includeRetweets, excludeReplies);
  cache.Insert(CacheKey, data, null, GetTwitterExpiryDate(), TimeSpan.Zero);
  return data;
}
private async Task<IEnumerable<Status>> CallTwitterApiAsync(int count, bool includeRetweets, bool excludeReplies)
{
  ...
}

如果两个不同的请求(来自两个不同的会话)在同一时间请求相同的twitter feed,则该feed将被请求两次,这种可能性很小。但我不会因此而失眠。