TangSengDaoDaoServer Documentation

repository·main·Indexed 26 days ago

https://github.com/tangsengdaodao/tangsengdaodaoserver

The backend business logic layer of TangSengDaoDao, an enterprise-grade open-source instant messaging system. It manages social features such as friend relationships, group management, and moments, utilizing WuKongIM as its high-performance communication engine. The server is developed in Go (>= 1.20) and supports deployment via Docker Compose with dependencies including MySQL, Redis, and MinIO.

Tokens
3K
Snippets
9
Records
26
Agent score
82%

What's inside TangSengDaoDaoServer

  1. Overview of TangSengDaoDao Architecture

    main

    TangSengDaoDao is an enterprise-grade open-source instant messaging (IM) system. The architecture is split into two distinct layers to ensure simplicity, scalability, and high availability:

    1. Communication Layer (WuKongIM): Responsible for maintaining long connections, message delivery, and efficient message storage.
    2. Business Layer (TangSengDaoDao): Responsible for business logic such as friend relationships, group management, moments (朋友圈), and other IM-related business features.

    Inter-layer Communication:

    • WuKongIM to TangSengDaoDao: WuKongIM pushes chat-related data required by the business layer to TangSengDaoDao using a gRPC Webhook mechanism.
    • TangSengDaoDao to WuKongIM: When TangSengDaoDao needs to deliver system messages, it calls the WuKongIM message sending APIs.

    Client Interaction:

    • Clients establish long connections with WuKongIM via an SDK for sending and receiving messages.
    • Clients interact with TangSengDaoDao APIs to handle business logic like managing friends, groups, and moments.
  2. Generate Go code from webhook.proto

    main

    To generate the Go source files and gRPC service implementations from the webhook.proto definition, use the protoc compiler with the following command. This ensures that the generated files are placed in the correct relative paths for the wkhook package.

    protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative ./pkg/wkhook/webhook.proto
  3. Optimize code to prevent deadlocks

    main

    To prevent deadlocks at the application level, implement the following strategies:

    1. Use INSERT IGNORE: Instead of a standard INSERT, use INSERT IGNORE to handle duplicate key conflicts gracefully without triggering errors that cause transaction rollbacks.
    2. Sort IDs for Lock Ordering: Ensure all transactions acquire locks in the same order by sorting reminder_id values before processing.
    3. Batch Inserts: Use batch insertion to reduce the total time locks are held.
    4. Improve Error Handling: Ensure insertion errors are handled correctly rather than just being logged as warnings.
  4. Optimize database indexes and isolation levels

    main

    Adjust the database configuration to reduce lock contention:

    1. Index Optimization:
      • Remove the existing index: (reminder_id, uid).
      • Create a new index: (uid, reminder_id) (placing the more frequently used field first).
      • Add auxiliary indexes: (reminder_id) and (created_at).
    2. Transaction Isolation Level: If business logic permits, consider switching from REPEATABLE READ to READ COMMITTED to reduce the duration locks are held.
  5. Run the TangSengDaoDao API server

    main

    The server can be started using the default binary. By default, it runs in api mode. You can specify the server type as a command-line argument. If you provide api, config, or no argument, the API service will start.

    To specify a custom configuration file, use the -config flag.

  6. Deploy TangSengDaoDao via Docker Compose

    main

    The project provides a docker-compose.yaml file to orchestrate the full stack, including the business logic server, web interface, IM communication service, file storage, and databases.

    To deploy the stack, ensure you have the necessary configuration files (tsdd.yaml, wk.yaml) and environment variables prepared in your local directory as specified in the volumes section.

    version: '3.1'
    services:
      wukongim:
        image: registry.cn-shanghai.aliyuncs.com/wukongim/wukongim:latest
        # ... other services