@@ -13,6 +13,7 @@ import kotlinx.serialization.json.jsonObject
13
13
import kotlinx.serialization.json.jsonPrimitive
14
14
import java.io.File
15
15
import java.io.FileInputStream
16
+ import java.lang.ref.WeakReference
16
17
import java.util.concurrent.Executors
17
18
import java.util.concurrent.ScheduledExecutorService
18
19
import java.util.concurrent.TimeUnit
@@ -34,7 +35,7 @@ class SegmentDestination(
34
35
35
36
override val key: String = " Segment.io"
36
37
internal val httpClient: HTTPClient = HTTPClient (apiKey)
37
- internal lateinit var storage: Storage
38
+ internal lateinit var storage: WeakReference < Storage >
38
39
lateinit var flushScheduler: ScheduledExecutorService
39
40
internal val eventCount = AtomicInteger (0 )
40
41
@@ -73,7 +74,7 @@ class SegmentDestination(
73
74
val stringVal = Json .encodeToString(jsonVal)
74
75
analytics.log(" $key running $stringVal " )
75
76
try {
76
- storage.write(Storage .Constants .Events , stringVal)
77
+ storage.get()?. write(Storage .Constants .Events , stringVal)
77
78
if (eventCount.incrementAndGet() >= flushCount) {
78
79
flush()
79
80
}
@@ -84,7 +85,7 @@ class SegmentDestination(
84
85
85
86
override fun setup (analytics : Analytics ) {
86
87
super .setup(analytics)
87
- storage = analytics.storage
88
+ storage = WeakReference ( analytics.storage)
88
89
89
90
// register timer for flush interval
90
91
flushScheduler = Executors .newScheduledThreadPool(1 )
@@ -93,7 +94,7 @@ class SegmentDestination(
93
94
94
95
// If we have events in queue flush them
95
96
val eventFilePaths =
96
- parseFilePaths(storage.read(Storage .Constants .Events ))
97
+ parseFilePaths(storage.get()?. read(Storage .Constants .Events ))
97
98
if (eventFilePaths.isNotEmpty()) {
98
99
initialDelay = 0
99
100
}
@@ -127,7 +128,7 @@ class SegmentDestination(
127
128
return
128
129
}
129
130
analytics.log(" $key performing flush" )
130
- val fileUrls = parseFilePaths(storage.read(Storage .Constants .Events ))
131
+ val fileUrls = parseFilePaths(storage.get()?. read(Storage .Constants .Events ))
131
132
if (fileUrls.isEmpty()) {
132
133
analytics.log(" No events to upload" )
133
134
return
@@ -152,7 +153,7 @@ class SegmentDestination(
152
153
connection.close()
153
154
}
154
155
// Cleanup uploaded payloads
155
- storage.removeFile(fileUrl)
156
+ storage.get()?. removeFile(fileUrl)
156
157
analytics.log(" $key uploaded $fileUrl " )
157
158
} catch (e: HTTPException ) {
158
159
analytics.log(" $key exception while uploading, ${e.message} " )
@@ -162,7 +163,7 @@ class SegmentDestination(
162
163
message = " Payloads were rejected by server. Marked for removal." ,
163
164
type = LogType .ERROR
164
165
)
165
- storage.removeFile(fileUrl)
166
+ storage.get()?. removeFile(fileUrl)
166
167
} else {
167
168
analytics.log(
168
169
message = " Error while uploading payloads" ,
0 commit comments