Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions lib/gasfree_sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module GasfreeSdk
}

class << self
# Configure the SDK
# Configure the SDK (thread-safe, global)
# @yield [config] Configuration block
# @example
# GasfreeSdk.configure do |config|
Expand All @@ -52,18 +52,19 @@ def configure
return unless config.api_endpoint != old_endpoint

validate_api_endpoint!(config.api_endpoint)
# Reset all thread-local clients if needed (not implemented here)
end

# Create a new client instance
# Get or create a thread-local client instance
# @return [GasfreeSdk::Client]
def client
@client ||= Client.new
Thread.current[:gasfree_sdk_client] ||= Client.new
end

# Reset the client instance
# Reset the client instance for the current thread
# @return [void]
def reset_client!
@client = nil
Thread.current[:gasfree_sdk_client] = nil
end

private
Expand All @@ -83,5 +84,30 @@ def validate_api_endpoint!(value)

# Base error class for all SDK errors
class Error < StandardError; end
# Your code goes here...

# ----------------------------------------------------------------------
# Thread Safety Documentation
# ----------------------------------------------------------------------
#
# Thread Safety:
# - The SDK configuration is globally thread-safe (handled by dry-configurable).
# - Each thread gets its own client instance (thread-local, via Thread.current).
# - All client instances use the same global configuration.
# - Do NOT attempt to create per-thread config objects; always use GasfreeSdk.config.
#
# Usage Example:
# # Configure globally (thread-safe)
# GasfreeSdk.configure do |config|
# config.api_key = "your-api-key"
# config.api_secret = "your-api-secret"
# end
#
# # In each thread:
# client = GasfreeSdk.client
# # ... use client ...
#
# # To reset the client for the current thread:
# GasfreeSdk.reset_client!
#
# ----------------------------------------------------------------------
end
Loading