11package io .eigr .spawn .api .actors ;
22
3+ import com .github .benmanes .caffeine .cache .Cache ;
4+ import com .github .benmanes .caffeine .cache .Caffeine ;
35import com .google .protobuf .Any ;
46import com .google .protobuf .Empty ;
57import com .google .protobuf .GeneratedMessageV3 ;
1012import io .eigr .spawn .api .exceptions .ActorNotFoundException ;
1113import io .eigr .spawn .internal .client .SpawnClient ;
1214
15+ import java .time .Duration ;
16+ import java .util .Objects ;
1317import java .util .Optional ;
1418
1519public final class ActorRef {
20+ private static final int CACHE_MAXIMUM_SIZE = 1_000 ;
21+ private static final int CACHE_EXPIRE_AFTER_WRITE_SECONDS = 60 ;
22+ private static final Cache <ActorOuterClass .ActorId , ActorRef > ACTOR_REF_CACHE = Caffeine .newBuilder ()
23+ .maximumSize (CACHE_MAXIMUM_SIZE )
24+ .expireAfterWrite (Duration .ofSeconds (CACHE_EXPIRE_AFTER_WRITE_SECONDS ))
25+ .build ();
1626
1727 private final ActorOuterClass .ActorId actorId ;
1828
19- private final String name ;
20-
21- private final String system ;
22-
23- private final Optional <String > parent ;
24-
2529 private final SpawnClient client ;
2630
27- private ActorRef (SpawnClient client , String system , String name ) throws Exception {
31+ private ActorRef (ActorOuterClass . ActorId actorId , SpawnClient client ) {
2832 this .client = client ;
29- this .system = system ;
30- this .name = name ;
31- this .parent = Optional .empty ();
32- this .actorId = buildActorId ();
33- if (this .parent .isPresent ()){
34- spawnActor ();
35- }
36- }
37-
38- private ActorRef (SpawnClient client , String system , String name , String parent ) throws Exception {
39- this .client = client ;
40- this .system = system ;
41- this .name = name ;
42- this .parent = Optional .of (parent );
43- this .actorId = buildActorId ();
44- if (this .parent .isPresent ()){
45- spawnActor ();
46- }
33+ this .actorId = actorId ;
4734 }
4835
4936 public static ActorRef of (SpawnClient client , String system , String name ) throws Exception {
50- return new ActorRef (client , system , name );
37+ ActorOuterClass .ActorId actorId = buildActorId (system , name );
38+ ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
39+ if (Objects .nonNull (ref )){
40+ return ref ;
41+ }
42+
43+ ref = new ActorRef (actorId , client );
44+ ACTOR_REF_CACHE .put (actorId , ref );
45+ return ref ;
5146 }
5247
5348 public static ActorRef of (SpawnClient client , String system , String name , String parent ) throws Exception {
54- return new ActorRef (client , system , name , parent );
49+ ActorOuterClass .ActorId actorId = buildActorId (system , name , parent );
50+ ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
51+ if (Objects .nonNull (ref )){
52+ return ref ;
53+ }
54+
55+ spawnActor (actorId , client );
56+ ref = new ActorRef (actorId , client );
57+ ACTOR_REF_CACHE .put (actorId , ref );
58+ return ref ;
5559 }
5660
5761 public <T extends GeneratedMessageV3 > Optional <Object > invoke (String cmd , Class <T > outputType ) throws Exception {
@@ -121,42 +125,27 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeA
121125 }
122126
123127 public String getActorSystem () {
124- return this .system ;
128+ return this .actorId . getSystem () ;
125129 }
126130
127131 public String getActorName () {
128- return this .name ;
132+ return this .actorId . getName () ;
129133 }
130134
131135 public Optional <String > maybeActorParentName () {
132- return this .parent ;
136+ return Optional . ofNullable ( this .actorId . getParent ()) ;
133137 }
134138
135139 public String getActorParentName () {
136- return this .parent .get ();
137- }
138-
139- public boolean isUnnamedActor () {
140- return Optional .empty ().isPresent ();
140+ return this .actorId .getParent ();
141141 }
142142
143- private ActorOuterClass .ActorId buildActorId () {
144- ActorOuterClass .ActorId .Builder actorIdBuilder = ActorOuterClass .ActorId .newBuilder ()
145- .setSystem (this .system )
146- .setName (this .name );
147-
148- if (this .parent .isPresent ()) {
149- actorIdBuilder .setParent (this .parent .get ());
143+ public boolean isUnNamedActor () {
144+ if (Objects .nonNull (this .actorId .getParent ())) {
145+ return true ;
150146 }
151147
152- return actorIdBuilder .build ();
153- }
154-
155- private void spawnActor () throws Exception {
156- Protocol .SpawnRequest req = Protocol .SpawnRequest .newBuilder ()
157- .addActors (this .actorId )
158- .build ();
159- this .client .spawn (req );
148+ return false ;
160149 }
161150
162151 private <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <Object > invokeActor (
@@ -181,7 +170,7 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<Ob
181170 Any commandArg = Any .pack (argument );
182171
183172 invocationRequestBuilder
184- .setSystem (ActorOuterClass .ActorSystem .newBuilder ().setName (this .system ).build ())
173+ .setSystem (ActorOuterClass .ActorSystem .newBuilder ().setName (this .actorId . getSystem () ).build ())
185174 .setActor (actorRef )
186175 .setActionName (cmd )
187176 .setValue (commandArg )
@@ -194,7 +183,7 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<Ob
194183 case ERROR :
195184 case UNRECOGNIZED :
196185 throw new ActorInvokeException (
197- String .format ("Unknown error when trying to invoke Actor %s" , this .name ));
186+ String .format ("Unknown error when trying to invoke Actor %s" , this .getActorName () ));
198187 case ACTOR_NOT_FOUND :
199188 throw new ActorNotFoundException ();
200189 case OK :
@@ -206,4 +195,27 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<Ob
206195
207196 return Optional .empty ();
208197 }
198+
199+ private static ActorOuterClass .ActorId buildActorId (String system , String name ) {
200+ ActorOuterClass .ActorId .Builder actorIdBuilder = ActorOuterClass .ActorId .newBuilder ()
201+ .setSystem (system )
202+ .setName (name );
203+
204+ return actorIdBuilder .build ();
205+ }
206+
207+ private static ActorOuterClass .ActorId buildActorId (String system , String name , String parent ) {
208+ return ActorOuterClass .ActorId .newBuilder ()
209+ .setSystem (system )
210+ .setName (name )
211+ .setParent (parent )
212+ .build ();
213+ }
214+
215+ private static void spawnActor (ActorOuterClass .ActorId actorId , SpawnClient client ) throws Exception {
216+ Protocol .SpawnRequest req = Protocol .SpawnRequest .newBuilder ()
217+ .addActors (actorId )
218+ .build ();
219+ client .spawn (req );
220+ }
209221}
0 commit comments