Last active
July 31, 2023 17:14
-
-
Save M0nteCarl0/9159d2eaa6b51e40a16a9736ae3b03fa to your computer and use it in GitHub Desktop.
Golang connection manager
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
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
// ConnectionManager интерфейс определяет методы для управления соединениями | |
type ConnectionManager interface { | |
AddConnection(connection Connection) | |
RemoveConnection(connection Connection) | |
Broadcast(message string) | |
} | |
// Connection представляет собой соединение | |
type Connection struct { | |
ID int | |
Name string | |
} | |
// ConnectionManagerImpl реализация интерфейса ConnectionManager | |
type ConnectionManagerImpl struct { | |
connections map[int]Connection | |
mutex sync.RWMutex | |
} | |
// AddConnection добавляет новое соединение в менеджер | |
func (cm *ConnectionManagerImpl) AddConnection(connection Connection) { | |
cm.mutex.Lock() | |
defer cm.mutex.Unlock() | |
cm.connections[connection.ID] = connection | |
} | |
// RemoveConnection удаляет соединение из менеджера | |
func (cm *ConnectionManagerImpl) RemoveConnection(connection Connection) { | |
cm.mutex.Lock() | |
defer cm.mutex.Unlock() | |
delete(cm.connections, connection.ID) | |
} | |
// Broadcast отправляет сообщение всем соединениям в менеджере | |
func (cm *ConnectionManagerImpl) Broadcast(message string) { | |
cm.mutex.RLock() | |
defer cm.mutex.RUnlock() | |
var wg sync.WaitGroup | |
wg.Add(len(cm.connections)) | |
for _, connection := range cm.connections { | |
go func(conn Connection) { | |
defer wg.Done() | |
fmt.Printf("Sending message \"%s\" to connection %d\n", message, conn.ID) | |
}(connection) | |
} | |
wg.Wait() | |
} | |
func main() { | |
// Создание экземпляра менеджера соединений | |
connectionManager := &ConnectionManagerImpl{ | |
connections: make(map[int]Connection), | |
} | |
// Создание и добавление соединений | |
connection1 := Connection{ID: 1, Name: "Connection 1"} | |
connection2 := Connection{ID: 2, Name: "Connection 2"} | |
connectionManager.AddConnection(connection1) | |
connectionManager.AddConnection(connection2) | |
// Отправка широковещательного сообщения | |
connectionManager.Broadcast("Hello, everyone!") | |
// Удаление соединения | |
connectionManager.RemoveConnection(connection1) | |
// Отправка широковещательного сообщения после удаления соединения | |
connectionManager.Broadcast("Goodbye, everyone!") | |
} |
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
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
// ConnectionManager интерфейс определяет методы для управления соединениями | |
type ConnectionManager interface { | |
AddConnection(connection Connection) | |
RemoveConnection(connection Connection) | |
Broadcast(message string) | |
} | |
// Connection представляет собой соединение | |
type Connection struct { | |
ID int | |
Name string | |
} | |
// ConnectionManagerImpl реализация интерфейса ConnectionManager | |
type ConnectionManagerImpl struct { | |
connections map[int]Connection | |
mutex sync.RWMutex | |
} | |
// AddConnection добавляет новое соединение в менеджер | |
func (cm *ConnectionManagerImpl) AddConnection(connection Connection) { | |
cm.mutex.Lock() | |
defer cm.mutex.Unlock() | |
cm.connections[connection.ID] = connection | |
} | |
// RemoveConnection удаляет соединение из менеджера | |
func (cm *ConnectionManagerImpl) RemoveConnection(connection Connection) { | |
cm.mutex.Lock() | |
defer cm.mutex.Unlock() | |
delete(cm.connections, connection.ID) | |
} | |
// Broadcast отправляет сообщение всем соединениям в менеджере | |
func (cm *ConnectionManagerImpl) Broadcast(message string) { | |
cm.mutex.RLock() | |
defer cm.mutex.RUnlock() | |
for _, connection := range cm.connections { | |
fmt.Printf("Sending message \"%s\" to connection %d\n", message, connection.ID) | |
} | |
} | |
func main() { | |
// Создание экземпляра менеджера соединений | |
connectionManager := &ConnectionManagerImpl{ | |
connections: make(map[int]Connection), | |
} | |
// Создание и добавление соединений | |
connection1 := Connection{ID: 1, Name: "Connection 1"} | |
connection2 := Connection{ID: 2, Name: "Connection 2"} | |
connectionManager.AddConnection(connection1) | |
connectionManager.AddConnection(connection2) | |
// Отправка широковещательного сообщения | |
connectionManager.Broadcast("Hello, everyone!") | |
// Удаление соединения | |
connectionManager.RemoveConnection(connection1) | |
// Отправка широковещательного сообщения после удаления соединения | |
connectionManager.Broadcast("Goodbye, everyone!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment