@zio.dev/zio-blocks 0.0.51 → 0.0.55
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/adr/2026-07-18-data-migration.md +123 -0
- package/guides/async-getting-started.md +687 -0
- package/guides/compile-time-resource-safety-with-scope.md +6 -0
- package/guides/getting-started-with-mux.md +0 -112
- package/guides/query-dsl-extending.md +1 -1
- package/guides/query-dsl-fluent-builder.md +1 -1
- package/guides/query-dsl-reified-optics.md +1 -1
- package/guides/query-dsl-sql.md +395 -1
- package/guides/sql-checked-interpolation.md +173 -0
- package/guides/sql-transactions.md +286 -0
- package/guides/telemetry-guide.md +131 -70
- package/guides/zio-schema-migration.md +6 -6
- package/index.md +200 -583
- package/package.json +1 -1
- package/reference/async.md +1379 -531
- package/reference/chunk.md +3 -3
- package/reference/codegen/index.md +1 -1
- package/reference/combinators.md +4 -4
- package/reference/config/config-decoder.md +460 -0
- package/reference/config/config-source.md +489 -0
- package/reference/config/errors.md +278 -0
- package/reference/config/flags.md +369 -0
- package/reference/config/formats.md +314 -0
- package/reference/config/index.md +304 -0
- package/reference/config/rollout.md +336 -0
- package/reference/context.md +6 -49
- package/reference/data-migration.md +269 -0
- package/reference/datastar/attributes.md +302 -0
- package/reference/datastar/events.md +234 -0
- package/reference/datastar/index.md +256 -0
- package/reference/datastar/signals.md +230 -0
- package/reference/datastar/sse.md +295 -0
- package/reference/datastar.md +2 -2
- package/reference/docs.md +2 -2
- package/reference/endpoint/bulk-creation.md +96 -0
- package/reference/endpoint/index.md +9 -89
- package/reference/endpoint/path-codec.md +12 -24
- package/reference/endpoint/route-pattern.md +4 -6
- package/reference/endpoint/segment-codec.md +19 -32
- package/reference/html.md +313 -9
- package/reference/htmx/index.md +4 -52
- package/reference/htmx/response-headers.md +240 -0
- package/reference/http-model/headers.md +735 -0
- package/reference/http-model/index.md +3 -1
- package/reference/http-model/model.md +107 -71
- package/reference/http-model/schema-codecs.md +522 -0
- package/reference/http-model/schema.md +6 -3
- package/reference/http-model/server-sent-event.md +341 -0
- package/reference/jwt.md +195 -0
- package/reference/maybe.md +128 -11
- package/reference/media-type.md +2 -2
- package/reference/mux.md +254 -0
- package/reference/mux.mdx +7 -2
- package/reference/openapi.md +3 -3
- package/reference/projection.md +654 -0
- package/reference/resource-management/resource.md +2 -98
- package/reference/resource-management/scope.md +1 -209
- package/reference/resource-management/wire.md +4 -50
- package/reference/ringbuffer/advanced.mdx +1 -1
- package/reference/ringbuffer/index.mdx +3 -3
- package/reference/ringbuffer/mpmc.mdx +38 -4
- package/reference/ringbuffer/mpsc.mdx +36 -4
- package/reference/ringbuffer/spmc.mdx +1 -1
- package/reference/ringbuffer/spsc.mdx +87 -15
- package/reference/schema/allows.md +0 -96
- package/reference/schema/binding.md +2 -2
- package/reference/schema/built-in-codecs/avro.md +2 -2
- package/reference/schema/built-in-codecs/bson.md +50 -20
- package/reference/schema/built-in-codecs/csv.md +2 -2
- package/reference/schema/built-in-codecs/index.md +3 -3
- package/reference/schema/built-in-codecs/json/index.md +2 -2
- package/reference/schema/built-in-codecs/messagepack.md +3 -3
- package/reference/schema/built-in-codecs/thrift.md +2 -2
- package/reference/schema/built-in-codecs/toon.md +3 -3
- package/reference/schema/built-in-codecs/yaml.md +2 -2
- package/reference/schema/codec.md +11 -11
- package/reference/schema/dynamic-optic.md +48 -3
- package/reference/schema/dynamic-schema.md +3 -3
- package/reference/schema/index.md +2 -0
- package/reference/schema/path-interpolator.md +2 -0
- package/reference/schema/reflect-transformer.md +140 -0
- package/reference/schema/schema-evolution/as.md +4 -4
- package/reference/schema/schema-evolution/into.md +2 -2
- package/reference/schema/schema-expr.md +2 -2
- package/reference/schema/schema-search.md +263 -0
- package/reference/schema/schema.md +10 -2
- package/reference/schema/type-class-derivation.md +1 -1
- package/reference/smithy.md +502 -3
- package/reference/sql/db-codec-deriver.md +3 -3
- package/reference/sql/db-codec.md +22 -22
- package/reference/sql/db-con.md +4 -4
- package/reference/sql/db-connection.md +1 -1
- package/reference/sql/db-param.md +1 -1
- package/reference/sql/db-result-reader.md +4 -2
- package/reference/sql/db-tx.md +46 -14
- package/reference/sql/ddl.md +1 -1
- package/reference/sql/frag.md +44 -10
- package/reference/sql/index.md +7 -7
- package/reference/sql/repo.md +15 -15
- package/reference/sql/sql-dialect.md +1 -1
- package/reference/sql/sql-logger.md +1 -1
- package/reference/sql/sql-name-mapper.md +3 -3
- package/reference/sql/table-metadata.md +3 -3
- package/reference/sql/table.md +10 -10
- package/reference/sql/transactor-zio.md +1 -1
- package/reference/sql/transactor.md +21 -11
- package/reference/sql-zio.md +1 -1
- package/reference/streams/core/index.md +32 -0
- package/reference/streams/{pipeline.md → core/pipeline.md} +210 -74
- package/reference/streams/{sink.md → core/sink.md} +331 -353
- package/reference/streams/{stream.md → core/stream.md} +919 -209
- package/reference/streams/execution-and-compatibility/async-execution.md +822 -0
- package/reference/streams/execution-and-compatibility/index.md +35 -0
- package/reference/streams/execution-and-compatibility/platform-differences.md +297 -0
- package/reference/streams/execution-and-compatibility/scala-2-compatibility.md +88 -0
- package/reference/streams/execution-and-compatibility/zero-boxing.md +393 -0
- package/reference/streams/index.md +140 -67
- package/reference/streams/primitives/index.md +30 -0
- package/reference/streams/primitives/reader.md +1992 -0
- package/reference/streams/{writer.md → primitives/writer.md} +254 -98
- package/reference/telemetry/common/any-value.md +90 -0
- package/reference/telemetry/common/attribute-key.md +87 -0
- package/reference/telemetry/common/attributes.md +118 -0
- package/reference/telemetry/common/index.md +39 -0
- package/reference/telemetry/common/instrumentation-scope.md +24 -0
- package/reference/telemetry/common/resource.md +34 -0
- package/reference/telemetry/index.md +311 -0
- package/reference/telemetry/logging/index.md +197 -0
- package/reference/telemetry/logging/log-enrichment.md +72 -0
- package/reference/telemetry/logging/log-formatter.md +100 -0
- package/reference/telemetry/logging/log-record-processor.md +56 -0
- package/reference/telemetry/logging/log-record.md +44 -0
- package/reference/telemetry/logging/log-writer.md +64 -0
- package/reference/telemetry/logging/logger-provider.md +142 -0
- package/reference/telemetry/logging/logger.md +83 -0
- package/reference/telemetry/logging/severity.md +62 -0
- package/reference/telemetry/metrics/index.md +150 -0
- package/reference/telemetry/metrics/instruments.md +183 -0
- package/reference/telemetry/metrics/labeled-instruments.md +74 -0
- package/reference/telemetry/metrics/meter-provider.md +76 -0
- package/reference/telemetry/metrics/meter.md +98 -0
- package/reference/telemetry/metrics/metric-data.md +57 -0
- package/reference/telemetry/otel/custom-exporter.md +216 -0
- package/reference/telemetry/otel/index.md +212 -0
- package/reference/telemetry/tracing/index.md +155 -0
- package/reference/telemetry/tracing/sampler.md +89 -0
- package/reference/telemetry/tracing/span-builder.md +57 -0
- package/reference/telemetry/tracing/span-context.md +39 -0
- package/reference/telemetry/tracing/span-data.md +32 -0
- package/reference/telemetry/tracing/span-kind.md +55 -0
- package/reference/telemetry/tracing/span-processor.md +53 -0
- package/reference/telemetry/tracing/span-status.md +47 -0
- package/reference/telemetry/tracing/span.md +117 -0
- package/reference/telemetry/tracing/tracer-provider.md +91 -0
- package/reference/telemetry/tracing/tracer.md +52 -0
- package/reference/typeid.md +0 -64
- package/sidebars.js +150 -12
- package/undocumented-report.md +528 -270
- package/reference/config.md +0 -158
- package/reference/streams/concurrent-operators.md +0 -106
- package/reference/streams/reader.md +0 -1284
- package/reference/streams/scala-2-compatibility.md +0 -55
- package/reference/streams/zero-boxing.md +0 -275
- package/reference/telemetry.md +0 -693
|
@@ -36,13 +36,13 @@ Without Resources, managing complex initialization and cleanup is tedious and er
|
|
|
36
36
|
Add the ZIO Blocks Scope module to your `build.sbt`:
|
|
37
37
|
|
|
38
38
|
```scala
|
|
39
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-scope" % "0.0.
|
|
39
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-scope" % "0.0.55"
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
For cross-platform (Scala.js):
|
|
43
43
|
|
|
44
44
|
```scala
|
|
45
|
-
libraryDependencies += "dev.zio" %%% "zio-blocks-scope" % "0.0.
|
|
45
|
+
libraryDependencies += "dev.zio" %%% "zio-blocks-scope" % "0.0.55"
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
Supported Scala versions: 2.13.x and 3.x.
|
|
@@ -730,22 +730,6 @@ cd zio-blocks
|
|
|
730
730
|
This example demonstrates creating and automatically cleaning up temporary files using Resource's lifecycle management. It shows how Resource ensures files are closed even if exceptions occur:
|
|
731
731
|
|
|
732
732
|
```scala title="scope-examples/src/main/scala/scope/examples/TempFileHandlingExample.scala"
|
|
733
|
-
/*
|
|
734
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
735
|
-
*
|
|
736
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
737
|
-
* you may not use this file except in compliance with the License.
|
|
738
|
-
* You may obtain a copy of the License at
|
|
739
|
-
*
|
|
740
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
741
|
-
*
|
|
742
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
743
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
744
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
745
|
-
* See the License for the specific language governing permissions and
|
|
746
|
-
* limitations under the License.
|
|
747
|
-
*/
|
|
748
|
-
|
|
749
733
|
package scope.examples
|
|
750
734
|
|
|
751
735
|
import zio.blocks.scope._
|
|
@@ -843,22 +827,6 @@ sbt "scope-examples/runMain scope.examples.tempFileHandlingExample"
|
|
|
843
827
|
This example demonstrates the acquire-release pattern using Resource to manage database connections. It shows proper connection initialization and guaranteed cleanup:
|
|
844
828
|
|
|
845
829
|
```scala title="scope-examples/src/main/scala/scope/examples/DatabaseConnectionExample.scala"
|
|
846
|
-
/*
|
|
847
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
848
|
-
*
|
|
849
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
850
|
-
* you may not use this file except in compliance with the License.
|
|
851
|
-
* You may obtain a copy of the License at
|
|
852
|
-
*
|
|
853
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
854
|
-
*
|
|
855
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
856
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
857
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
858
|
-
* See the License for the specific language governing permissions and
|
|
859
|
-
* limitations under the License.
|
|
860
|
-
*/
|
|
861
|
-
|
|
862
830
|
package scope.examples
|
|
863
831
|
|
|
864
832
|
import zio.blocks.scope._
|
|
@@ -996,22 +964,6 @@ sbt "scope-examples/runMain scope.examples.runDatabaseExample"
|
|
|
996
964
|
This example demonstrates Resource.shared to create a singleton logger instance that is automatically cleaned up only when the last service releases it. Shows reference counting in action:
|
|
997
965
|
|
|
998
966
|
```scala title="scope-examples/src/main/scala/scope/examples/CachingSharedLoggerExample.scala"
|
|
999
|
-
/*
|
|
1000
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1001
|
-
*
|
|
1002
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1003
|
-
* you may not use this file except in compliance with the License.
|
|
1004
|
-
* You may obtain a copy of the License at
|
|
1005
|
-
*
|
|
1006
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1007
|
-
*
|
|
1008
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1009
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1010
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1011
|
-
* See the License for the specific language governing permissions and
|
|
1012
|
-
* limitations under the License.
|
|
1013
|
-
*/
|
|
1014
|
-
|
|
1015
967
|
package scope.examples
|
|
1016
968
|
|
|
1017
969
|
import zio.blocks.scope._
|
|
@@ -1163,22 +1115,6 @@ sbt "scope-examples/runMain scope.examples.runCachingExample"
|
|
|
1163
1115
|
This example demonstrates using Resource.shared for a database connection pool—an expensive resource that should exist exactly once. Shows how multiple services safely share the same pool instance with automatic cleanup:
|
|
1164
1116
|
|
|
1165
1117
|
```scala title="scope-examples/src/main/scala/scope/examples/ConnectionPoolExample.scala"
|
|
1166
|
-
/*
|
|
1167
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1168
|
-
*
|
|
1169
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1170
|
-
* you may not use this file except in compliance with the License.
|
|
1171
|
-
* You may obtain a copy of the License at
|
|
1172
|
-
*
|
|
1173
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1174
|
-
*
|
|
1175
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1176
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1177
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1178
|
-
* See the License for the specific language governing permissions and
|
|
1179
|
-
* limitations under the License.
|
|
1180
|
-
*/
|
|
1181
|
-
|
|
1182
1118
|
package scope.examples
|
|
1183
1119
|
|
|
1184
1120
|
import zio.blocks.scope._
|
|
@@ -1336,22 +1272,6 @@ sbt "scope-examples/runMain scope.examples.connectionPoolExample"
|
|
|
1336
1272
|
This example demonstrates combining Resource with transaction boundaries. Shows how to manage resources (connections, transactions) that must be coordinated across scopes with proper rollback on failure:
|
|
1337
1273
|
|
|
1338
1274
|
```scala title="scope-examples/src/main/scala/scope/examples/TransactionBoundaryExample.scala"
|
|
1339
|
-
/*
|
|
1340
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1341
|
-
*
|
|
1342
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1343
|
-
* you may not use this file except in compliance with the License.
|
|
1344
|
-
* You may obtain a copy of the License at
|
|
1345
|
-
*
|
|
1346
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1347
|
-
*
|
|
1348
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1349
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1350
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1351
|
-
* See the License for the specific language governing permissions and
|
|
1352
|
-
* limitations under the License.
|
|
1353
|
-
*/
|
|
1354
|
-
|
|
1355
1275
|
package scope.examples
|
|
1356
1276
|
|
|
1357
1277
|
import zio.blocks.scope._
|
|
@@ -1504,22 +1424,6 @@ sbt "scope-examples/runMain scope.examples.runTransactionBoundaryExample"
|
|
|
1504
1424
|
This example demonstrates Resource.from macro to automatically build a complex dependency graph with multiple services. Shows automatic wiring of constructor dependencies and cleanup in correct LIFO order:
|
|
1505
1425
|
|
|
1506
1426
|
```scala title="scope-examples/src/main/scala/scope/examples/LayeredWebServiceExample.scala"
|
|
1507
|
-
/*
|
|
1508
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1509
|
-
*
|
|
1510
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1511
|
-
* you may not use this file except in compliance with the License.
|
|
1512
|
-
* You may obtain a copy of the License at
|
|
1513
|
-
*
|
|
1514
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1515
|
-
*
|
|
1516
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1517
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1518
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1519
|
-
* See the License for the specific language governing permissions and
|
|
1520
|
-
* limitations under the License.
|
|
1521
|
-
*/
|
|
1522
|
-
|
|
1523
1427
|
package scope.examples
|
|
1524
1428
|
|
|
1525
1429
|
import zio.blocks.scope._
|
|
@@ -55,7 +55,7 @@ If you've used `try/finally`, `Using`, or ZIO's `Scope`, this is the same proble
|
|
|
55
55
|
Add the following dependency to your `build.sbt`:
|
|
56
56
|
|
|
57
57
|
```scala
|
|
58
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-scope" % "0.0.
|
|
58
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-scope" % "0.0.55"
|
|
59
59
|
```
|
|
60
60
|
|
|
61
61
|
Supported Scala versions: **2.13.x** and **3.x**.
|
|
@@ -1096,22 +1096,6 @@ cd zio-blocks
|
|
|
1096
1096
|
This example demonstrates how to allocate a database connection within a scope, ensure proper cleanup, and handle the connection's lifecycle safely.
|
|
1097
1097
|
|
|
1098
1098
|
```scala title="scope-examples/src/main/scala/scope/examples/DatabaseConnectionExample.scala"
|
|
1099
|
-
/*
|
|
1100
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1101
|
-
*
|
|
1102
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1103
|
-
* you may not use this file except in compliance with the License.
|
|
1104
|
-
* You may obtain a copy of the License at
|
|
1105
|
-
*
|
|
1106
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1107
|
-
*
|
|
1108
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1109
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1110
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1111
|
-
* See the License for the specific language governing permissions and
|
|
1112
|
-
* limitations under the License.
|
|
1113
|
-
*/
|
|
1114
|
-
|
|
1115
1099
|
package scope.examples
|
|
1116
1100
|
|
|
1117
1101
|
import zio.blocks.scope._
|
|
@@ -1247,22 +1231,6 @@ sbt "scope-examples/runMain runDatabaseExample"
|
|
|
1247
1231
|
This example demonstrates allocating multiple connections from a pool within the same scope and ensuring all are cleaned up correctly.
|
|
1248
1232
|
|
|
1249
1233
|
```scala title="scope-examples/src/main/scala/scope/examples/ConnectionPoolExample.scala"
|
|
1250
|
-
/*
|
|
1251
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1252
|
-
*
|
|
1253
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1254
|
-
* you may not use this file except in compliance with the License.
|
|
1255
|
-
* You may obtain a copy of the License at
|
|
1256
|
-
*
|
|
1257
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1258
|
-
*
|
|
1259
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1260
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1261
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1262
|
-
* See the License for the specific language governing permissions and
|
|
1263
|
-
* limitations under the License.
|
|
1264
|
-
*/
|
|
1265
|
-
|
|
1266
1234
|
package scope.examples
|
|
1267
1235
|
|
|
1268
1236
|
import zio.blocks.scope._
|
|
@@ -1418,22 +1386,6 @@ sbt "scope-examples/runMain scope.examples.connectionPoolExample"
|
|
|
1418
1386
|
This example shows how to allocate temporary file resources and ensure they are automatically cleaned up when the scope closes, even if errors occur.
|
|
1419
1387
|
|
|
1420
1388
|
```scala title="scope-examples/src/main/scala/scope/examples/TempFileHandlingExample.scala"
|
|
1421
|
-
/*
|
|
1422
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1423
|
-
*
|
|
1424
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1425
|
-
* you may not use this file except in compliance with the License.
|
|
1426
|
-
* You may obtain a copy of the License at
|
|
1427
|
-
*
|
|
1428
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1429
|
-
*
|
|
1430
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1431
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1432
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1433
|
-
* See the License for the specific language governing permissions and
|
|
1434
|
-
* limitations under the License.
|
|
1435
|
-
*/
|
|
1436
|
-
|
|
1437
1389
|
package scope.examples
|
|
1438
1390
|
|
|
1439
1391
|
import zio.blocks.scope._
|
|
@@ -1529,22 +1481,6 @@ sbt "scope-examples/runMain scope.examples.tempFileHandlingExample"
|
|
|
1529
1481
|
This example demonstrates managing database transactions within a scope, showing how to handle commit and rollback operations correctly.
|
|
1530
1482
|
|
|
1531
1483
|
```scala title="scope-examples/src/main/scala/scope/examples/TransactionBoundaryExample.scala"
|
|
1532
|
-
/*
|
|
1533
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1534
|
-
*
|
|
1535
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1536
|
-
* you may not use this file except in compliance with the License.
|
|
1537
|
-
* You may obtain a copy of the License at
|
|
1538
|
-
*
|
|
1539
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1540
|
-
*
|
|
1541
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1542
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1543
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1544
|
-
* See the License for the specific language governing permissions and
|
|
1545
|
-
* limitations under the License.
|
|
1546
|
-
*/
|
|
1547
|
-
|
|
1548
1484
|
package scope.examples
|
|
1549
1485
|
|
|
1550
1486
|
import zio.blocks.scope._
|
|
@@ -1695,22 +1631,6 @@ sbt "scope-examples/runMain scope.examples.runTransactionBoundaryExample"
|
|
|
1695
1631
|
This example shows how to build an HTTP client pipeline with interceptors for logging, authentication, and error handling, all managed within a scope.
|
|
1696
1632
|
|
|
1697
1633
|
```scala title="scope-examples/src/main/scala/scope/examples/HttpClientPipelineExample.scala"
|
|
1698
|
-
/*
|
|
1699
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1700
|
-
*
|
|
1701
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1702
|
-
* you may not use this file except in compliance with the License.
|
|
1703
|
-
* You may obtain a copy of the License at
|
|
1704
|
-
*
|
|
1705
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1706
|
-
*
|
|
1707
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1708
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1709
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1710
|
-
* See the License for the specific language governing permissions and
|
|
1711
|
-
* limitations under the License.
|
|
1712
|
-
*/
|
|
1713
|
-
|
|
1714
1634
|
package scope.examples
|
|
1715
1635
|
|
|
1716
1636
|
import zio.blocks.scope._
|
|
@@ -1834,22 +1754,6 @@ sbt "scope-examples/runMain scope.examples.httpClientPipelineExample"
|
|
|
1834
1754
|
This example demonstrates allocating a logger once at the top level and sharing it across multiple services, ensuring it is properly closed when the application shuts down.
|
|
1835
1755
|
|
|
1836
1756
|
```scala title="scope-examples/src/main/scala/scope/examples/CachingSharedLoggerExample.scala"
|
|
1837
|
-
/*
|
|
1838
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
1839
|
-
*
|
|
1840
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
1841
|
-
* you may not use this file except in compliance with the License.
|
|
1842
|
-
* You may obtain a copy of the License at
|
|
1843
|
-
*
|
|
1844
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
1845
|
-
*
|
|
1846
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
1847
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
1848
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
1849
|
-
* See the License for the specific language governing permissions and
|
|
1850
|
-
* limitations under the License.
|
|
1851
|
-
*/
|
|
1852
|
-
|
|
1853
1757
|
package scope.examples
|
|
1854
1758
|
|
|
1855
1759
|
import zio.blocks.scope._
|
|
@@ -1999,22 +1903,6 @@ sbt "scope-examples/runMain scope.examples.runCachingExample"
|
|
|
1999
1903
|
This example shows how to build a multi-layered web service using Scope for dependency injection, allocating services at different layers and passing them down through child scopes.
|
|
2000
1904
|
|
|
2001
1905
|
```scala title="scope-examples/src/main/scala/scope/examples/LayeredWebServiceExample.scala"
|
|
2002
|
-
/*
|
|
2003
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
2004
|
-
*
|
|
2005
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2006
|
-
* you may not use this file except in compliance with the License.
|
|
2007
|
-
* You may obtain a copy of the License at
|
|
2008
|
-
*
|
|
2009
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2010
|
-
*
|
|
2011
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2012
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2013
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2014
|
-
* See the License for the specific language governing permissions and
|
|
2015
|
-
* limitations under the License.
|
|
2016
|
-
*/
|
|
2017
|
-
|
|
2018
1906
|
package scope.examples
|
|
2019
1907
|
|
|
2020
1908
|
import zio.blocks.scope._
|
|
@@ -2135,22 +2023,6 @@ sbt "scope-examples/runMain scope.examples.layeredWebServiceExample"
|
|
|
2135
2023
|
This example demonstrates loading configuration from a file within a scope, ensuring the file handle is properly closed when no longer needed.
|
|
2136
2024
|
|
|
2137
2025
|
```scala title="scope-examples/src/main/scala/scope/examples/ConfigReaderExample.scala"
|
|
2138
|
-
/*
|
|
2139
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
2140
|
-
*
|
|
2141
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2142
|
-
* you may not use this file except in compliance with the License.
|
|
2143
|
-
* You may obtain a copy of the License at
|
|
2144
|
-
*
|
|
2145
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2146
|
-
*
|
|
2147
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2148
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2149
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2150
|
-
* See the License for the specific language governing permissions and
|
|
2151
|
-
* limitations under the License.
|
|
2152
|
-
*/
|
|
2153
|
-
|
|
2154
2026
|
package scope.examples
|
|
2155
2027
|
|
|
2156
2028
|
import zio.blocks.scope._
|
|
@@ -2279,22 +2151,6 @@ sbt "scope-examples/runMain scope.examples.runConfigReaderExample"
|
|
|
2279
2151
|
This example shows how to build a plugin system that discovers and loads plugins dynamically, managing their lifecycle with scopes.
|
|
2280
2152
|
|
|
2281
2153
|
```scala title="scope-examples/src/main/scala/scope/examples/PluginArchitectureExample.scala"
|
|
2282
|
-
/*
|
|
2283
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
2284
|
-
*
|
|
2285
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2286
|
-
* you may not use this file except in compliance with the License.
|
|
2287
|
-
* You may obtain a copy of the License at
|
|
2288
|
-
*
|
|
2289
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2290
|
-
*
|
|
2291
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2292
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2293
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2294
|
-
* See the License for the specific language governing permissions and
|
|
2295
|
-
* limitations under the License.
|
|
2296
|
-
*/
|
|
2297
|
-
|
|
2298
2154
|
package scope.examples
|
|
2299
2155
|
|
|
2300
2156
|
import zio.blocks.scope._
|
|
@@ -2422,22 +2278,6 @@ sbt "scope-examples/runMain scope.examples.pluginArchitectureExample"
|
|
|
2422
2278
|
This example demonstrates how Scope enforces thread ownership, preventing cross-thread scope misuse and illustrating the difference between owned and unowned scopes.
|
|
2423
2279
|
|
|
2424
2280
|
```scala title="scope-examples/src/main/scala/scope/examples/ThreadOwnershipExample.scala"
|
|
2425
|
-
/*
|
|
2426
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
2427
|
-
*
|
|
2428
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2429
|
-
* you may not use this file except in compliance with the License.
|
|
2430
|
-
* You may obtain a copy of the License at
|
|
2431
|
-
*
|
|
2432
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2433
|
-
*
|
|
2434
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2435
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2436
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2437
|
-
* See the License for the specific language governing permissions and
|
|
2438
|
-
* limitations under the License.
|
|
2439
|
-
*/
|
|
2440
|
-
|
|
2441
2281
|
package scope.examples
|
|
2442
2282
|
|
|
2443
2283
|
import zio.blocks.scope._
|
|
@@ -2607,22 +2447,6 @@ sbt "scope-examples/runMain runThreadOwnershipExample"
|
|
|
2607
2447
|
This example shows how to detect and handle circular dependencies in resource management, illustrating how scopes help prevent subtle bugs in complex dependency graphs.
|
|
2608
2448
|
|
|
2609
2449
|
```scala title="scope-examples/src/main/scala/scope/examples/CircularDependencyDemoExample.scala"
|
|
2610
|
-
/*
|
|
2611
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
2612
|
-
*
|
|
2613
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2614
|
-
* you may not use this file except in compliance with the License.
|
|
2615
|
-
* You may obtain a copy of the License at
|
|
2616
|
-
*
|
|
2617
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2618
|
-
*
|
|
2619
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2620
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2621
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2622
|
-
* See the License for the specific language governing permissions and
|
|
2623
|
-
* limitations under the License.
|
|
2624
|
-
*/
|
|
2625
|
-
|
|
2626
2450
|
package scope.examples
|
|
2627
2451
|
|
|
2628
2452
|
import zio.blocks.scope._
|
|
@@ -2746,22 +2570,6 @@ sbt "scope-examples/runMain scope.examples.circularDependencyDemoExample"
|
|
|
2746
2570
|
This example demonstrates how to integrate Scope with legacy libraries that don't natively support resource management, using wrapper resources and the `leak` escape hatch when necessary.
|
|
2747
2571
|
|
|
2748
2572
|
```scala title="scope-examples/src/main/scala/scope/examples/LegacyLibraryInteropExample.scala"
|
|
2749
|
-
/*
|
|
2750
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
2751
|
-
*
|
|
2752
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2753
|
-
* you may not use this file except in compliance with the License.
|
|
2754
|
-
* You may obtain a copy of the License at
|
|
2755
|
-
*
|
|
2756
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2757
|
-
*
|
|
2758
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2759
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2760
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2761
|
-
* See the License for the specific language governing permissions and
|
|
2762
|
-
* limitations under the License.
|
|
2763
|
-
*/
|
|
2764
|
-
|
|
2765
2573
|
package scope.examples
|
|
2766
2574
|
|
|
2767
2575
|
import zio.blocks.scope._
|
|
@@ -2888,22 +2696,6 @@ sbt "scope-examples/runMain scope.examples.legacyLibraryInteropExample"
|
|
|
2888
2696
|
This example shows how to use Scope to manage test fixtures and resources in integration tests, ensuring automatic cleanup between test runs and proper resource finalization.
|
|
2889
2697
|
|
|
2890
2698
|
```scala title="scope-examples/src/main/scala/scope/examples/IntegrationTestHarnessExample.scala"
|
|
2891
|
-
/*
|
|
2892
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
2893
|
-
*
|
|
2894
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
2895
|
-
* you may not use this file except in compliance with the License.
|
|
2896
|
-
* You may obtain a copy of the License at
|
|
2897
|
-
*
|
|
2898
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
2899
|
-
*
|
|
2900
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
2901
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
2902
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
2903
|
-
* See the License for the specific language governing permissions and
|
|
2904
|
-
* limitations under the License.
|
|
2905
|
-
*/
|
|
2906
|
-
|
|
2907
2699
|
package scope.examples
|
|
2908
2700
|
|
|
2909
2701
|
import zio.blocks.scope._
|
|
@@ -92,13 +92,13 @@ Scope.global.scoped { scope =>
|
|
|
92
92
|
Add the following dependency to your `build.sbt`:
|
|
93
93
|
|
|
94
94
|
```scala
|
|
95
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-scope" % "0.0.
|
|
95
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-scope" % "0.0.55"
|
|
96
96
|
```
|
|
97
97
|
|
|
98
98
|
For cross-platform (Scala.js):
|
|
99
99
|
|
|
100
100
|
```scala
|
|
101
|
-
libraryDependencies += "dev.zio" %%% "zio-blocks-scope" % "0.0.
|
|
101
|
+
libraryDependencies += "dev.zio" %%% "zio-blocks-scope" % "0.0.55"
|
|
102
102
|
```
|
|
103
103
|
|
|
104
104
|
Supported Scala versions: 2.13.x and 3.x.
|
|
@@ -505,6 +505,8 @@ When `Resource.from` composes wires, it respects the sharing strategy:
|
|
|
505
505
|
|
|
506
506
|
The macro detects cycles, duplicate providers, and missing dependencies at compile time.
|
|
507
507
|
|
|
508
|
+
The `AppConfig` leaf above is hardcoded, which is rarely what a real graph wants. `Config.wire[A]` produces a `Wire.Shared[ConfigSource, A]` that decodes `A` from an injected `ConfigSource`, so configuration becomes a node in the graph rather than something resolved before it — see [Config](../config/index.md).
|
|
509
|
+
|
|
508
510
|
## Comparison with Alternatives
|
|
509
511
|
|
|
510
512
|
| Feature | Wire | Manual Passing | Service Locator |
|
|
@@ -531,22 +533,6 @@ cd zio-blocks
|
|
|
531
533
|
Basic wire construction demonstrates how to create and use `Wire` for dependency injection. View the example source code:
|
|
532
534
|
|
|
533
535
|
```scala title="scope-examples/src/main/scala/wire/WireBasicExample.scala"
|
|
534
|
-
/*
|
|
535
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
536
|
-
*
|
|
537
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
538
|
-
* you may not use this file except in compliance with the License.
|
|
539
|
-
* You may obtain a copy of the License at
|
|
540
|
-
*
|
|
541
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
542
|
-
*
|
|
543
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
544
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
545
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
546
|
-
* See the License for the specific language governing permissions and
|
|
547
|
-
* limitations under the License.
|
|
548
|
-
*/
|
|
549
|
-
|
|
550
536
|
package wire
|
|
551
537
|
|
|
552
538
|
import zio.blocks.scope._
|
|
@@ -650,22 +636,6 @@ sbt "scope-examples/runMain wire.wireBasicExample"
|
|
|
650
636
|
Comparing shared vs unique semantics shows how shared wires reuse the same instance across dependents, while unique wires create fresh instances. View the example:
|
|
651
637
|
|
|
652
638
|
```scala title="scope-examples/src/main/scala/wire/WireSharedUniqueExample.scala"
|
|
653
|
-
/*
|
|
654
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
655
|
-
*
|
|
656
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
657
|
-
* you may not use this file except in compliance with the License.
|
|
658
|
-
* You may obtain a copy of the License at
|
|
659
|
-
*
|
|
660
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
661
|
-
*
|
|
662
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
663
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
664
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
665
|
-
* See the License for the specific language governing permissions and
|
|
666
|
-
* limitations under the License.
|
|
667
|
-
*/
|
|
668
|
-
|
|
669
639
|
package wire
|
|
670
640
|
|
|
671
641
|
import zio.blocks.scope._
|
|
@@ -771,22 +741,6 @@ sbt "scope-examples/runMain wire.wireSharedUniqueExample"
|
|
|
771
741
|
Manual wire construction demonstrates how to use `fromFunction` for custom construction logic. View the example:
|
|
772
742
|
|
|
773
743
|
```scala title="scope-examples/src/main/scala/wire/WireFromFunctionExample.scala"
|
|
774
|
-
/*
|
|
775
|
-
* Copyright 2024-2026 John A. De Goes and the ZIO Contributors
|
|
776
|
-
*
|
|
777
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
778
|
-
* you may not use this file except in compliance with the License.
|
|
779
|
-
* You may obtain a copy of the License at
|
|
780
|
-
*
|
|
781
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
782
|
-
*
|
|
783
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
784
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
785
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
786
|
-
* See the License for the specific language governing permissions and
|
|
787
|
-
* limitations under the License.
|
|
788
|
-
*/
|
|
789
|
-
|
|
790
744
|
package wire
|
|
791
745
|
|
|
792
746
|
import zio.blocks.scope._
|
|
@@ -64,7 +64,7 @@ Batching reduces per-element synchronization costs.
|
|
|
64
64
|
|
|
65
65
|
When multiple workers consume from the same queue, use `MpmcRingBuffer`. Each worker calls `MpmcRingBuffer#take` to grab the next item atomically:
|
|
66
66
|
|
|
67
|
-
```scala
|
|
67
|
+
```scala
|
|
68
68
|
import zio.blocks.ringbuffer.MpmcRingBuffer
|
|
69
69
|
|
|
70
70
|
case class Task(id: Int, work: String)
|
|
@@ -48,7 +48,7 @@ Building low-latency systems — trading platforms, game engines, real-time even
|
|
|
48
48
|
|
|
49
49
|
A naive approach is to pre-allocate a large `Array[A]` and manually manage head/tail indices. This code shows a simplified single-threaded version:
|
|
50
50
|
|
|
51
|
-
```scala
|
|
51
|
+
```scala
|
|
52
52
|
class SimpleRingBuffer {
|
|
53
53
|
// WARNING: Mutable state (vars) demonstrated here to show the problem;
|
|
54
54
|
// this is NOT how ring buffers actually solve concurrency.
|
|
@@ -124,13 +124,13 @@ RingBuffer is ideal when you control both producer and consumer thread counts an
|
|
|
124
124
|
Add the ZIO Blocks Ring Buffer module to your `build.sbt`:
|
|
125
125
|
|
|
126
126
|
```scala
|
|
127
|
-
libraryDependencies += "dev.zio" %% "zio-blocks-ringbuffer" % "
|
|
127
|
+
libraryDependencies += "dev.zio" %% "zio-blocks-ringbuffer" % "0.0.55"
|
|
128
128
|
```
|
|
129
129
|
|
|
130
130
|
For Scala.js cross-platform support:
|
|
131
131
|
|
|
132
132
|
```scala
|
|
133
|
-
libraryDependencies += "dev.zio" %%% "zio-blocks-ringbuffer" % "
|
|
133
|
+
libraryDependencies += "dev.zio" %%% "zio-blocks-ringbuffer" % "0.0.55"
|
|
134
134
|
```
|
|
135
135
|
|
|
136
136
|
## Implementations
|
|
@@ -92,7 +92,7 @@ Creates a new MPMC ring buffer with the given capacity. The capacity must be a p
|
|
|
92
92
|
|
|
93
93
|
To create an MPMC buffer:
|
|
94
94
|
|
|
95
|
-
```scala
|
|
95
|
+
```scala
|
|
96
96
|
import zio.blocks.ringbuffer.MpmcRingBuffer
|
|
97
97
|
|
|
98
98
|
val rb = MpmcRingBuffer[String](128)
|
|
@@ -138,10 +138,44 @@ All four implementations provide the same method signatures: `size`, `isEmpty`,
|
|
|
138
138
|
|
|
139
139
|
For workloads with multiple producers and consumers, use `MpmcRingBuffer`. This example demonstrates how multiple workers coordinate to process tasks from a shared queue.
|
|
140
140
|
|
|
141
|
-
```scala
|
|
142
|
-
|
|
141
|
+
```scala title="zio-blocks-examples/src/main/scala/ringbuffer/MpmcExample.scala"
|
|
142
|
+
package ringbuffer
|
|
143
143
|
|
|
144
|
-
|
|
144
|
+
import zio.blocks.ringbuffer.MpmcRingBuffer
|
|
145
|
+
import java.util.concurrent.{CountDownLatch, Thread}
|
|
146
|
+
import java.util.concurrent.atomic.AtomicInteger
|
|
147
|
+
|
|
148
|
+
object MpmcExample extends App {
|
|
149
|
+
val buffer = MpmcRingBuffer[String](32)
|
|
150
|
+
val processed = new AtomicInteger(0)
|
|
151
|
+
val latch = new CountDownLatch(2)
|
|
152
|
+
|
|
153
|
+
val producers = (0 until 2).map { id =>
|
|
154
|
+
new Thread(() => {
|
|
155
|
+
for (i <- 1 to 5) {
|
|
156
|
+
buffer.offer(s"task-$id-$i")
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
val consumers = (0 until 2).map { _ =>
|
|
162
|
+
new Thread(() => {
|
|
163
|
+
while (processed.get() < 10) {
|
|
164
|
+
val task = buffer.take()
|
|
165
|
+
if (task ne null) {
|
|
166
|
+
println(s"Worker processing: $task")
|
|
167
|
+
processed.incrementAndGet()
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
latch.countDown()
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
(producers ++ consumers).foreach(_.start())
|
|
175
|
+
producers.foreach(_.join())
|
|
176
|
+
latch.await()
|
|
177
|
+
println("All tasks completed")
|
|
178
|
+
}
|
|
145
179
|
```
|
|
146
180
|
|
|
147
181
|
([source](https://github.com/zio/zio-blocks/blob/main/zio-blocks-examples/src/main/scala/ringbuffer/MpmcExample.scala))
|