package Ġorg . t esser akt . disk ord in . gateway . shard Ċ Ċ import Ġkotlinx . coroutines . Coroutine Scope Ċ import Ġkotlinx . coroutines . flow . State Flow Ċ import Ġkotlinx . coroutines . launch Ċ import Ġmu . Kotlin Logging Ċ import Ġorg . t esser akt . disk ord in . core . client . Shard Context Ċ import Ġorg . t esser akt . disk ord in . gateway . Gateway Lifecycle Manager Ċ import Ġorg . t esser akt . disk ord in . gateway . json . commands . Ident ify Ċ import Ġorg . t esser akt . disk ord in . gateway . json . commands . Resume Ċ import Ġorg . t esser akt . disk ord in . gateway . send Payload Ċ import Ġorg . t esser akt . disk ord in . impl . gateway . interceptor . Connection Observer Ċ Ċ private Ġconst Ġval ĠINDEX _ OUT _ OF _ SH ARD _ COUNT Ġ= Ġ" Given Ġindex Ġof Ġshard Ġmore Ġthan Ġshard Ġcount " Ċ Ċ class ĠSh ard Controller Ġinternal Ġconstructor ( ĊĠĠĠ Ġprivate Ġval Ġcontext : ĠSh ard Context , ĊĠĠĠ Ġprivate Ġval Ġl ifec yc les : ĠList < Gateway Lifecycle Manager >, ĊĠĠĠ Ġprivate Ġval Ġgateway Scope : ĠCor outine Scope Ċ ) Ġ{ ĊĠĠĠ Ġprivate Ġval Ġlogger Ġ= ĠKotlin Logging . logger ("[ Gateway ]") ĊĊĠĠĠ Ġprivate Ġval Ġsh ards Ġ= Ġmutable ListOf < Shard >() ĊĠĠĠ Ġprivate Ġval Ġobserver Ġ= ĠConnection Observer ( this ) ĊĊĠĠĠ Ġprivate Ġval Ġconnection Properties Ġ= ĠIdent ify . Connection Properties ( ĊĠĠĠĠĠĠĠ ĠSystem . getProperty (" os . name "), ĊĠĠĠĠĠĠĠ Ġ" Disk ord in ", ĊĠĠĠĠĠĠĠ Ġ" Disk ord in " ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġprivate Ġfun Ġis Shard Compressed ( shard Index : ĠInt ) Ġ= Ġwhen Ġ( context . compression Strategy ) Ġ{ ĊĠĠĠĠĠĠĠ ĠCompression Strategy . Compress All Ġ-> Ġtrue ĊĠĠĠĠĠĠĠ Ġis ĠCompression Strategy . Compress Only Ġ-> Ġshard Index Ġin Ġcontext . compression Strategy . shard Indexes ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġis Shard Sub scribed ( shard Index : ĠInt ) Ġ= Ġwhen Ġ( context . guild Subscriptions Strategy ) Ġ{ ĊĠĠĠĠĠĠĠ ĠGuild Subscriptions Strategy . Subscribe To All Ġ-> Ġtrue ĊĠĠĠĠĠĠĠ Ġis ĠGuild Subscriptions Strategy . Subscribe To Ġ-> Ġshard Index Ġin Ġcontext . guild Subscriptions Strategy . shard Indexes ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġget Shard Threshold ( shard Index : ĠInt ) Ġ= Ġcontext . shard Threshold Overrides . overrides [ shard Index ] Ġ?: Ġ 5 0 ĊĊĠĠĠ Ġsuspend Ġfun Ġopen Shard ( shard Index : ĠInt , Ġsequence : ĠState Flow < Int ?> ) Ġ{ ĊĠĠĠĠĠĠĠ Ġrequire ( shard Index Ġ< Ġcontext . shard Count . value ()) Ġ{ ĠINDEX _ OUT _ OF _ SH ARD _ COUNT Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġidentify Ġ= ĠIdent ify ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcontext . token , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġconnection Properties , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis Shard Compressed ( shard Index ), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġget Shard Threshold ( shard Index ), ĊĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( shard Index , Ġcontext . shard Count . value ()), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcontext . initial Presence , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcontext . int ents , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġis Shard Sub scribed ( shard Index ) ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġl ifec yc les [ shard Index ]. connection . send Payload ( identify , Ġsequence . value ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsuspend Ġfun Ġapprove Shard ( shard : ĠSh ard , ĠsessionId : ĠString ) Ġ{ ĊĠĠĠĠĠĠĠ Ġshard ._ sessionId . value Ġ= ĠsessionId ĊĠĠĠĠĠĠĠ Ġsh ards Ġ+= Ġshard ĊĠĠĠĠĠĠĠ Ġgateway Scope . launch Ġ{ Ġobserver . observe ( shard ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsuspend Ġfun Ġclose Shard ( shard Index : ĠInt ) Ġ{ ĊĠĠĠĠĠĠĠ Ġrequire ( shard Index Ġ< Ġcontext . shard Count . value ()) Ġ{ ĠINDEX _ OUT _ OF _ SH ARD _ COUNT Ġ} ĊĠĠĠĠĠĠĠ Ġval Ġshard Ġ= Ġsh ards . find Ġ{ Ġit . shard Data . index Ġ== Ġshard Index Ġ} ĊĊĠĠĠĠĠĠĠ Ġif Ġ( shard Ġ== Ġnull Ġ|| Ġshard . state . value Ġ== ĠSh ard . State . Closing ) Ġreturn ĊĠĠĠĠĠĠĠ Ġshard ._ state . value Ġ= ĠSh ard . State . Closing ĊĊĠĠĠĠĠĠĠ Ġlogger . info (" Closing Ġshard Ġ#$ shard Index ") ĊĠĠĠĠĠĠĠ Ġl ifec yc les [ shard Index ]. stop ( 1 0 0 0 , Ġ" Normal Ġclosing ") ĊĠĠĠĠĠĠĠ Ġshard ._ state . value Ġ= ĠSh ard . State . Disconnected ĊĠĠĠĠĠĠĠ Ġsh ards . remove ( shard ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsuspend Ġfun Ġresume Shard ( shard : ĠSh ard ) Ġ{ ĊĠĠĠĠĠĠĠ Ġl ifec yc les [ shard . shard Data . index ]. start () ĊĠĠĠĠĠĠĠ Ġval Ġresume Ġ= ĠResume ( shard . token , Ġshard . sessionId . value !! , Ġshard . sequence . value ) ĊĠĠĠĠĠĠĠ Ġl ifec yc les [ shard . shard Data . index ]. connection . send Payload ( resume , Ġshard . sequence . value ) ĊĠĠĠ Ġ} Ċ }