-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor TCP server initialization This refactor uses the functional options pattern to extract away the optional TCP server configuration parameters. This: - improves overall readability, by moving away from global variables - makes it easier to configure the tcp server for tests * Use ticker instead of for loop for room deletion Go offers a ticker abstraction designed for performing tasks at a regular interval, and this change uses the ticker for tcp room deletion. It also cleans up the deletion goroutine gracefully. * Add local relay interaction diagram The diagram sketches out the interaction between clients and a local relay. * Add debug logs for room cleanup These would be useful for future development (e.g. adding a stopping mechanism for the TCP listener).
- Loading branch information
Showing
5 changed files
with
126 additions
and
27 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package tcp | ||
|
||
import "time" | ||
|
||
const ( | ||
DEFAULT_LOG_LEVEL = "debug" | ||
DEFAULT_ROOM_CLEANUP_INTERVAL = 10 * time.Minute | ||
DEFAULT_ROOM_TTL = 3 * time.Hour | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package tcp | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// TODO: maybe export from logger library? | ||
var availableLogLevels = []string{"info", "error", "warn", "debug", "trace"} | ||
|
||
type serverOptsFunc func(s *server) error | ||
|
||
func WithBanner(banner ...string) serverOptsFunc { | ||
return func(s *server) error { | ||
if len(banner) > 0 { | ||
s.banner = banner[0] | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func WithLogLevel(level string) serverOptsFunc { | ||
return func(s *server) error { | ||
if !containsSlice(availableLogLevels, level) { | ||
return fmt.Errorf("invalid log level specified: %s", level) | ||
} | ||
s.debugLevel = level | ||
return nil | ||
} | ||
} | ||
|
||
func WithRoomCleanupInterval(interval time.Duration) serverOptsFunc { | ||
return func(s *server) error { | ||
s.roomCleanupInterval = interval | ||
return nil | ||
} | ||
} | ||
|
||
func WithRoomTTL(ttl time.Duration) serverOptsFunc { | ||
return func(s *server) error { | ||
s.roomTTL = ttl | ||
return nil | ||
} | ||
} | ||
|
||
func containsSlice(s []string, e string) bool { | ||
for _, ss := range s { | ||
if e == ss { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters