1- using System . Collections . Immutable ;
1+ using System . Collections . Immutable ;
22using Chirper . Grains . Models ;
33using Microsoft . Extensions . Logging ;
44using Orleans . Concurrency ;
77namespace Chirper . Grains ;
88
99[ Reentrant ]
10- public sealed class ChirperAccount : Grain , IChirperAccount
10+ public sealed class ChirperAccount (
11+ [ PersistentState ( stateName : "account" , storageName : "AccountState" ) ] IPersistentState < ChirperAccountState > state ,
12+ ILogger < ChirperAccount > logger ) : Grain , IChirperAccount
1113{
14+ private static string GrainType => nameof ( ChirperAccount ) ;
15+
1216 /// <summary>
1317 /// Size for the recently received message cache.
1418 /// </summary>
@@ -29,28 +33,17 @@ public sealed class ChirperAccount : Grain, IChirperAccount
2933 /// This list is not part of state and will not survive grain deactivation.
3034 /// </summary>
3135 private readonly HashSet < IChirperViewer > _viewers = new ( ) ;
32- private readonly ILogger < ChirperAccount > _logger ;
33- private readonly IPersistentState < ChirperAccountState > _state ;
3436
3537 /// <summary>
3638 /// Allows state writing to happen in the background.
3739 /// </summary>
3840 private Task ? _outstandingWriteStateOperation ;
3941
40- public ChirperAccount (
41- [ PersistentState ( stateName : "account" , storageName : "AccountState" ) ] IPersistentState < ChirperAccountState > state ,
42- ILogger < ChirperAccount > logger )
43- {
44- _state = state ;
45- _logger = logger ;
46- }
47-
48- private static string GrainType => nameof ( ChirperAccount ) ;
4942 private string GrainKey => this . GetPrimaryKeyString ( ) ;
5043
5144 public override Task OnActivateAsync ( CancellationToken _ )
5245 {
53- _logger . LogInformation ( "{GrainType} {GrainKey} activated." , GrainType , GrainKey ) ;
46+ logger . LogInformation ( "{GrainType} {GrainKey} activated." , GrainType , GrainKey ) ;
5447
5548 return Task . CompletedTask ;
5649 }
@@ -59,49 +52,49 @@ public async ValueTask PublishMessageAsync(string message)
5952 {
6053 var chirp = CreateNewChirpMessage ( message ) ;
6154
62- _logger . LogInformation ( "{GrainType} {GrainKey} publishing new chirp message '{Chirp}'." ,
55+ logger . LogInformation ( "{GrainType} {GrainKey} publishing new chirp message '{Chirp}'." ,
6356 GrainType , GrainKey , chirp ) ;
6457
65- _state . State . MyPublishedMessages . Enqueue ( chirp ) ;
58+ state . State . MyPublishedMessages . Enqueue ( chirp ) ;
6659
67- while ( _state . State . MyPublishedMessages . Count > PublishedMessagesCacheSize )
60+ while ( state . State . MyPublishedMessages . Count > PublishedMessagesCacheSize )
6861 {
69- _state . State . MyPublishedMessages . Dequeue ( ) ;
62+ state . State . MyPublishedMessages . Dequeue ( ) ;
7063 }
7164
7265 await WriteStateAsync ( ) ;
7366
7467 // notify viewers of new message
75- _logger . LogInformation ( "{GrainType} {GrainKey} sending new chirp message to {ViewerCount} viewers." ,
68+ logger . LogInformation ( "{GrainType} {GrainKey} sending new chirp message to {ViewerCount} viewers." ,
7669 GrainType , GrainKey , _viewers . Count ) ;
7770
7871 _viewers . ForEach ( _ => _ . NewChirp ( chirp ) ) ;
7972
8073 // notify followers of a new message
81- _logger . LogInformation ( "{GrainType} {GrainKey} sending new chirp message to {FollowerCount} followers." ,
82- GrainType , GrainKey , _state . State . Followers . Count ) ;
74+ logger . LogInformation ( "{GrainType} {GrainKey} sending new chirp message to {FollowerCount} followers." ,
75+ GrainType , GrainKey , state . State . Followers . Count ) ;
8376
84- await Task . WhenAll ( _state . State . Followers . Values . Select ( _ => _ . NewChirpAsync ( chirp ) ) . ToArray ( ) ) ;
77+ await Task . WhenAll ( state . State . Followers . Values . Select ( _ => _ . NewChirpAsync ( chirp ) ) . ToArray ( ) ) ;
8578 }
8679
8780 public ValueTask < ImmutableList < ChirperMessage > > GetReceivedMessagesAsync ( int number , int start )
8881 {
8982 if ( start < 0 ) start = 0 ;
90- if ( start + number > _state . State . RecentReceivedMessages . Count )
83+ if ( start + number > state . State . RecentReceivedMessages . Count )
9184 {
92- number = _state . State . RecentReceivedMessages . Count - start ;
85+ number = state . State . RecentReceivedMessages . Count - start ;
9386 }
9487
9588 return ValueTask . FromResult (
96- _state . State . RecentReceivedMessages
89+ state . State . RecentReceivedMessages
9790 . Skip ( start )
9891 . Take ( number )
9992 . ToImmutableList ( ) ) ;
10093 }
10194
10295 public async ValueTask FollowUserIdAsync ( string username )
10396 {
104- _logger . LogInformation (
97+ logger . LogInformation (
10598 "{GrainType} {UserName} > FollowUserName({TargetUserName})." ,
10699 GrainType ,
107100 GrainKey ,
@@ -111,7 +104,7 @@ public async ValueTask FollowUserIdAsync(string username)
111104
112105 await userToFollow . AddFollowerAsync ( GrainKey , this . AsReference < IChirperSubscriber > ( ) ) ;
113106
114- _state . State . Subscriptions [ username ] = userToFollow ;
107+ state . State . Subscriptions [ username ] = userToFollow ;
115108
116109 await WriteStateAsync ( ) ;
117110
@@ -121,7 +114,7 @@ public async ValueTask FollowUserIdAsync(string username)
121114
122115 public async ValueTask UnfollowUserIdAsync ( string username )
123116 {
124- _logger . LogInformation (
117+ logger . LogInformation (
125118 "{GrainType} {GrainKey} > UnfollowUserName({TargetUserName})." ,
126119 GrainType ,
127120 GrainKey ,
@@ -132,7 +125,7 @@ await GrainFactory.GetGrain<IChirperPublisher>(username)
132125 . RemoveFollowerAsync ( GrainKey ) ;
133126
134127 // remove this publisher from the subscriptions list
135- _state . State . Subscriptions . Remove ( username ) ;
128+ state . State . Subscriptions . Remove ( username ) ;
136129
137130 // save now
138131 await WriteStateAsync ( ) ;
@@ -142,10 +135,10 @@ await GrainFactory.GetGrain<IChirperPublisher>(username)
142135 }
143136
144137 public ValueTask < ImmutableList < string > > GetFollowingListAsync ( ) =>
145- ValueTask . FromResult ( _state . State . Subscriptions . Keys . ToImmutableList ( ) ) ;
138+ ValueTask . FromResult ( state . State . Subscriptions . Keys . ToImmutableList ( ) ) ;
146139
147140 public ValueTask < ImmutableList < string > > GetFollowersListAsync ( ) =>
148- ValueTask . FromResult ( _state . State . Followers . Keys . ToImmutableList ( ) ) ;
141+ ValueTask . FromResult ( state . State . Followers . Keys . ToImmutableList ( ) ) ;
149142
150143 public ValueTask SubscribeAsync ( IChirperViewer viewer )
151144 {
@@ -162,50 +155,50 @@ public ValueTask UnsubscribeAsync(IChirperViewer viewer)
162155 public ValueTask < ImmutableList < ChirperMessage > > GetPublishedMessagesAsync ( int number , int start )
163156 {
164157 if ( start < 0 ) start = 0 ;
165- if ( start + number > _state . State . MyPublishedMessages . Count )
158+ if ( start + number > state . State . MyPublishedMessages . Count )
166159 {
167- number = _state . State . MyPublishedMessages . Count - start ;
160+ number = state . State . MyPublishedMessages . Count - start ;
168161 }
169162 return ValueTask . FromResult (
170- _state . State . MyPublishedMessages
163+ state . State . MyPublishedMessages
171164 . Skip ( start )
172165 . Take ( number )
173166 . ToImmutableList ( ) ) ;
174167 }
175168
176169 public async ValueTask AddFollowerAsync ( string username , IChirperSubscriber follower )
177170 {
178- _state . State . Followers [ username ] = follower ;
171+ state . State . Followers [ username ] = follower ;
179172 await WriteStateAsync ( ) ;
180173 _viewers . ForEach ( cv => cv . NewFollower ( username ) ) ;
181174 }
182175
183176 public ValueTask RemoveFollowerAsync ( string username )
184177 {
185- _state . State . Followers . Remove ( username ) ;
178+ state . State . Followers . Remove ( username ) ;
186179 return WriteStateAsync ( ) ;
187180 }
188181
189182 public async Task NewChirpAsync ( ChirperMessage chirp )
190183 {
191- _logger . LogInformation (
184+ logger . LogInformation (
192185 "{GrainType} {GrainKey} received chirp message = {Chirp}" ,
193186 GrainType ,
194187 GrainKey ,
195188 chirp ) ;
196189
197- _state . State . RecentReceivedMessages . Enqueue ( chirp ) ;
190+ state . State . RecentReceivedMessages . Enqueue ( chirp ) ;
198191
199192 // only relevant when not using fixed queue
200- while ( _state . State . RecentReceivedMessages . Count > ReceivedMessagesCacheSize ) // to keep not more than the max number of messages
193+ while ( state . State . RecentReceivedMessages . Count > ReceivedMessagesCacheSize ) // to keep not more than the max number of messages
201194 {
202- _state . State . RecentReceivedMessages . Dequeue ( ) ;
195+ state . State . RecentReceivedMessages . Dequeue ( ) ;
203196 }
204197
205198 await WriteStateAsync ( ) ;
206199
207200 // notify any viewers that a new chirp has been received
208- _logger . LogInformation (
201+ logger . LogInformation (
209202 "{GrainType} {GrainKey} sending received chirp message to {ViewerCount} viewers" ,
210203 GrainType ,
211204 GrainKey ,
@@ -246,7 +239,7 @@ private async ValueTask WriteStateAsync()
246239 if ( _outstandingWriteStateOperation is null )
247240 {
248241 // If after the initial write is completed, no other request initiated a new write operation, do it now.
249- currentWriteStateOperation = _state . WriteStateAsync ( ) ;
242+ currentWriteStateOperation = state . WriteStateAsync ( ) ;
250243 _outstandingWriteStateOperation = currentWriteStateOperation ;
251244 }
252245 else
0 commit comments