Skip to content

Commit

Permalink
Handle ping/pong action, plus Github Action for Relase
Browse files Browse the repository at this point in the history
  • Loading branch information
owenkellogg committed Jan 7, 2025
1 parent 905d2b5 commit 93ebb25
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 1 deletion.
40 changes: 40 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Build and Release

on:
push:
tags:
- 'v*'

jobs:
build-linux:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: x86_64-unknown-linux-gnu
override: true

- name: Build
run: |
cargo build --release --target x86_64-unknown-linux-gnu
tar -czf anypay-websockets-linux-x86_64.tar.gz -C target/x86_64-unknown-linux-gnu/release anypay-websockets
- name: Create Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
anypay-websockets-linux-x86_64.tar.gz
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: anypay-websockets-linux-x86_64
path: target/x86_64-unknown-linux-gnu/release/anypay-websockets
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ shortid = "1.0.6"
bigdecimal = "0.4.7"
anyhow = "1.0"
alloy = { version = "0.3", features = ["full"] }
futures-util = "0.3"
futures-util = "0.3"

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = 'abort'
strip = true # Automatically strip symbols from the binary

[target.x86_64-unknown-linux-gnu]
linker = "x86_64-unknown-linux-gnu-gcc"
22 changes: 22 additions & 0 deletions docs/asyncapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ channels:
publish:
message:
oneOf:
- name: Ping
payload:
type: object
required:
- type
properties:
type:
type: string
const: Ping
- name: CreateInvoice
payload:
type: object
Expand Down Expand Up @@ -132,6 +141,19 @@ channels:
subscribe:
message:
oneOf:
- name: Pong
payload:
type: object
properties:
type:
type: string
const: pong
status:
type: string
enum: [success]
timestamp:
type: integer
format: int64
- name: Response
payload:
oneOf:
Expand Down
41 changes: 41 additions & 0 deletions scripts/test_ping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import asyncio
import websockets
import json
from datetime import datetime

async def test_ping():
try:
# Connect to WebSocket server
async with websockets.connect('ws://localhost:8080') as websocket:
# Create ping message
ping_message = {
"action": "ping"
}

print("Sending ping...")
await websocket.send(json.dumps(ping_message))

# Wait for pong response
response = await websocket.recv()
response_data = json.loads(response)

print(f"Received response: {response_data}")

# Validate response
if (response_data.get('type') == 'pong' and
response_data.get('status') == 'success' and
'timestamp' in response_data):

# Convert timestamp to readable format
timestamp = datetime.fromtimestamp(response_data['timestamp'])
print(f"✅ Received pong at {timestamp}")
print(f"Round trip latency: {datetime.utcnow().timestamp() - response_data['timestamp']}s")
else:
print("❌ Invalid pong response:", response_data)

except Exception as e:
print(f"❌ Error: {e}")

if __name__ == "__main__":
asyncio.run(test_ping())
1 change: 1 addition & 0 deletions src/amqp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ async fn consume_events(mut consumer: Consumer) {
while let Some(delivery) = consumer.next().await {
if let Ok(delivery) = delivery {
if let Ok(data) = std::str::from_utf8(&delivery.data) {
println!("AMQP Event: {}", data);
tracing::info!("AMQP Event: {}", data);
}
delivery.ack(BasicAckOptions::default()).await.ok();
Expand Down
7 changes: 7 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ impl AnypayEventsServer {
})
}
}
Message::Ping => {
json!({
"type": "pong",
"status": "success",
"timestamp": chrono::Utc::now().timestamp()
})
},
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub enum Message {
CancelInvoice {
uid: String,
},
#[serde(rename = "ping")]
Ping,
}

fn deserialize_number_from_string<'de, D>(deserializer: D) -> Result<f64, D::Error>
Expand Down

0 comments on commit 93ebb25

Please sign in to comment.