Skip to content

Commit

Permalink
a shit actor
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Feb 26, 2024
1 parent 95e2b59 commit b2395ec
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ version = "1.0.0"
gleam_stdlib = "~> 0.34 or ~> 1.0"
gleam_erlang = "~> 0.24"
gleam_otp = "~> 0.9"
prng = "~> 3.0"

[dev-dependencies]
gleeunit = "~> 1.0"
7 changes: 5 additions & 2 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_bitwise", version = "1.3.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "B36E1D3188D7F594C7FD4F43D0D2CE17561DE896202017548578B16FE1FE9EFC" },
{ name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" },
{ name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" },
{ name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" },
{ name = "gleam_stdlib", version = "0.35.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5443EEB74708454B65650FEBBB1EF5175057D1DEC62AEA9D7C6D96F41DA79152" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "prng", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_bitwise"], otp_app = "prng", source = "hex", outer_checksum = "C78A80DE41469A0BB1AB3B0B0610CCE5DB70C5659A540E2E0E6C54FA38134290" },
]

[requirements]
gleam_erlang = { version = "~> 0.24" }
gleam_otp = { version = "~> 0.9"}
gleam_otp = { version = "~> 0.9" }
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
prng = { version = "~> 3.0"}
1 change: 1 addition & 0 deletions src/supervisors.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//// Todo
38 changes: 38 additions & 0 deletions src/supervisors/a_shit_actor.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// This module implements a shit actor that crashes every now and then.
//// Having an actor that fails every now and then will help us test out our supervisors.
//// It's also a nice refresher on actors. Read the code and make sure it makes sense.

import gleam/otp/actor
import gleam/erlang/process.{type Subject}
import prng/random

pub fn new() -> Result(Subject(Message), actor.StartError) {
actor.start(Nil, handle_message)
}

pub fn shutdown(subject: Subject(Message)) {
actor.send(subject, Shutdown)
}

pub fn play_game(subject: Subject(Message)) -> String {
let msg_generator = random.weighted(#(0.9, Duck), [#(0.1, Goose)])
let msg = random.random_sample(msg_generator)
actor.call(subject, msg, 1000)
}

pub type Message {
Duck(client: Subject(String))
Goose(client: Subject(String))
Shutdown
}

fn handle_message(message: Message, _state: Nil) -> actor.Next(Message, Nil) {
case message {
Duck(client) -> {
actor.send(client, "duck")
actor.continue(Nil)
}
Goose(_) -> panic as "Oh shit it's a fucking goose!!!!"

This comment has been minimized.

Copy link
@tankorsmash

tankorsmash Mar 14, 2024

amazing :)

This comment has been minimized.

Copy link
@bcpeinhardt

bcpeinhardt Mar 14, 2024

Author Owner

Geese are scary sometimes 😆

Shutdown -> actor.Stop(process.Normal)
}
}

0 comments on commit b2395ec

Please sign in to comment.