Documentation
¶
Index ¶
- Constants
- Variables
- func SetTx(ctx context.Context, tx Tx) context.Context
- type ActiveClient
- func (ac *ActiveClient) AddConn(c Conn) (int, error)
- func (ac *ActiveClient) Delete(repo *ActiveClientRepository) (event.ActiveClientInactivated, error)
- func (ac *ActiveClient) ForceDelete(repo *ActiveClientRepository) (event.ActiveClientInactivated, error)
- func (ac *ActiveClient) HasConn(c Conn) bool
- func (ac *ActiveClient) RemoveConn(c Conn) (int, error)
- func (ac *ActiveClient) Send(ev event.Event)
- type ActiveClientRepository
- func (cm *ActiveClientRepository) ExistByConn(c Conn) bool
- func (cm *ActiveClientRepository) Find(userID uint64) (*ActiveClient, error)
- func (cm *ActiveClientRepository) FindAllByUserIDs(userIDs []uint64) ([]*ActiveClient, error)
- func (cm *ActiveClientRepository) Remove(ac *ActiveClient) error
- func (cm *ActiveClientRepository) Store(ac *ActiveClient) error
- type Conn
- type EmptyTxBeginner
- type EventHolder
- type Message
- type MessageRepository
- type Repositories
- type Room
- func (r *Room) AddMember(user User) (event.RoomAddedMember, error)
- func (r *Room) Delete(ctx context.Context, repo RoomRepository, user *User) error
- func (r *Room) HasMember(member User) bool
- func (r *Room) MemberIDs() []uint64
- func (r *Room) NotExist() bool
- func (r *Room) ReadMessagesBy(u *User, readAt time.Time) (event.RoomMessagesReadByUser, error)
- func (r *Room) RemoveMember(user User) (event.RoomRemovedMember, error)
- type RoomRepository
- type SimpleRepositories
- type TimeSet
- type Tx
- type TxBeginner
- type User
- type UserIDSet
- type UserRepository
Constants ¶
const MaxConns = 16
MaxConns is the maximum number of the connections per ActiveClient.
Variables ¶
var ErrExceedsMaxConns = errors.New("exceed the number of connections")
ErrExceedsConnsMax indicates the number of the connection for the ActiveClient exceeds the maximum limits.
Functions ¶
Types ¶
type ActiveClient ¶
type ActiveClient struct {
// contains filtered or unexported fields
}
ActiveClient is a one active domain User which has several connections.
ActiveClient can have multiple conncetions, because one user can conncets from PC, mobile device, and so on.
func NewActiveClient ¶
func NewActiveClient(repo *ActiveClientRepository, c Conn, u User) (*ActiveClient, event.ActiveClientActivated, error)
func (*ActiveClient) AddConn ¶
func (ac *ActiveClient) AddConn(c Conn) (int, error)
Add new connection to ActiveClient. It returns current number of the connections and error when the connection is not for the AcitiveClient or ErrExceedsMaxConns when the number of the connections exceeds the maximum limit.
func (*ActiveClient) Delete ¶
func (ac *ActiveClient) Delete(repo *ActiveClientRepository) (event.ActiveClientInactivated, error)
Delete deletes this ActiveClient from the repository. If ayt of connections exist, it fail and return error.
func (*ActiveClient) ForceDelete ¶
func (ac *ActiveClient) ForceDelete(repo *ActiveClientRepository) (event.ActiveClientInactivated, error)
ForceDelete forcibly deletes this ActiveClient from the repository. It closes all of underlying connections and removes from ActiveClient. It returns error if already Deleted.
func (*ActiveClient) HasConn ¶
func (ac *ActiveClient) HasConn(c Conn) bool
func (*ActiveClient) RemoveConn ¶
func (ac *ActiveClient) RemoveConn(c Conn) (int, error)
RemoveConn removes connection from ActiveClient. It returns the rest number of the connection and errors related with the connection is removed.
func (*ActiveClient) Send ¶
func (ac *ActiveClient) Send(ev event.Event)
Send domain event to all of the client connections.
type ActiveClientRepository ¶
type ActiveClientRepository struct {
// contains filtered or unexported fields
}
ActiveClientRepository manages active clients. It is inmemory struct type rather than interface because ActiveClient has connection interface which can not be serialized.
func NewActiveClientRepository ¶
func NewActiveClientRepository(capHint int) *ActiveClientRepository
func (*ActiveClientRepository) ExistByConn ¶
func (cm *ActiveClientRepository) ExistByConn(c Conn) bool
Check whether the specified connection exists in the repository. It returns true if found otherwise returns false.
func (*ActiveClientRepository) Find ¶
func (cm *ActiveClientRepository) Find(userID uint64) (*ActiveClient, error)
Find ActiveClient by user ID. It returns found AcitiveClient and error if not found.
func (*ActiveClientRepository) FindAllByUserIDs ¶
func (cm *ActiveClientRepository) FindAllByUserIDs(userIDs []uint64) ([]*ActiveClient, error)
Find all ActiveClients by user ID list. It returns found AcitiveClients and error if all of the user ID are not found. The number of the returned ActiveClients may not same as that of user ID list, when a part of the user ID list are not found.
func (*ActiveClientRepository) Remove ¶
func (cm *ActiveClientRepository) Remove(ac *ActiveClient) error
Remove connection from the repository.
func (*ActiveClientRepository) Store ¶
func (cm *ActiveClientRepository) Store(ac *ActiveClient) error
Store activeClient to the repository. It returns error when something wrong.
type Conn ¶
type Conn interface {
// It returns user specific id to distinguish which client
// connect to.
UserID() uint64
// It sends any domain event to client.
Send(ev event.Event)
// Close close the underlying connection.
// It should not panic when it is called multiple time, returnning error is OK.
Close() error
}
Conn is a interface for the end-point connection for sending domain event.
type EmptyTxBeginner ¶
type EmptyTxBeginner struct{}
EmptyTxBeginner implements Tx and TxBeginner interfaces. It is used to implement the repository with the no-operating transaction, typically used as embedded struct.
func (EmptyTxBeginner) Commit ¶
func (EmptyTxBeginner) Commit() error
func (EmptyTxBeginner) Rollback ¶
func (EmptyTxBeginner) Rollback() error
type EventHolder ¶
type EventHolder struct {
// contains filtered or unexported fields
}
EventHolder holds event objects. It is used to embed into entity.
func NewEventHolder ¶
func NewEventHolder() EventHolder
func (*EventHolder) AddEvent ¶
func (holder *EventHolder) AddEvent(ev event.Event)
func (*EventHolder) Events ¶
func (holder *EventHolder) Events() []event.Event
type Message ¶
type Message struct {
EventHolder
// ID and CreatedAt are auto set.
ID uint64 `db:"id"`
CreatedAt time.Time `db:"created_at"`
Content string `db:"content"`
UserID uint64 `db:"user_id"`
RoomID uint64 `db:"room_id"`
Deleted bool `db:"deleted"`
}
func NewRoomMessage ¶
func NewRoomMessage( ctx context.Context, msgs MessageRepository, u User, r Room, content string, ) (Message, error)
NewRoomMessage creates new message for the specified room. The created message is immediately stored into the repository. It returns new message holding event message created and error if any.
type MessageRepository ¶
type MessageRepository interface {
TxBeginner
Find(ctx context.Context, msgID uint64) (Message, error)
// Store stores given message to the repository.
// user need not to set ID for message since it is auto set
// when message is newly.
// It returns stored Message ID and error.
Store(ctx context.Context, m Message) (uint64, error)
// RemoveAllByRoomID removes all messages related with roomID.
RemoveAllByRoomID(ctx context.Context, roomID uint64) error
}
type Repositories ¶
type Repositories interface {
Users() UserRepository
Messages() MessageRepository
Rooms() RoomRepository
Events() event.EventRepository
}
Repositories holds any XXXRepository. you can get each repository from this.
type Room ¶
type Room struct {
EventHolder
// ID = 0 means new entity which is not in the repository
ID uint64
Name string
IsTalkRoom bool
CreatedAt time.Time
OwnerID uint64
MemberIDSet UserIDSet
// key: userID, value: ReadTime
MemberReadTimes TimeSet
}
Room entity. Its fields are exported due to construct from the datastore. In application side, creating/modifying/deleting the room should be done by the methods which emits the domain event.
func NewRoom ¶
func NewRoom(ctx context.Context, roomRepo RoomRepository, name string, user *User, memberIDs UserIDSet) (*Room, error)
create new Room entity into the repository. It retruns room holding RoomCreated event and error if any.
func (*Room) AddMember ¶
func (r *Room) AddMember(user User) (event.RoomAddedMember, error)
It adds the member to the room. It returns the event adding to the room, and error when the user already exist in the room.
func (*Room) Delete ¶
It deletes the room from repository. After successing that, the room holds RoomDeleted event.
func (*Room) HasMember ¶
It returns true when the room member exists in the room, otherwise returns false.
func (*Room) ReadMessagesBy ¶
ReadMessagesBy marks that the room messages before time readAt are read by the specified user.
It returns MessageReadByUser event and error if any.
func (*Room) RemoveMember ¶
func (r *Room) RemoveMember(user User) (event.RoomRemovedMember, error)
RoomRemovedMember removes the member from the room. It returns the event the room member is removed or returns error when the user already does not exist in the room.
type RoomRepository ¶
type RoomRepository interface {
TxBeginner
// get one room.
Find(ctx context.Context, roomID uint64) (Room, error)
// get all the rooms which user has, from repository.
FindAllByUserID(ctx context.Context, userID uint64) ([]Room, error)
// store new room to repository and return
// stored room id.
Store(ctx context.Context, r Room) (uint64, error)
// remove room from repository.
Remove(ctx context.Context, r Room) error
}
type SimpleRepositories ¶
type SimpleRepositories struct {
UserRepository UserRepository
MessageRepository MessageRepository
RoomRepository RoomRepository
EventRepository event.EventRepository
}
SimpleRepositories implementes Repositories interface. It acts just returning its fields when interface methods, Users(), Messages() and Rooms(), are called.
func (SimpleRepositories) Events ¶
func (s SimpleRepositories) Events() event.EventRepository
func (SimpleRepositories) Messages ¶
func (s SimpleRepositories) Messages() MessageRepository
func (SimpleRepositories) Rooms ¶
func (s SimpleRepositories) Rooms() RoomRepository
func (SimpleRepositories) Users ¶
func (s SimpleRepositories) Users() UserRepository
type TimeSet ¶
type TimeSet struct {
// contains filtered or unexported fields
}
TimeSet is a set for the time.Time. empty value is valid for use.
func NewTimeSet ¶
type Tx ¶
Tx is a interface for the transaction context. the transaction must end by calling Commit() or Rollback().
Because the transaction object depends on the external infrastructures such as Sql-like DB, it can be used with type assertion to use full features for the implementation-specific transaction object.
type TxBeginner ¶
TxBeginner can start transaction with context object. TxOptions are typically used to specify the transaction level. A nil TxOptions means to use default transaction level.
type User ¶
type User struct {
EventHolder
ID uint64
Name string
FirstName string
LastName string
Password string
FriendIDs UserIDSet
}
User entity. Its fields are exported due to construct from the datastore. In application side, creating/modifying/deleting the user should be done by the methods which emits the domain event.
func NewUser ¶
func NewUser( ctx context.Context, userRepo UserRepository, name, firstName, lastName, password string, friendIDs UserIDSet, ) (User, error)
create new Room entity into the repository. It retruns the new user holding event for UserCreated and error if any.
func (*User) AddFriend ¶
func (u *User) AddFriend(friend User) (event.UserAddedFriend, error)
It adds the friend to the user. It returns the event adding into the user, and error when the friend already exist in the user.