Skip to content

Commit

Permalink
Custom slot suplier (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sushisource authored Dec 5, 2024
1 parent c219615 commit 15a3142
Show file tree
Hide file tree
Showing 31 changed files with 1,456 additions and 130 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ obj/
/tests/golangworker/golangworker
/.vs
/.vscode
/.idea
/.idea
/.zed
Temporalio.sln.DotSettings.user
29 changes: 15 additions & 14 deletions src/Temporalio/Bridge/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/Temporalio/Bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
anyhow = "1.0"
async-trait = "0.1"
futures = "0.3"
libc = "0.2"
log = "0.4"
Expand All @@ -21,7 +22,9 @@ rand = "0.8.5"
rand_pcg = "0.3.1"
serde_json = "1.0"
temporal-client = { version = "0.1.0", path = "./sdk-core/client" }
temporal-sdk-core = { version = "0.1.0", path = "./sdk-core/core", features = ["ephemeral-server"] }
temporal-sdk-core = { version = "0.1.0", path = "./sdk-core/core", features = [
"ephemeral-server",
] }
temporal-sdk-core-api = { version = "0.1.0", path = "./sdk-core/core-api" }
temporal-sdk-core-protos = { version = "0.1.0", path = "./sdk-core/sdk-core-protos" }
tokio = "1.26"
Expand All @@ -39,3 +42,4 @@ incremental = false

[build-dependencies]
cbindgen = "0.24"
anyhow = "1.0"
36 changes: 3 additions & 33 deletions src/Temporalio/Bridge/CustomMetricMeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ namespace Temporalio.Bridge
/// <summary>
/// Core wrapper for a custom metric meter implementation.
/// </summary>
internal class CustomMetricMeter
internal class CustomMetricMeter : NativeInvokeableClass<Interop.CustomMetricMeter>
{
private readonly Temporalio.Runtime.ICustomMetricMeter meter;
private readonly Temporalio.Runtime.CustomMetricMeterOptions options;
private readonly List<GCHandle> handles = new();

/// <summary>
/// Initializes a new instance of the <see cref="CustomMetricMeter" /> class.
Expand All @@ -38,20 +37,9 @@ public unsafe CustomMetricMeter(
meter_free = FunctionPointer<Interop.CustomMetricMeterMeterFreeCallback>(Free),
};

// Pin the metric meter pointer and set it as the first handle
var interopMeterHandle = GCHandle.Alloc(interopMeter, GCHandleType.Pinned);
handles.Insert(0, interopMeterHandle);
Ptr = (Interop.CustomMetricMeter*)interopMeterHandle.AddrOfPinnedObject();

// Add handle for ourself
handles.Add(GCHandle.Alloc(this));
PinCallbackHolder(interopMeter);
}

/// <summary>
/// Gets the pointer to the native metric meter.
/// </summary>
internal unsafe Interop.CustomMetricMeter* Ptr { get; private init; }

private static unsafe string? GetStringOrNull(Interop.ByteArrayRef bytes) =>
(int)bytes.size == 0 ? null : GetString(bytes);

Expand Down Expand Up @@ -220,23 +208,5 @@ private unsafe void RecordMetricDuration(void* metric, ulong valueMs, void* attr
}

private unsafe void FreeAttributes(void* attributes) => GCHandle.FromIntPtr(new(attributes)).Free();

private unsafe void Free(Interop.CustomMetricMeter* meter)
{
// Free in order which frees function pointers first then meter handles
foreach (var handle in handles)
{
handle.Free();
}
}

// Similar to Scope.FunctionPointer
private IntPtr FunctionPointer<T>(T func)
where T : Delegate
{
var handle = GCHandle.Alloc(func);
handles.Add(handle);
return Marshal.GetFunctionPointerForDelegate(handle.Target!);
}
}
}
}
Loading

0 comments on commit 15a3142

Please sign in to comment.